Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

background-repeat: tile, space or resize the image

Control each axis separately and distinguish gaps between tiles from rescaling the tiles themselves.

Version: CSS Modules

Syntax

background-repeat: space no-repeat;

background-repeat describes how each background image tiles after sizing and positioning. Its initial repeat repeats in both directions. With two keywords, the first controls the horizontal axis and the second the vertical one. repeat-x means repeat no-repeat, while repeat-y means no-repeat repeat. no-repeat paints only one copy on that axis; it does not resize the element or prevent clipping. For multiple images, comma-separated repeat styles follow layer order and a shorter list repeats.

repeat fills the painting area and can leave partial tiles at its edges. space fits as many whole copies as possible in the positioning area, puts the first and last against the edges and distributes the gaps. It ignores background-position on that axis when at least two copies fit; otherwise one copy uses that position. round instead changes the tile size to fit a whole number of copies. This interacts with background-size, including its auto dimension and image aspect ratio, rather than simply inserting space.

The example uses a red/blue 50×20px tile in 120px-wide boxes. repeat no-repeat leaves the last tile partly visible. space no-repeat paints two unchanged tiles with a 20px white gap. round no-repeat makes two 60px-wide tiles, each with 30px red and blue halves. A centered no-repeat tile begins at 35px. In a narrower 70px box, space fits only one copy and honors center, starting it at 10px. The console prints computed repeat styles; a real PNG scanline checks every color run and gap.

Examples

const cases=[['repeat no-repeat',120,'left top'],['space no-repeat',120,'center top'],['round no-repeat',120,'left top'],['no-repeat',120,'center top'],['space no-repeat',70,'center top']];
cases.forEach(([repeat,width,position],i)=>{
  const box=document.createElement('div');box.id='repeat-'+i;
  box.style.cssText='position:absolute;left:0;height:30px;background-image:linear-gradient(to right,red 50%,blue 50%);background-size:50px 20px';
  box.style.top=(i*40)+'px';box.style.width=width+'px';box.style.backgroundRepeat=repeat;box.style.backgroundPosition=position;document.body.append(box);
  console.log(i+': '+getComputedStyle(box).backgroundRepeat+'; width='+box.offsetWidth);
});

Result

0: repeat-x; width=120
1: space no-repeat; width=120
2: round no-repeat; width=120
3: no-repeat; width=120
4: space no-repeat; width=70

Executed in Chromium 157.0.8103.51 using hard-edged gradient tiles and exact scanlines at device pixel ratio 1. The test fixes both background-size dimensions, so automatic aspect-ratio adjustment under round and multi-layer overlap are not claimed as executed cases.

Sources