Div未在section标签内正确嵌套

时间:2013-08-31 15:41:43

标签: html css html5 css3

我正在使用以下HTML和CSS代码尝试在其中创建一个包含3个div的部分,问题是即使div位于section标签内,在网页中它们也会出现在section element?

HTML代码

<section id="content">
<div class="homebox">
<h3>Who I am</h3>
<p>Here is some text  Here is some text  Here is some text  Here is some text  </p>
</div>

<div class="homebox">
<h3>What I do</h3>
<p> Here is some text  Here is some text  Here is some text  Here is some text  Here is some text  Here is some text   </p> 
</div>

<div class="homebox">
<h3>Where I do it</h3>
<p> Here is some text  Here is some text  Here is some text  Here is some text  Here is some text  Here is some text  </p>
</div>

</section>

这是我正在使用的CSS代码

#content {
    color:#FFF;
    margin-top: 20px;
    width: 100%;
    padding-left: 20px;
}

#content h3 {
    color:#FFF;
    font-size: 40px;
    font-family: Impact, Arial, sans-serif;
    margin:0;
    font-weight: 100;
}

#content > .homebox {
    float:left;
    width: 28%;
    padding: 0px 20px 20px; /* Top 0 padding, left and right 20px, bottom 20px padding */
    margin-right: 18px;
    text-align:center;
    border-radius:40px;
    background: #818181;
    background: -moz-radial-gradient(center, ellipse cover,  rgba(234,211,0,0.6) 0%, rgba(255,255,255,0) 100%); /* FF3.6+ */
    background: -webkit-gradient(radial, center center, 0px, center center, 100%, color-stop(0%,rgba(234,211,0,0.6)), color-stop(63%,rgba(255,255,255,0.22)), color-stop(100%,rgba(255,255,255,0))); /* Chrome,Safari4+ */
    background: -webkit-radial-gradient(center, ellipse cover,  rgba(234,211,0,0.6) 0%,rgba(255,255,255,0.22) 63%,rgba(255,255,255,0) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-radial-gradient(center, ellipse cover,  rgba(234,211,0,0.6) 0%,rgba(255,255,255,0.22) 63%,rgba(255,255,255,0) 100%); /* Opera 12+ */
    background: -ms-radial-gradient(center, ellipse cover,  rgba(234,211,0,0.6) 0%,rgba(255,255,255,0.22) 63%,rgba(255,255,255,0) 100%); /* IE10+ */
    background: radial-gradient(ellipse at center,  rgba(234,211,0,0.6) 0%,rgba(255,255,255,0.22) 63%,rgba(255,255,255,0) 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#99ead300', endColorstr='#00ffffff',GradientType=1 ); /* IE6-9 fallback on horizontal gradient */
}

.homebox > p {
    margin: 0px;
    color: #FFF;
    font-family: Trebuchet MS, Arial, sans-serif;
}

1 个答案:

答案 0 :(得分:5)

您需要清除浮动或触发新的块格式化上下文:

#content {
    color:#FFF;
    margin-top: 20px;
    width: 100%;
    padding-left: 20px;
    overflow: auto; /* Will cause all child floats to be enclosed. */
}

如何工作 - 块格式化上下文

通过将overflow: auto属性添加到块元素,CSS引擎会触发新的块格式化上下文。这意味着块中的所有内容都将在块中格式化,并忽略块外的任何元素。如果你有浮点数,那么浮点数会注意父元素的边缘,并且不受父元素之外的任何内容的影响。

参考:http://www.w3.org/TR/CSS2/visuren.html#block-formatting

相关问题