1

例如,我希望 h1 元素调整大小以适应浮动的红色 div 旁边。文本已经适应浮动,但背景在浮动元素的后面。

我知道我可以为 h1 添加右边距,但红色块的宽度可以是可变的,我不能有可变的边距。

CSS:

#redblock {            
    background: red;
    float: right;
    margin: 10px;
    width: 100px;
    height:100px;
}
#wrapper {
    border: 1px solid black;
    margin: 10px;
    padding: 10px;
    width: 300px;
}
p {
    background-color: #ffd;
}
h1 {
    background-color: #667788;
    font-size: 1.1em;
    margin: 10px 0;
}

HTML:

<div id="wrapper">
    <div id="redblock"></div>

    <h1>Test tEst teSt tesT</h1>

    <p>Test tEst teSt tesT Test tEst teSt tesT Test tEst teSt tesT Test tEst teSt tesT Test tEst teSt
    tesT Test tEst teSt tesT Test tEst teSt tesT</p>
</div>

http://jsbin.com/ucili4

4

1 回答 1

0

如果您添加一个width:80%forh1将创建一个宽度,该宽度将找到当前红色框的中间。因此,如果您有更大或更小的红色框,我想它会起作用。(可能是灰色背景,是为了测试,对吧?)

示例:http: //jsbin.com/okive4

于 2011-01-15T10:40:17.657 回答