jQuery width() function example



jQuery width() function is used to get the current computed width for the first element in the set of matched elements or set the width of every matched element.
  • width()
  • width( value )

jQuery width() function

jQuery width() function is used to get the current computed width for the first element in the set of matched elements.

Syntax

width()
This method does not accept any arguments.
Example: This method is also able to find the width of the window and document.
// Returns width of browser viewport
$( window ).width();
 
// Returns width of HTML document
$( document ).width();

Example:

Show various widths. Note the values are from the iframe so it might be smaller than you expected. The yellow highlight shows the iframe body.
<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>width demo</title>
    <style>
        body {
            background: yellow;
        }

        button {
            font-size: 12px;
            margin: 2px;
        }

        p {
            width: 150px;
            border: 1px red solid;
        }

        div {
            color: red;
            font-weight: bold;
        }
    </style>
    <script src="https://code.jquery.com/jquery-3.4.1.js"></script>
</head>

<body>

    <button id="getp">Get Paragraph Width</button>
    <button id="getd">Get Document Width</button>
    <button id="getw">Get Window Width</button>
    <div>&nbsp;</div>
    <p>
        Sample paragraph to test width
    </p>

    <script>
        function showWidth(ele, w) {
            $("div").text("The width for the " + ele + " is " + w + "px.");
        }
        $("#getp").click(function() {
            showWidth("paragraph", $("p").width());
        });
        $("#getd").click(function() {
            showWidth("document", $(document).width());
        });
        $("#getw").click(function() {
            showWidth("window", $(window).width());
        });
    </script>

</body>

</html>
Try it Yourself - Copy paste code in Online HTML Editor to see the result

jQuery width( value ) function

jQuery width( value ) function is used to set the CSS width of each element in the set of matched elements.

Syntax

width( value )

value - An integer representing the number of pixels, or an integer along with an optional unit of measure appended (as a string).

Example

Change the width of each div the first time it is clicked (and change its color).
<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>width demo</title>
    <style>
        div {
            width: 70px;
            height: 50px;
            float: left;
            margin: 5px;
            background: red;
            cursor: pointer;
        }

        .mod {
            background: blue;
            cursor: default;
        }
    </style>
    <script src="https://code.jquery.com/jquery-3.4.1.js"></script>
</head>

<body>

    <div>d</div>
    <div>d</div>
    <div>d</div>
    <div>d</div>
    <div>d</div>

    <script>
        var modWidth = 50;
        $("div").one("click", function() {
            $(this).width(modWidth).addClass("mod");
            modWidth -= 8;
        });
    </script>

</body>

</html>
Try it Yourself - Copy paste code in Online HTML Editor to see the result

Comments