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.
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;
}
To repeat the background image only horizontally, use the value repeat-x
.
body {
background-image: url('background.jpg');
background-repeat: repeat-x;
}
To repeat the background image only vertically, use the value repeat-y
.
body {
background-image: url('background.jpg');
background-repeat: repeat-y;
}
To repeat the background image both horizontally and vertically (default behavior), use the value repeat
.
body {
background-image: url('background.jpg');
background-repeat: repeat;
}
body {
background-image: url('background.jpg');
background-repeat: space;
}
The background image is repeated and rescaled to fit the container without clipping.
body {
background-image: url('background.jpg');
background-repeat: round;
}
<!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>