Open In App

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: 



object.style.width
object.style.width = "auto|length|%|initial|inherit"

Property Value Description

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



Example 1: 




<!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>

Output:

Example 2: 




<!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>

Output:

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


Article Tags :