以div为中心让孩子们向左漂浮

时间:2013-08-30 07:40:37

标签: javascript html css

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

一切都是位置:相对;但是当我添加一个浮点数时,它就像#Latest一样:left; HALP。

1 个答案:

答案 0 :(得分:1)

<强> 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