Open In App

HTML | DOM Style backgroundPosition Property

The HTML DOM Style backgroundPosition : It sets or returns position of the background-image in an element. 

Syntax:



object.style.backgroundPosition
object.style.backgroundPosition = value

Return Values: It returns a string value, that represents the position of a background-image.

Property Values:



The values are explained using the following examples: 

Example-1: Using keyword values. We use the value ‘bottom right’ in this example. 




<!DOCTYPE html>
<html lang="en">
 
<head>
    <title>
      DOM Style backgroundPosition Property
    </title>
    <style>
        .bg-img {
            height: 300px;
            width: 300px;
            border-style: solid;
            background:
                        no-repeat center;
        }
    </style>
</head>
 
<body>
    <h1 style="color: green">
      GeeksforGeeks
    </h1>
    <b>
      DOM Style backgroundPosition Property
    </b>
    <p>
      Click on the button to change the
      position of the background image
    </p>
    <div class="bg-img">
    </div>
 
    <button onclick="changePos()">
        Change position of background image
    </button>
 
    <script>
        function changePos() {
            elem = document.querySelector('.bg-img');
 
            // Setting the position to bottom vertically
            // and right horizontally
            elem.style.backgroundPosition = 'bottom right';
        }
    </script>
</body>
 
</html>

Output:

 

Example-2: Using percentage to specify the position. We use ‘25% 75%’ to position the image. 




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <title>
      DOM Style backgroundPosition Property
    </title>
    <style>
        .bg-img {
            height: 300px;
            width: 300px;
            border-style: solid;
            background:
                        no-repeat center;
        }
    </style>
</head>
 
<body>
    <h1 style="color: green">
      GeeksforGeeks
    </h1>
    <b>
      DOM Style backgroundPosition Property
    </b>
    <p>
      Click on the button to change the
      position of the background image
    </p>
 
    <div class="bg-img">
    </div>
    <button onclick="changePos()">
        Change position of background image
    </button>
 
    <script>
        function changePos() {
            elem = document.querySelector('.bg-img');
 
            // Setting the position to 25% horizontally
            //and 75% vertically
            elem.style.backgroundPosition = '25% 75%';
        }
    </script>
</body>
 
</html>

Output:

 

Example-3: Using fixed units to specify the position. We use ’50px 25px’ to position the image. 




<!DOCTYPE html>
<html lang="en">
 
<head>
    <title>
      DOM Style backgroundPosition Property
    </title>
    <style>
        .bg-img {
            height: 300px;
            width: 300px;
            border-style: solid;
            background:
                        no-repeat center;
        }
    </style>
</head>
 
<body>
    <h1 style="color: green">
      GeeksforGeeks
    </h1>
    <b>
      DOM Style backgroundPosition Property
    </b>
    <p>
      Click on the button to change the
      position of the background image</p>
    <div class="bg-img">
    </div>
 
    <button onclick="changePos()">
        Change position of background image
    </button>
 
    <script>
        function changePos() {
            elem = document.querySelector('.bg-img');
 
            // Setting the position to 50px horizontally
           //and 25px horizontally
            elem.style.backgroundPosition = '50px 25px';
        }
    </script>
</body>
 
</html>

Output:

 

Example-4: Using the initial value. This sets the position to its default value. 




<!DOCTYPE html>
<html lang="en">
 
<head>
    <title>DOM Style backgroundPosition Property</title>
    <style>
        .bg-img {
            height: 300px;
            width: 300px;
            border-style: solid;
            background:
                        no-repeat center;
        }
    </style>
</head>
 
<body>
    <h1 style="color: green">
      GeeksforGeeks
    </h1>
    <b>
      DOM Style backgroundPosition Property
    </b>
    <p>
      Click on the button to change the
      position of the background image
    </p>
    <div class="bg-img">
    </div>
 
    <button onclick="changePos()">
        Change position of background image
    </button>
 
    <script>
        function changePos() {
            elem = document.querySelector('.bg-img');
 
            // Setting the position to the default
            // value with initial
            elem.style.backgroundPosition = 'initial';
        }
    </script>
</body>
 
</html>

Output:

 

 

Example-5: Using the inherit value. This inherits the position from its parent element. 




<!DOCTYPE html>
<html lang="en">
 
<head>
    <title>
      DOM Style backgroundPosition Property
    </title>
    <style>
        /* Parent element */
         
        #parent {
            height: 300px;
            width: 300px;
            border-style: solid;
            /* Setting the parent's background-position
          //to center left*/
            background-position: center left;
        }
         
        .bg-img {
            height: 300px;
            width: 300px;
            background:
                        no-repeat center;
        }
    </style>
</head>
 
<body>
    <h1 style="color: green">
      GeeksforGeeks
    </h1>
    <b>
      DOM Style backgroundPosition Property
    </b>
    <p>
      Click on the button to change the
      position of the background image
    </p>
    <div id="parent">
        <div class="bg-img"></div>
    </div>
 
    <button onclick="changePos()">
        Change position of background image
    </button>
 
    <script>
        function changePos() {
            elem = document.querySelector('.bg-img');
 
            // Setting the position to inherit from its parent
            elem.style.backgroundPosition = 'inherit';
        }
    </script>
</body>
 
</html>

Output:

 

Supported Browsers: The browser supported by backgroundPosition Property are listed below:


Article Tags :