如何使 div 宽度灵活?
How to make a div width flexible?
我有 2 个 div 坐在 side.I 旁边,将用来自 database.Now 的文本填充两个 div,div L.H.S it.On 中可能有也可能没有文本,另一方面 R.H.S 上的 div。 it.My 中总会有文本问题是;我如何设计 R.H.S div 以便当 L.H.S div 中没有文本时它会动态增长到 100% 如果有文本在 L.H.S div 中,它将调整到其原始宽度的 50%。
我的 div 看起来像:
<div id="left" style="float:right;width:50%;background-color:yellow;">
Left content
</div>
<div id="right" style="float:right;width:50%;background-color:red;">
Right content
</div>
您可以使用 CSS 选择器执行此操作:
<div id="left">Left content</div>
<div id="right">Right content</div>
CSS
#left, #right {
float:left;
display:block;
background:#eee;
width:50%;
}
#left:empty {
width:0;
}
#left:empty + #right {
width:100%;
}
请注意:要使 :empty
正常工作,您的 div 中必须没有任何内容,甚至 space.
中也不能
我有 2 个 div 坐在 side.I 旁边,将用来自 database.Now 的文本填充两个 div,div L.H.S it.On 中可能有也可能没有文本,另一方面 R.H.S 上的 div。 it.My 中总会有文本问题是;我如何设计 R.H.S div 以便当 L.H.S div 中没有文本时它会动态增长到 100% 如果有文本在 L.H.S div 中,它将调整到其原始宽度的 50%。
我的 div 看起来像:
<div id="left" style="float:right;width:50%;background-color:yellow;">
Left content
</div>
<div id="right" style="float:right;width:50%;background-color:red;">
Right content
</div>
您可以使用 CSS 选择器执行此操作:
<div id="left">Left content</div>
<div id="right">Right content</div>
CSS
#left, #right {
float:left;
display:block;
background:#eee;
width:50%;
}
#left:empty {
width:0;
}
#left:empty + #right {
width:100%;
}
请注意:要使 :empty
正常工作,您的 div 中必须没有任何内容,甚至 space.