How to Achieve Variable Width Distribution in Nested Divs
In xHTML/CSS, it is common to encounter a scenario where multiple nested divs need to be aligned horizontally, with the inner divs having varying widths based on their content. This question explores a common challenge: how to allocate the remaining available horizontal space to one of the inner divs when its width is not specified and depends on its content.
To achieve this effect, we can utilize the CSS property "overflow: hidden;". This property prevents elements adjacent to floating elements from extending behind the float, creating a controlled layout.
Consider the HTML structure below:
<div>
To configure the layout, the following CSS can be applied:
#outer { overflow: hidden; width: 100%; border: solid 3px #666; background: #ddd; } #inner1 { float: left; border: solid 3px #c00; background: #fdd; } #inner2 { overflow: hidden; border: solid 3px #00c; background: #ddf; }
By setting "overflow: hidden;" on the "outer" div, we force the floated "inner1" div to remain contained within the "outer" div. The "inner2" div then fills the remaining horizontal space.
To ensure that this layout works in older versions of Internet Explorer (IE 6 and 7), the following additional CSS can be used with HTML conditional comments:
<!--[if lte IE 6]> <style type="text/css"> #inner2 { zoom: 1; } #inner1 { margin-right: -3px; } </style> <![endif]-->
This style ensures that the "inner2" div fills the remaining space correctly in IE 6 and adjusts the "inner1" div's margin to compensate for a gap introduced by the "zoom" property.
With this configuration, the "inner1" div will dynamically adjust its width based on its content, and the "inner2" div will occupy the remaining available width, creating a layout where the divs are aligned horizontally with their respective widths.
The above is the detailed content of How to Achieve Variable Width Distribution in Nested Divs with Overflow: Hidden?. For more information, please follow other related articles on the PHP Chinese website!