我是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;
}


小孩:

.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” ...

编辑:这是带有“!important”标记的css:

position:absolute !important;
left:0 !important;
right:0 !important;
top:0 !important;
bottom:0 !important;
margin:auto !important;

07-25 22:19
查看更多