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

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

Linda Hamilton
Linda HamiltonOriginal
2024-10-27 06:58:02615browse

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>

In the example provided:

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

And CSS:

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

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!

Statement:
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