CSS

background-repeat


The background-repeat property in CSS is used to control the repetition of background images. It can be set to repeat horizontally, vertically, both, or not at all.

 

Background-Repeat Options

 

1. No Repeat

If you don't want the background image to repeat, use the value no-repeat.

body {
    background-image: url('background.jpg');
    background-repeat: no-repeat;
}

 

2. Repeat Horizontally

To repeat the background image only horizontally, use the value repeat-x.

body {
    background-image: url('background.jpg');
    background-repeat: repeat-x;
}

 

3. Repeat Vertically

To repeat the background image only vertically, use the value repeat-y.

body {
    background-image: url('background.jpg');
    background-repeat: repeat-y;
}

 

4. Repeat Both Horizontally and Vertically

To repeat the background image both horizontally and vertically (default behavior), use the value repeat.

body {
    background-image: url('background.jpg');
    background-repeat: repeat;
}

 

5. Space 

  • Space: The background image is repeated as many times as possible without clipping. The remaining space is distributed around the images.
body {
    background-image: url('background.jpg');
    background-repeat: space;
}

 

6. Round

 The background image is repeated and rescaled to fit the container without clipping.

body {
    background-image: url('background.jpg');
    background-repeat: round;
}

 

Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Background Repeat Example</title>
    <style>
        body {
            font-family: Arial, sans-serif;
        }
        h2 {
            margin-top: 20px;
        }
        .repeat-example {
            width: 300px;
            height: 200px;
            background-image: url('background.png');
            border: 1px solid #000;
        }
    </style>
</head>
<body>
    <h2>Repeat</h2>
    <div class="repeat-example" style="background-repeat: repeat;"></div>
    <h2>Repeat-X</h2>
    <div class="repeat-example" style="background-repeat: repeat-x;"></div>
    <h2>Repeat-Y</h2>
    <div class="repeat-example" style="background-repeat: repeat-y;"></div>
    <h2>No Repeat</h2>
    <div class="repeat-example" style="background-repeat: no-repeat;"></div>
    <h2>Space</h2>
    <div class="repeat-example" style="background-repeat: space;"></div>
    <h2>Round</h2>
    <div class="repeat-example" style="background-repeat: round;"></div>
</body>
</html>