У меня есть div, содержащий два выровненных div
s. К сожалению, правый div
не имеет надлежащей высоты, как видно из его заднего фона. Как я могу убедиться, что он будет настолько высок, насколько он высок по сравнению с ангелом div
?
Html:
<div id="engl">
<div id="lleft">Some text text
text text text text text text
text text text text text text
</div>
<div id="rright">Other text</div>
</div>
CSS:
#engl {
background-color: red;
width: 200px;
height: auto;
overflow: auto;
}
#lleft {
background-color: yellow;
width: 140px;
padding: 10px;
float: left;
}
#rright {
background-color: green;
width: 40px;
float: left;
height: inherited;
}
JsFiddle: http://jsfiddle.net/9bLLN/1/
Вы можете использовать позиционирование для этого:
#engl {
background-color: red;
width: 200px;
height: auto;
overflow: auto;
position:relative; /*NEW*/
}
#lleft {
background-color: yellow;
width: 140px;
padding: 10px;
float: left;
}
#rright {
background-color: green;
width: 40px;
float: left;
height: inherited;
position: absolute; /*NEW*/
top:0; /*NEW*/
bottom:0; /*NEW*/
right:0; /*NEW*/
}
Измененный html:
<div id="engl">
<div id="lleft">Some text text
text text text text text text
text text text text text text
</div>
<div id="rright">Other text</div>
<div style="clear: both"></div>
</div>
Измененный css:
#rright {
background-color: green;
width: 40px;
float: left;
height: 100%;
}
Плавающие элементы не могут сказать свою высоту своим родителям. Это связано с тем, что требуется третий, пустой DIV с "clear: both".
Это решение немного проблематично, потому что с точки зрения браузеров это не тривиально, высота какого элемента должна указывать, какая. Но с текущими браузерами (ie8+) работает.