我是 CSS 新手,但我尝试了不同的方法,但我就是无法让我的 div 垂直居中。
父级:
#ModalScroll{
position:absolute;
top:0;
left:0;
z-index:1001;
height:1px;
width:1px;
overflow:hidden;
display:none;
font-size:100%;
text-align:left;
}
child :
.ModalDialog{
border:1px solid #3C4749;
background-color:#FFF;
margin:0 auto 0 auto;
display:none;
font-size: 12px;
box-sizing: border-box;
width: 280px;
height: 160px;
position: absolute;
left: 50%;
top: 50%;
margin: -80px 0 0 -140px;
}
我不知道 jQuery 中的解决方案是否会更好,但我更喜欢 CSS 中的解决方案。
最佳答案
您是否使用了 firebug 来确保您的 css 得到充分利用? (也许其他 CSS 会重载您元素的某些属性...)
position:absolute; /*it can be fixed too*/
left:0; right:0;
top:0; bottom:0;
margin:auto;
应该可以解决问题(如 Center a DIV horizontally and vertically 中所述) 也许尝试为每个属性添加“!important”...
编辑:这是带有“!重要”标记的CSS:
position:absolute !important;
left:0 !important;
right:0 !important;
top:0 !important;
bottom:0 !important;
margin:auto !important;
关于jquery - 我应该如何使 div 垂直和水平居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29646579/