Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Properties

CSS background-origin Property

Posted in CSS Tutorial
Updated on Oct 01, 2024
By Mari Selvan
πŸ‘οΈ 17 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
CSS background-origin Property

Photo Credit to CodeToFun

πŸ™‹ Introduction

The background-origin property in CSS defines the positioning area of the background image. It specifies where the background image should be positioned relative to the element’s border, padding, or content box.

This property is particularly useful when you need precise control over how your background image is displayed within an element.

πŸ’‘ Syntax

The syntax for the background-origin property is straightforward and allows you to choose from three possible values.

Syntax
Copied
Copy To Clipboard
element {
  background-origin: value;
}

πŸŽ›οΈ Default Value

The default value of the background-origin property is padding-box. This means the background image starts from the upper-left corner of the padding area of the element.

🏠 Property Values

ValueDescription
border-boxThe background image starts from the upper-left corner of the border box, which includes the padding and border.
padding-boxThe background image starts from the upper-left corner of the padding box, which includes only the padding area (default).
content-boxThe background image starts from the upper-left corner of the content box, which excludes padding and border.

πŸ“„ Example

In this example, we'll apply the background-origin property to an element with a background image and see how it affects the image positioning with different values.

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-origin Example</title>
  <style>
    .box {
      width: 200px;
      height: 200px;
      border: 20px solid #333;
      padding: 20px;
      background-image: url('https://via.placeholder.com/200');
      background-origin: border-box; /* Try padding-box or content-box */
      background-repeat: no-repeat;
      background-size: cover;
    }
  </style>
</head>
<body>
  <h1>Background Origin Example</h1>
  <div class="box"></div>
</body>
</html>

πŸ–₯️ Browser Compatibility

The background-origin property is well-supported across all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. It's a reliable property for controlling the positioning of background images within an element.

πŸŽ‰ Conclusion

The background-origin property provides developers with fine-grained control over the positioning of background images.

Whether you want your background image to be aligned with the border, padding, or content of an element, this property gives you the flexibility to achieve your desired layout. Experimenting with the different values of background-origin can help you better understand how it influences the appearance of your background images.

πŸ‘¨β€πŸ’» 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