2

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

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;
}
4

1 回答 1

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

于 2013-08-31T15:45:23.417 回答