9.6 · Layout

Fill without overflowing: width: stretch

Fill the available space and respect margins, unlike width: 100%.

width: 100% means 100% of the container, and margins are added on top, so an element with margins overflows. That’s why people reach for calc(100% - 3rem).

The stretch keyword sizes the element to fill the space that’s left after its margins. It works for inline-size, block-size and their min and max versions. Older browsers know it as -webkit-fill-available or -moz-available.

Before
.bar {
  margin-inline: 1.5rem;
  width: calc(100% - 3rem);
}
Now
.bar {
  margin-inline: 1.5rem;
  inline-size: stretch;
}