HTML DOM Style backgroundOrigin Property


The background-origin property is used to set or get the background origin i.e. its relative position. It can be relative to any of the four box model areas.

Syntax

Following is the syntax for −

Setting the backgroundOrigin property −

object.style.backgroundOrigin = "padding-box|border-box|content-box|initial|inherit"

Values

The above property values are explained as follows −

Sr.NoValue & Description
1padding-box
For having the background image positioned relative to the padding box.
It is the default value.
Default value. The background image is positioned relative to the padding box.
2border-box
For having the background image positioned relative to border box.
3content-box
To have background image positioned relative to content box.
4initial
For setting this property to initial value.
5inherit
To inherit the parent property value.

Example

Let us look at an example for the backgroundOrigin property −

<!DOCTYPE html>
<html>
<head>
<style>
   #DIV1 {
      margin: 20px;
      box-shadow: 0 0 5px black;
      padding: 30px;
      background: url("https://www.tutorialspoint.com/power_bi/images/power-bi-minilogo.jpg");
      background-origin: content-box;
      background-repeat: no-repeat;
      background-size: cover;
      font-size: 24px;
   }
   div>p {
      box-shadow: 0 0 5px darkgrey;
   }
</style>
<script>
   function changeBackOrigin(){
      document.getElementById("DIV1").style.backgroundOrigin="padding-box";
      document.getElementById("Sample").innerHTML="The background origin is now set to to padding-box";
   }
</script>
</head>
<body>
<h2>PowerBI Tutorial</h2>
<div id="DIV1">
<p>Learn PowerBI...Learn PowerBI...Learn PowerBI...Learn PowerBI...Learn
PowerBI...Learn PowerBI...Learn PowerBI...Learn PowerBI...Learn PowerBI...Learn
PowerBI...Learn PowerBI...Learn PowerBI... Learn PowerBI...Learn PowerBI...</p>
</div>
<p>Change the above div background origin by clicking the below button</p>
<button onclick="changeBackOrigin()">CHANGE ORIGIN</button>
<p id="Sample"></p>
</body>
</html>

Output

This will produce the following output −

On clicking the CHANGE ORIGIN button −

Updated on: 20-Aug-2019

22 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements