Open In App

jQuery outerWidth() Method

Last Updated : 08 Apr, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The outerWidth() Method in jQuery is used to return the value of outer width of an element which includes border & padding.

Syntax:

$(selector).outerWidth( includemargin )

Parameters:

This method accepts single parameter includeMargin which is optional. It contains boolean value and used to specify whether the margin to be included or not. If includeMargin is set to true then margin included otherwise margin not included. By default the includeMargin set to false.

Example 1:

This example display the outer width including margin.

<!DOCTYPE html>
<html>
    
<head>
    <title>
        jQuery outerWidth() Method
    </title>
    
    <!-- Style to create box -->
    <style>
        .GFG {
            height: 200px;
            width: 350px;
            padding: 20px;
            margin: 3px;
            border: 3px solid green;
            background-color: lightgreen;
        }
    </style>
    
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>
    
    <!-- Script to display outerWidth of
        box including margin -->
    <script>
        $(document).ready(function() {
            $("button").click(function(){
                alert("Outer width of div: " 
                + $("div").outerWidth(true));
            });
        });
    </script>
</head>

<body>
    
    <button>outerwidth</button>
 
    <div class="GFG"></div>

</body>

</html>

Before Click on the button:

After Click on the button:


outerw1

Example 2:

This example display the outer width and does not include margin.

<!DOCTYPE html>
<html>

<head>
    <title>
        jQuery outerWidth() Method
    </title>
    
    <!-- Style to create box -->
    <style>
        .GFG {
            height: 200px;
            width: 350px;
            padding: 20px;
            margin: 3px;
            border: 3px solid green;
            background-color: lightgreen;
        }
    </style>
    
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>
    
    <!-- Script to display outer width excluding margin -->
    <script>
        $(document).ready(function() {
            $("button").click(function() {
                alert("Outer width of div: "
                + $("div").outerWidth());
            });
        });
    </script>
</head>

<body>
    <button>outerwidth</button>
 
    <div class = "GFG"></div>
 
</body>
</html>

Before Click on the button:

After Click on the button:


Next Article

Similar Reads

three90RightbarBannerImg