Snapping Container Height to a Background Pattern Step with CSS calc-size()Brief · digest for October 16
Brief2 sections≈ 2 min

Snapping Container Height to a Background Pattern Step with CSS calc-size()

You've surely spotted this annoying visual flaw when styling decorative sections. You give a container a geometric background pattern—like a dot grid, diagonal stripes, or isometric diamonds—and at the bottom edge of the box, the pattern gets awkwardly sliced in half.

Using background-repeat: round prevents slicing, yet does so bluntly: the browser forces tiles to squash or stretch, distorting your graphics. As we noted in CSS Trends 2026: from Anchor Positioning to declarative routes and gap decorators, modern CSS gives us much sharper math tools.

Frontend expert Ahmad Shadeed demonstrated a clever trick. Instead of scaling the pattern, we snap the height of the container itself using the new calc-size() and round() functions.

Base Markup and Fallback

First, define the background pattern using CSS custom properties along with a min-height fallback:

.pattern-section {
  --tile-width: 48px;
  --tile-height: 40px;

  background-image: url("/assets/pattern-grid.svg");
  background-size: var(--tile-width) var(--tile-height);
  background-repeat: repeat;

  /* Basic fallback for older browsers */
  min-height: 240px;
}

Rounding Container Height Mathematically

Next, apply the calc-size() function available in Chromium-based browsers:

.pattern-section {
  /* Round the automatic box height up to a whole tile multiple */
  height: calc-size(auto, round(up, size, var(--tile-height)));
}

calc-size() measures the content's natural intrinsic height (auto), and round(up, size, var(--tile-height)) rounds that value up to the next multiple of the grid step. As a result, the bottom edge of the section always aligns cleanly with a full row of tiles, without stretching a single pixel.

Browser Support and Progressive Enhancement

Since calc-size() is currently supported mainly in Chromium/Blink, wrap the enhanced rule in @supports (height: calc-size(auto, size)). Unsupported browsers will cleanly keep the standard min-height.

Want a deep diveVotes help us choose what to break down in detail. This material has 0 votes.
Sources · 2