【发布时间】:2020-01-22 18:31:44
【问题描述】:
如何获得正确的浮动 DIV 来填充其可用空间?
.content{
background-color: #fff;
margin: 0px auto;
width: 760px;
border: 1px solid blue;
font-size: 10pt;
}
.content .leftSide {
background-color: yellow;
float: left;
padding: 5px;
}
.content .rightSide {
background-color: orange;
float: left;
width: *;
padding: 5px;
text-align: center;
}
<div class="content">
<div class="leftSide"><img src="test.jpg"/></div>
<div class="rightSide">Right side text should be centered.</div>
<div style="clear:both"></div>
</div>
<div class="content">
<div class="leftSide"><img src="test2.jpg"/></div>
<div class="rightSide">And the background should fill the DIV of course.</div>
<div style="clear:both"></div>
</div>
中间答案:
感谢 Guffa 和 Rich,将 float:left 放在右侧允许文本居中,但要使背景颜色向下延伸,我还必须制作父 DIV 的背景颜色。
但是,我仍然无法让文本在中间垂直对齐,因为 DIV 实际上并没有一直向下,是否也有“自动”?例如高度:*,或浮动:自动?正如 Cletus 下面提到的,这在 HTML 表格中都是微不足道的,CSS 设计者肯定包含了一些属性来“使垂直空间向下填充”。
alt text http://tanguay.info/web/external/cssRightFixed.png
.content{
background-color: orange;
margin: 0px auto;
width: 760px;
border: 1px solid blue;
font-size: 10pt;
}
.content .leftSide {
background-color: yellow;
float: left;
padding: 5px;
}
.content .rightSide {
background-color: orange;
padding: 5px;
text-align: center;
vertical-align: middle;
/* DOESN'T WORK SINCE THE DIV DOES
NOT ACTUALLY GO ALL THE WAY DOWN */
}
<div class="content">
<div class="leftSide"><img src="test.jpg"/></div>
<div class="rightSide">Right side text should be centered.</div>
<div style="clear:both"></div>
</div>
<div class="content">
<div class="leftSide"><img src="test2.jpg"/></div>
<div class="rightSide">And the background should fill the DIV of course.</div>
<div style="clear:both"></div>
</div>
<div class="content">
<div class="leftSide"><img src="test3.jpg"/></div>
<div class="rightSide">And the background should fill the DIV of course.</div>
<div style="clear:both"></div>
</div>
<div class="content">
<div class="leftSide">this is a text on the left with no image</div>
<div class="rightSide">And the background should fill the DIV of course.</div>
<div style="clear:both"></div>
</div>
【问题讨论】:
标签: css