How CSS background-repeat: round Scales Patterns

This article provides an overview of the CSS background-repeat: round property value, detailing how it works, how it differs from traditional background repetition methods, and the mathematical logic browsers use to scale image tiles. You will learn how to prevent clipped background images and maintain seamless, gap-free tiled designs across dynamically sized containers.


What is background-repeat: round?

The round keyword is a value for the CSS background-repeat property. Traditionally, using background-repeat: repeat tiles an image across an element's background at its original or specified size. If the element's dimensions are not an exact multiple of the image's dimensions, the pattern abruptly cuts off at the edge.

The round value prevents this clipping. It instructs the browser to repeat the image as many times as possible, but automatically adjusts the dimensions of the tiles—stretching or squishing them—so that only a whole number of complete tiles fills the container from edge to edge.

How the Scaling Logic Works

When background-repeat: round is applied, the browser calculates how many tiles can fit along the specified axis (horizontal, vertical, or both) using the following process:

  1. Calculates Potential Count: The browser divides the container's dimension by the background image's specified or intrinsic size along that axis.
  2. Determines Nearest Whole Number:
    • If the remaining fractional space is less than half the width (or height) of an additional tile, the browser discards the partial tile and stretches the existing full tiles to fill the remaining area.
    • If the remaining fractional space is equal to or greater than half the size of a tile, the browser adds an extra tile and compresses all tiles to make room.
  3. Rescales the Tile: Once the whole integer count of tiles (\(N\)) is established, the browser rescales the tile's dimension along that axis to exactly:

\[\text{New Tile Dimension} = \frac{\text{Container Dimension}}{N}\]

Because the tile scales proportionally along the repeating axis, it maintains full coverage without gaps, margins, or truncated edges.

Syntax and Multi-Axis Control

The background-repeat property accepts either one value (affecting both axes) or two values (defining horizontal and vertical behavior independently):

/* Scales and fits whole tiles both horizontally and vertically */
.pattern-container {
  background-image: url('tile.png');
  background-repeat: round;
}

/* Scales horizontally, but uses standard repeat vertically */
.hybrid-container {
  background-image: url('tile.png');
  background-repeat: round repeat;
}

/* Scales horizontally, but prevents vertical repetition entirely */
.banner {
  background-image: url('tile.png');
  background-repeat: round no-repeat;
}

round vs. repeat vs. space

Understanding when to choose round depends on how you want the browser to handle leftover space:

  • repeat: Retains the original tile size. Any extra space at the edge results in a clipped, partial image tile.
  • space: Retains the original tile size. Never clips tiles, but instead distributes any leftover space evenly between the tiles as whitespace.
  • round: Distorts the tile size slightly to ensure zero gaps between tiles and zero clipped edges.

Practical Use Cases

  • Seamless Borders and Dividers: Ideal for scalloped edges, stitch patterns, or decorative dividing lines where half a tile would break visual consistency.
  • Responsive Layouts: In fluid grid layouts where container widths are percentages, round ensures that expanding or shrinking the viewport never causes a background pattern to terminate halfway through a design motif.