How To Make A Div Fill The Remaining Horizontal Space Using CSS?
Last Updated :
19 Nov, 2024
To make a div fill the remaining horizontal space in a container using CSS, you can use different techniques to achieve a flexible and responsive layout. This is useful when designing layouts where some elements occupy a fixed space, while others expand to fill the rest of the available space.
1. Using Flexbox
Flexbox is a layout model that makes it easy to distribute space and align-items within a container. It automatically allows a div to fill the available space flexibly.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.container {
display: flex;
width: 100%;
}
.fixed-width {
width: 200px;
background-color: lightcoral;
padding: 10px;
text-align: center;
}
.fill-space {
flex: 1;
background-color: lightblue;
padding: 10px;
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<div class="fixed-width">Fixed Width</div>
<div class="fill-space">Fills Remaining Space</div>
</div>
</body>
</html>
Explanation
- Flexbox Setup: The .container class uses display: flex to create a flexible layout.
- Fixed Width Element: The .fixed-width class specifies a fixed width for the first div.
- Fill Space Element: The .fill-space class uses flex: 1, which allows it to grow and occupy the remaining horizontal space
Output

Make A Div Fill The Remaining Horizontal Space Using CSS
2. Using calc() with CSS Width
The calc() function can dynamically calculate the remaining space by subtracting the width of other elements from the total width.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.container {
width: 100%;
display: flex;
}
.fixed-width {
width: 250px;
background-color: lightgreen;
padding: 10px;
text-align: center;
}
.fill-space {
width: calc(100% - 250px);
background-color: lightgoldenrodyellow;
padding: 10px;
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<div class="fixed-width">Fixed Width</div>
<div class="fill-space">Fills Remaining Space</div>
</div>
</body>
</html>
Explanation
- calc() Function: The .fill-space class calculates its width as the total container width (100%) minus the width of the .fixed-width element (250px).
- Responsive Layout: This approach allows the .fill-space element to adjust dynamically when the container’s width changes.
Output

Make A Div Fill The Remaining Horizontal Space Using CSS
3. Using Float and Overflow
This method uses the traditional float technique, which has been around for a long time in CSS. By floating one element to the left or right and setting the overflow property on the second element, you can achieve the desired effect.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.fixed-width {
float: left;
width: 150px;
background-color: lightgreen;
padding: 10px;
text-align: center;
}
.fill-space {
overflow: hidden;
background-color: lightpink;
padding: 10px;
text-align: center;
}
</style>
</head>
<body>
<div class="fixed-width">Fixed Width</div>
<div class="fill-space">Fills Remaining Space</div>
</body>
</html>
Explanation
- Floating the Fixed Element: The .fixed-width class floats the first element to the left.
- Expanding the Second Element: The .fill-space class uses overflow: hidden to make the second element expand and fill the remaining space.
Output

Make A Div Fill The Remaining Horizontal Space Using CSS
Make A Div Fill The Remaining Horizontal Space Using CSS – FAQs
Which approach is the most modern and recommended?
Flexbox and CSS Grid are the most modern and widely recommended techniques for achieving flexible layouts.
Can I use these approaches for vertical space as well?
Yes, Flexbox and CSS Grid can handle both horizontal and vertical space allocation.
Is it necessary to use CSS Grid for simple layouts?
No, Flexbox is more suitable for simpler, one-dimensional layouts (rows or columns).
What’s the difference between Flexbox and Grid?
Flexbox is best for one-dimensional layouts (either rows or columns), while Grid handles two-dimensional layouts (both rows and columns).
How does calc() work with responsive designs?
The calc() function dynamically calculates sizes, making it flexible for responsive designs by adjusting to changing container dimensions.