css中position:fixed實現div置中

來源:互聯網
上載者:User

標籤:style   http   color   io   os   ar   for   div   on   

上下左右 置中

代碼如下 複製代碼
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 元素在瀏覽器視窗置中

其實,實現這個效果並不複雜,利用 CSS 中的 position 定位就可以輕鬆搞定了。來看看代碼吧:

代碼如下 複製代碼
<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;
fon(www.111cn.net)t-size: 14px;
background-color:#F4F4F4;
overflow:hidden;
}
</style>

<div class="dialog">我是在視窗正中央的,呵呵!</div>

設定的技巧全部在這裡:

代碼如下 複製代碼
.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;
}

設定 position: fixed; _position:absolute;
設定 left:50% 和 top:50%;
設定 margin: -(DIV的offsetWidth/2) 0 0 -(DIV的offsetHeight/2)
from:http://www.111cn.net/cssdiv/css/62281.htm

css中position:fixed實現div置中

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.