CSS background-origin Property

You are Here:

CSS background-origin Property

CSS background-origin property sets the origin position of a background image.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> div{ border: 10px double #8c8c8c; padding: 25px; height: 240px; width: 240px; margin-bottom: 15px; background: url(husky.jpg); background-repeat: no-repeat; } #point{ background-origin: border-box; } #point1{ background-origin: padding-box; } #point2{ background-origin: content-box; } </style> </head> <body> <h1>CSS background-origin Property</h1> <h2>background-origin: border-box</h2> <div id="point"></div> <h2>background-origin: padding-box</h2> <div id="point1"></div> <h2>background-origin: content-box</h2> <div id="point2"></div> </body> </html>

Syntax

Using CSS

element{ background-origin: padding-box; }

Using Javascript

object.style.backgroundOrigin="padding-box";

Animatable

No, background-origin property is not animatable. CSS Animatable Properties Reference.

Default Value

Default value for CSS background-origin property is padding-box.

Property Value

The following table provides a list of values for CSS background-origin property.

ValueExplanation
border-boxSpecifies the background image starts from the upper left corner of the border.
padding-boxSpecifies the background image starts from the upper left corner of the padding edge.
content-boxSpecifies the background image starts from the upper left corner of the content.

Using JavaScript

In the following example, we will demonstrate how to change the CSS background-origin property of an element using JavaScript.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> #point{ border: 10px double #8c8c8c; padding: 25px; height: 240px; width: 240px; margin-bottom: 15px; background: url(husky.jpg); background-repeat: no-repeat; background-origin: border-box; } </style> </head> <body> <h1>CSS background-origin Property</h1> <div id="point"> </div> <button onclick="myFunction()">Click Me</button> <script> var x = document.getElementById("point"); function myFunction(){ x.style.backgroundOrigin = "padding-box"; } </script> </body> </html>

Reminder

Hi Developers, we almost covered 98.7% of CSS Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in CSS.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author