CSS中position屬性之fixed實現(xiàn)div居中
position 屬性規(guī)定元素的定位類型。這個屬性定義建立元素布局所用的定位機制。任何元素都可以定位,不過絕對或固定元素會生成一個塊級框,而不論該元素本身是什么類型。相對定位元素會相對于它在正常流中的默認(rèn)位置偏移。
上下左右 居中
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 元素在瀏覽器窗口居中
其實,實現(xiàn)這個效果并不復(fù)雜,利用 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; font-size: 14px; background-color:#F4F4F4; overflow:hidden; } </style> <div class="dialog">我是在窗口正中央的,呵呵!</div>
設(shè)置的技巧全部在這里:
.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)
效果圖
以上內(nèi)容是小編給大家分享的CSS中position屬性之fixed實現(xiàn)div居中的全部敘述,希望大家喜歡。
相關(guān)文章
js substr、substring和slice使用說明小記
關(guān)于substr、substring和slice方法區(qū)別的文章,網(wǎng)上搜到了許多,文章內(nèi)容也基本一致。而后,我將其中一篇文章中的代碼挪到本地進行了測試,發(fā)現(xiàn)測試結(jié)果和原文中的有些出入。2011-09-09JS區(qū)分Object與Aarry的六種方法總結(jié)
下面小編就為大家?guī)硪黄狫S區(qū)分Object與Aarry的六種方法總結(jié)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02JS封裝的自動創(chuàng)建表格的實現(xiàn)代碼
這篇文章主要介紹了JS封裝的自動創(chuàng)建表格的實現(xiàn)代碼的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-06-06