【转载】用CSS将登录框水平垂直居中于浏览器

来源:互联网 发布:外网监控软件 编辑:程序博客网 时间:2024/06/05 03:17

用CSS将登录框水平垂直居中于浏览器,代码:

<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>无标题文档</title> 
<style type="text/css"> 
    html,body{ margin:0; padding:0; border:0; background-color:#333;} 
    #loginPanel{background-color:#eee;} 
</style> 
</head>

<body>

<div id="loginPanel" style="position:absolute; top:50%; left:50%; width:480px; height:240px; margin-left:-240px; margin-top:-120px;">

这里是登录

</div>

20101127092314031

 

要点:

1、登录面板div绝对布局(position:absolute;),位置topleft都是50%(相对于浏览器窗口尺寸); 
(现在若登录面板div宽度和高度都是0的话,它就刚好居中了。……那为0有什么用?!……当然没用,只是说明原理)

2、要定死登录面板div的宽度和高度,一定要定死!例如:width:480px;height:240px;

3、登录面板div有宽度和高度了,那它就不居中了,它有多宽,整个面板div就会向右偏移宽度的一半;它有多高,就会向下偏移高度的一半

4、再移回去!例如:margin-left:-240px;margin-top:-120px;

完成!

0 0
原创粉丝点击