CSS background-repeat Property

Example

Repeat a background-image only vertically:

body {
background-image: url(“paper.gif”);
background-repeat: repeat-y;
}

Try it Yourself »

More “Try it Yourself” examples below.


Definition and Usage

The background-repeat property sets if/how a background image will be repeated.

By default, a background-image is repeated both vertically and horizontally.

Tip: The background image is placed according to the background-position property. If no background-position is specified, the image is always placed at the element’s top left corner.

Default value: repeat
Inherited: no
Animatable: no. Read about animatable
Version: CSS1
JavaScript syntax: object.style.backgroundRepeat=”repeat-x” Try it

CSS Syntax
background-repeat: repeat|repeat-x|repeat-y|no-repeat|initial|inherit;
Property Values
Value Description Code
repeat The background image is repeated both vertically and horizontally.  The last image will be clipped if it does not fit. This is default div {
background-image:url(wcss.gif);
background-repeat:repeat ;
}
repeat-x The background image is repeated only horizontally div {
background-image:url(wcss.gif);
background-repeat:repeat-x;
}
repeat-y The background image is repeated only vertically div {
background-image:url(wcss.gif);
background-repeat:repeat-y;
}
no-repeat The background-image is not repeated. The image will only be shown once div {
background-image:url(wcss.gif);
background-repeat:no-repeat ;
}
space The background-image is repeated as much as possible without clipping. The first and last images are pinned to either side of the element, and whitespace is distributed evenly between the images div {
background-image:url(wcss.gif);
background-repeat:space;
}
round The background-image is repeated and squished or stretched to fill the space (no gaps) div {
background-image:url(wcss.gif);
background-repeat:round;
}
initial Sets this property to its default value. Read about initial div {
background-image:url(wcss.gif);
background-repeat:initial;
}
inherit Inherits this property from its parent element. Read about inherit

Example

Repeat a background image both vertically and horizontally (this is default):

body {
background-image: url(“paper.gif”);
background-repeat: repeat;
}

Try it Yourself »

Example

Repeat a background image only horizontally:

body {
background-image: url(“paper.gif”);
background-repeat: repeat-x;
}

Try it Yourself »

Example

Do not repeat a background image. The image will only be shown once:

body {
background-image: url(“paper.gif”);
background-repeat: no-repeat;
}

Try it Yourself »

Example

Using background-repeat: space and background-repeat: round:

#example2 {
border: 2px solid black;
padding: 25px;
background: url(“w3css.gif”);
background-repeat: space;
}

#example3 {
border: 1px solid black;
padding: 25px;
background: url(“w3css.gif”);
background-repeat: round;
}

Try it Yourself »

Post Author: Zahid Farid