我有2个div,我希望其中一个在屏幕上居中,另一个在其父项#MyContent的左边(居中)。但是,似乎要添加“ Float:left;”。为了#Latest导致DOM错误,#MyContent不再持有#Latest,并且缩小了#Latest,就像#Latest是“ position:absolute;”一样。当其“相对”时。

我有这些:

HTML:

<center>
    <div id="MyContent">
        <div id="Latest">
            <div class="Last"></div>
        </div>
    </div>
</center>


CSS:

#MyContent {
    position: relative;
    font-family: FontStencil;
    font-size: 12px;
    padding: 20px 20px 160px 20px;
    color: black;
    background: rgba(240, 240, 240, 0.2);
    border-radius: 18px;
    border: 1px solid #CCCCCC;
    top: 250;
    width: 950px;
    box-shadow: 0px 5px 15px -2px black;
}

#Latest {
    position: relative;
    font-family: FontStencil;
    float: left; /* When I remove THIS, everything works Fine BUT its all centered */
    text-align: left;
    font-size: 18px;
    padding: 20px 20px 30px 20px;
    color: #000033;
    background: white;
    border-radius: 18px;
    border: 1px solid #CCCCCC;
    left: 20px;
    top: 30px;
    width: 570px;
    box-shadow: 0px 1px 17px -5px black;
}

.Last {
    position: relative;
    font-family: SuperG;
    text-align: left;
    right: -18px;
    font-size: 12px;
    padding: 15px;
    color: #000033;
    background: white;
    border-radius: 18px;
    border: 1px solid #CCCCCC;
    top: 25px;
    width: 500px;
    height: 415px;
    box-shadow: 0px 1px 17px -5px black;
}


一切都在位置:相对;但它的行为就像是否在我添加float时#Latest不是:帮忙。

最佳答案

的CSS

#MyContent{
font-family:FontStencil;
font-size:12px;
padding:20px 20px 160px 20px;
color:black;
background:rgba(240,240,240, 0.2);
border-radius:18px;
border:1px solid #CCCCCC;
top:250;
width:950px;
box-shadow: 0px 5px 15px -2px black;
margin: 0 auto;
    display: inline-block;
}

#Latest{
position:relative;
font-family:FontStencil;
float: left; /* When I remove THIS, everything works Fine BUT its all centered */
text-align:left;
font-size:18px;
padding:20px 20px 30px 20px;
color:#000033;
background:white;
border-radius:18px;
border:1px solid #CCCCCC;
left:20px;
top:30px;
width:570px;
box-shadow: 0px 1px 17px -5px black;
}

.Last{
position:relative;
font-family:SuperG;
text-align:left;
right:-18px;
font-size:12px;
padding:15px;
color:#000033;
background:white;
border-radius:18px;
border:1px solid #CCCCCC;
top:25px;
width:500px;
height:415px;
box-shadow: 0px 1px 17px -5px black;
}


试试上面的CSS。 DEMO

关于javascript - 使div居中以保持 child 向左 float ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18527507/

10-12 01:01