HTML | DOM Style objectPosition Property

The DOM Style objectPosition property is used to set or return how an image or video would be positioned in their own content box.

Syntax:

  • It returns the objectPosition property.
    object.style.objectPosition
  • It is used to set the objectPosition property.
    object.style.objectPosition = "position|initial|inherit"

Property Values:

  • position: This is used to specify the position of the image or video in terms of either length values or strings (left, right and center).

    Example-1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <head>
        <title>
            DOM Style objectPosition Property
        </title>
        <style>
            .content {
                border: 1px solid;
                object-fit: cover;
                height: 250px;
                width: 500px;
            }
        </style>
    </head>
      
    <body>
        <h1 style="color: green">
          GeeksforGeeks
      </h1>
        <b>
          DOM Style objectPosition Property
      </b>
        <p>
            The objectPosition property 
          specify how an image or video 
          should be positioned in its content box.
        </p>
        <img src=
             height="800" 
             width="800" 
             class="content" />
        
        <button onclick="setObjectPosition()">
            Change resize
        </button>
      
        <!-- Script to set objectPosition to 50% 100% -->
        <script>
            function setObjectPosition() {
                elem = document.querySelector('.content');
                elem.style.objectPosition = '75% 100%';
            }
        </script>
    </body>
      
    </html>

    chevron_right

    
    

    Output:

    Before clicking the button:

    position-before

    After clicking the button:

    position-after

  • initial: This is used to set this property to its default value.

    Example-2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <head>
        <title>
            DOM Style objectPosition Property
        </title>
        <style>
            .content {
                border: 1px solid;
                object-fit: cover;
                height: 250px;
                width: 500px;
                object-position: 50% 100%;
            }
        </style>
    </head>
      
    <body>
        <h1 style="color: green">
          GeeksforGeeks
      </h1>
        <b>
          DOM Style objectPosition Property
      </b>
        <p>
            The objectPosition property specify 
          how an image or video should be
          positioned in its content box.
        </p>
        <img src=
             height="800"
             width="800"
             class="content" />
      
        <button onclick="setObjectPosition()">
            Change resize
        </button>
      
        <!-- Script to set objectPosition to initial -->
        <script>
            function setObjectPosition() {
                elem = document.querySelector('.content');
                elem.style.objectPosition = 'initial';
            }
        </script>
    </body>
      
    </html>

    chevron_right

    
    

    Output:

    Before clicking the button:

    initial-before

    After clicking the button:

    initial-after

  • inherit: This inherits the property from its parent.

    Example-3:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <head>
        <title>
            DOM Style objectPosition Property
        </title>
        <style>
            #parent {
                object-position: 50% 100%;
            }
              
            .content {
                border: 1px solid;
                object-fit: cover;
                height: 250px;
                width: 500px;
            }
        </style>
    </head>
      
    <body>
        <h1 style="color: green">
          GeeksforGeeks
      </h1>
        <b>
          DOM Style objectPosition Property
      </b>
        <p>
            The objectPosition property specify how an 
          image or video should be positioned in its content box.
        </p>
        <div id="parent">
            <img src=
                 height="800"
                 width="800" 
                 class="content" />
        </div>
      
        <button onclick="setObjectPosition()">
            Change resize
        </button>
      
        <!-- Script to set objectPosition to inherit -->
        <script>
            function setObjectPosition() {
                elem = document.querySelector('.content');
                elem.style.objectPosition = 'inherit';
            }
        </script>
    </body>
      
    </html>

    chevron_right

    
    

    Output:

    Before clicking the button:

    inherit-before

    After clicking the button:

    inherit-after

Supported Browsers: The browser supported by objectPosition property are listed below:

  • Google Chrome 31.0
  • Internet Explorer 16.0
  • Firefox 36.0
  • Apple Safari 10.1
  • Opera 19.0


My Personal Notes arrow_drop_up

Technical Writer Lets have an update

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.