使用自动高度与浮动

时间:2014-11-22 10:05:24

标签: html css

当使用浮动时自动高度不起作用但是当移除浮动时它的工作正常。怎么解决呢?

有自动高度的DIV( aboutus ),里面是另一个div( aboutus-title p ),左边有浮动但是内容溢出怎么可以全部内容div内有自动高度?

http://jsfiddle.net/haeb0q8d/1/



.aboutus {
	position: relative;
	z-index: 2;
	width: 100%;
	height: auto;
	background: #333333;
}

.aboutus-title div h1{
	text-align: center;
	text-transform: uppercase;
	font-size: 24px;
	padding-top: 80px;
	color: #fcd803;
}

.aboutus-title hr {
	margin:  0 auto;
	border: 0;
    height: 1px;
    background: #333;
    margin-top: 30px;
    width: 60px;
}

.aboutus-detail {
	width: 100%;
}

.aboutus-detail p{
	text-align: center;
	color: #fcd803;
	line-height: 25px;
	font-size: 17px;
	margin-bottom: 30px;
	padding-right: 30px;
	padding-left: 30px;
    float: left;
}

<div class="aboutus" id="aboutus">
  <div class="aboutus-title">
    <div><h1>about</h1></div>
    <hr>
    <div class="aboutus-detail">
      <p>
        We are a tight knit team of digital thinkers, designers and<br>
        developers, working together to create fresh, effective projects<br> delivered personally.
      </p>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您必须使用clear: both清除浮动。

.aboutus {
    position: relative;
    z-index: 2;
    width: 100%;
    height: auto;
    background: #333333;
}
.aboutus-title div h1 {
    text-align: center;
    text-transform: uppercase;
    font-size: 24px;
    padding-top: 80px;
    color: #fcd803;
}
.aboutus-title hr {
    margin: 0 auto;
    border: 0;
    height: 1px;
    background: #333;
    margin-top: 30px;
    width: 60px;
}
.aboutus-detail {
    width: 100%;
}
.aboutus-detail p {
    text-align: center;
    color: #fcd803;
    line-height: 25px;
    font-size: 17px;
    margin-bottom: 30px;
    padding-right: 30px;
    padding-left: 30px;
    float: left;
}
.clear {
    clear: both;
}
<div class="aboutus" id="aboutus">
    <div class="aboutus-title">
        <div>
            <h1>about</h1>
        </div>
        <hr>
        <div class="aboutus-detail">
            <p>We are a tight knit team of digital thinkers, designers and
                <br>developers, working together to create fresh, effective projects
                <br>delivered personally.</p>
        </div>
    </div>
    <div class="clear"></div>
</div>