閱讀357 返回首頁    go 阿裏雲 go 技術社區[雲棲]


css3 position fixed居中的問題

通常,我們要讓某元素居中,會這樣做:

#element{
margin:0 auto;
}


如果還想讓此元素位置固定呢?一般我們會添加position:fixed,如下:

#element{
position:fixed;
margin:0 auto;
}


但是這樣做的結果就是,元素不居中了。這說明fixed使對象脫離了正常文檔流

解決方案:

#element{
position:fixed;
margin:0 auto;
left:0;
right:0;
}

但是在IE7以下的版本中無法工作,要將其更改為:

#element{
position:fixed;
margin:0 auto;
left:auto;
right:auto;
}

最後我們可以這樣:

if ($.browser.msie && parseInt($.browser.version, 10) <= 7) {
    strAlertWrapper.css({position:'fixed', bottom:'0', height:'auto', left:'auto', right:'auto'});
 }
示例代碼:https://jsfiddle.net/4Ly4B/33/
如果對你有幫助,歡迎加入:QQ群:124116463,一起討論前端技術吧!






最後更新:2017-04-03 08:26:19

  上一篇:go ORACLE 按表字段值的不同統計數量
  下一篇:go XML(4)——schema文件相互引用