Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Properties

CSS background-repeat Property

Posted in CSS Tutorial
Updated on Oct 01, 2024
By Mari Selvan
👁️ 24 - Views
⏳ 4 mins
💬 1 Comment
CSS background-repeat Property

Photo Credit to CodeToFun

🙋 Introduction

The background-repeat property in CSS is used to control how background images are repeated within an element. By default, a background image is repeated both horizontally and vertically, covering the entire area of the element. However, you can use the background-repeat property to change this behavior, specifying whether the image should repeat in one direction, both directions, or not at all.

💡 Syntax

The syntax for the background-repeat property is simple and can be applied to any element that supports background images.

Syntax
Copied
Copy To Clipboard
element {
  background-repeat: repeat | repeat-x | repeat-y | no-repeat | space | round;
}

🎛️ Default Value

The default value of the background-repeat property is repeat, which means the background image will repeat both horizontally and vertically.

🏠 Property Values

ValueDescription
repeatThe background image is repeated both horizontally and vertically (default).
repeat-xThe background image is repeated only horizontally.
repeat-yThe background image is repeated only vertically.
no-repeatThe background image is not repeated.
spaceThe background image is repeated as much as possible without clipping, and the remaining space is distributed around the images.
roundThe background image is repeated and rescaled to fill the space, ensuring that the image fits the area without clipping.

📄 Example

In this example, we'll demonstrate the different values of the background-repeat property.

index.html
Copied
Copy To Clipboard
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS background-repeat Example</title>
  <style>
    .box {
      width: 200px;
      height: 200px;
      background-image: url('your-image-url.jpg');
    }

    .repeat {
      background-repeat: repeat;
    }

    .repeat-x {
      background-repeat: repeat-x;
    }

    .repeat-y {
      background-repeat: repeat-y;
    }

    .no-repeat {
      background-repeat: no-repeat;
    }

    .space {
      background-repeat: space;
    }

    .round {
      background-repeat: round;
    }
  </style>
</head>
<body>
  <h1>CSS background-repeat Property Examples</h1>
  
  <h2>Repeat</h2>
  <div class="box repeat"></div>

  <h2>Repeat-X</h2>
  <div class="box repeat-x"></div>

  <h2>Repeat-Y</h2>
  <div class="box repeat-y"></div>

  <h2>No Repeat</h2>
  <div class="box no-repeat"></div>

  <h2>Space</h2>
  <div class="box space"></div>

  <h2>Round</h2>
  <div class="box round"></div>
</body>
</html>

🖥️ Browser Compatibility

The background-repeat property is widely supported across all modern browsers. This includes Chrome, Firefox, Safari, Edge, and Opera. It has been a standard feature in CSS for a long time, so you can confidently use it without worrying about browser compatibility issues.

🎉 Conclusion

The background-repeat property is a versatile tool in CSS that allows you to control how background images are repeated within an element. Whether you want to tile an image across the entire background, repeat it in just one direction, or not repeat it at all, this property gives you the flexibility to achieve the desired visual effect. Experiment with the different values and see how they can enhance the design of your web pages.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy