How to Achieve Variable Width Distribution in Nested Divs with Overflow: Hidden?

Barbara Streisand
Release: 2024-11-07 18:32:03
Original
600 people have browsed it

How to Achieve Variable Width Distribution in Nested Divs with Overflow: Hidden?

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>
Copy after login

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;
}
Copy after login

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]-->
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!