brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: width: min(100% – 2rem, 70rem) in CSS is a container in one line

Css
~/tips/tip-css-min-width-container

width: min(100% - 2rem, 70rem);

A centered column that stays narrow on wide screens and keeps side space on phones.

The classic content wrapper uses max-width, padding and a media query for small screens. min() picks the smaller of two values and does all of that in one declaration.

.wrapper {
  width: min(100% - 2rem, 70rem);
  margin-inline: auto;
}

On a wide screen 70rem is the smaller value, so the column stops there. On a phone 100% - 2rem wins, which leaves 1rem of space on each side after centering. No padding is needed, so a full width background inside the wrapper is not boxed in by it.

Math inside min() works without an extra calc(). The spaces around the minus are required though: 100%-2rem is invalid and the whole line is dropped.

max() is the mirror image and is handy for a minimum, like padding: max(1rem, 3vw). Combine both and you end up with clamp().

All current browsers support these functions; Safari since 11.1. The one thing I’d watch is what 100% refers to: the width of the containing block. On an absolutely positioned element or inside a flex item that shrinks, that can be much smaller than the screen.

Further reading: MDN: min()

More Css tips CSS Overview in Chrome DevTools audits the colors and fonts of a page .notice:empty { .toast { article { counter-reset: h2; } .toolbar { All Css tips (66) All topics

Link