HTML | DOM Style width Property

The Style width property in HTML DOM is used to set or return the width of an element which can be block-level elements or elements with fixed position.

Syntax:

  • It is used to return the width property:
    object.style.width
  • It is used to set the width property:
    object.style.width = "auto|length|%|initial|inherit"

Property Value Description



  • auto-This value automatically sets the default width value by the browser.
  • length-This value sets the width value in the specified length units.
  • %-Percentage value sets the width value in the specified percentage of the parent element’s width.
  • initial-This value sets the width property to its browser’s default value.
  • inherit-This value sets the width property to the value from its parent element.

Return Value: A String, representing the width of an element.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <style>
        #GfG {
            width: 100px;
            height: 100px;
            background-color: green;
            color: white;
        }
    </style>
</head>
  
<body>
  
    <p>HTML|DOM Style Width Property:</p>
  
    <button onclick="myFunction()">Click here!</button>
  
    <div id="GfG">
        <h1>GfG</h1>
    </div>
  
    <script>
        function myFunction() {
            document.getElementById("GfG")
            .style.width = "500px";
        }
    </script>
  
</body>
  
</html>

chevron_right


Output:

  • Before click on the button:

  • After click on the button:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
  
    <h1>GeeksforGeeks</h1>
  
    <h2>HTML|DOM Style Width Property:</h2>
  
    <button type="button" id="myBtn" 
            onclick="myFunction()">Click Here!</button>
  
    <script>
        function myFunction() {
            document.getElementById("myBtn")
            .style.width = "500px";
        }
    </script>
  
</body>
  
</html>

chevron_right


Output:

  • Before click on the button:

  • After click on the button:

Supported Browsers: The browser supported by DOM style width property are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Safari
  • Opera


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 :

Be the First to upvote.


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