Home > Web Front-end > CSS Tutorial > How to Make a Div Fill Remaining Width in a Parent Container?

How to Make a Div Fill Remaining Width in a Parent Container?

Linda Hamilton
Release: 2024-12-02 01:57:13
Original
634 people have browsed it

How to Make a Div Fill Remaining Width in a Parent Container?

Achieving Dynamic Width Distribution: Filling Remaining Div Width

When working with multiple divs within a parent div, the task of ensuring that one div fills up the remaining width can arise. This technique can be particularly useful in creating responsive layouts that accommodate varying content sizes.

In your provided HTML code, you have a parent div (#Main) with two divs (#div1 and #div3) of fixed widths and a third div (#div2) that you want to fill up the remaining space. To accomplish this, you can employ several methods:

Floating Divs:

<style>
    #divMain { width: 500px; }
    #left-div { width: 100px; float: left; }
    #middle-div { float: left; width: calc(100% - 200px); }
    #right-div { width: 100px; float: right; }
</style>
Copy after login

Flexbox Layout:

<style>
    #divMain { 
        display: flex;
        width: 500px; 
    }
    #left-div { width: 100px; }
    #middle-div { flex-grow: 1; }
    #right-div { width: 100px; }
</style>
Copy after login

Grid Layout:

<style>
    #divMain { 
        display: grid;
        grid-template-columns: 100px auto 100px; 
    }
</style>
Copy after login

The above is the detailed content of How to Make a Div Fill Remaining Width in a Parent Container?. 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