Home > Web Front-end > CSS Tutorial > How to Fill Remaining Container Width with CSS Using `calc`?

How to Fill Remaining Container Width with CSS Using `calc`?

Linda Hamilton
Release: 2024-10-27 06:58:02
Original
718 people have browsed it

How to Fill Remaining Container Width with CSS Using `calc`?

Filling Remaining Container Width with CSS

The objective is to maximize the width of an element named 'middle' within a container div, allowing it to occupy the remaining space after accommodating other elements.

Solution:

Utilizing the CSS property 'calc' is the key to achieving this. By calculating the available width and subtracting the fixed width of other elements, we can dynamically determine the remaining width.

<code class="css">#middle {
  display: inline-block;
  width: calc(100% - <fixed-element-width>);
}</code>
Copy after login

In the example provided:

<code class="html"><div class="left">100 px wide!</div><div class="right">Fills width!</div></code>
Copy after login

And CSS:

<code class="css">.left {
  width: 100px;
}
.right {
  width: calc(100% - 100px);
}</code>
Copy after login

The red element has a fixed width of 100px, while the blue element fills the remaining space after accounting for the fixed width.

As an alternative to leaving no space between the elements, you can set font-size: 0 on the outer container element.

The above is the detailed content of How to Fill Remaining Container Width with CSS Using `calc`?. 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