欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

css中position:fixed實(shí)現(xiàn)div在窗口上下左右居中

  發(fā)布時間:2014-10-20 17:13:43   作者:佚名   我要評論
實(shí)現(xiàn)div居中的方法有很多,本例介紹的使用css中position:fixed來實(shí)現(xiàn)div的上下左右居中,下面以一個DIV 元素在瀏覽器窗口居中為例
上下左右 居中

代碼如下

復(fù)制代碼
代碼如下:

div{
position:fixed;
margin:auto;
left:0;
right:0;
top:0;
bottom:0;
width:200px;
height:150px;
}

如果只需要左右居中,那么把 bottom:0; 或者 top:0; 刪掉即可
如果只需要上下居中,那么把 left:0; 或者 right:0; 即可

下面附一個DIV 元素在瀏覽器窗口居中

其實(shí),實(shí)現(xiàn)這個效果并不復(fù)雜,利用 CSS 中的 position 定位就可以輕松搞定了。來看看代碼吧:

代碼如下

復(fù)制代碼
代碼如下:

<style type="text/css">
.dialog{
position: fixed;
_position:absolute;
z-index:1;
top: 50%;
left: 50%;
margin: -141px 0 0 -201px;
width: 400px;
height:280px;
border:1px solid #CCC;
line-height: 280px;
text-align:center;
font-size: 14px;
background-color:#F4F4F4;
overflow:hidden;
}
</style>
<div class="dialog">我是在窗口正中央的,呵呵!</div>

設(shè)置的技巧全部在這里:

代碼如下

復(fù)制代碼
代碼如下:

.dialog{
position: fixed;
_position:absolute; /* hack for IE6 */
z-index:1;
top: 50%;
left: 50%;
margin: -141px 0 0 -201px;
width: 400px;
height:280px;
border:1px solid #CCC;
line-height: 280px;
text-align:center;
font-size: 14px;
background-color:#F4F4F4;
overflow:hidden;
}

設(shè)置 position: fixed; _position:absolute;
設(shè)置 left:50% 和 top:50%;
設(shè)置 margin: -(DIV的offsetWidth/2) 0 0 -(DIV的offsetHeight/2)

相關(guān)文章

最新評論