What is the difference between height and outerHeight in jQuery?

jQueryWeb DevelopmentFront End Technology

height in jQuery

The height() is the vertical measurement of the container, for example, height of a div. It excludes the padding border and margin.

To get the height of an element in jQuery, use the height() method in jQuery.

Example

You can try to run the following code to get the height:

Live Demo

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        alert("Height of div element: " + $("div").height());
    });
});
</script>
</head>
<body>

<div style="height:100px;width:350px;padding:10px;margin:10px;border:1px solid red; background-color:blue;"></div><br>
<button>Get Height of div</button>

</body>
</html>

outerHeight() in jQuery

The outerHeight() returns the outer height of the first matched element. It includes padding and border.

Example

You can try to run the following code to get the outer height in jQuery:

Live Demo

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        alert("Outer Height of div element: " + $("div").outerHeight());
    });
});
</script>
</head>
<body>

<div style="height:200px;width:450px;padding:10px;margin:10px;border:1px solid red; background-color:blue;"></div><br>
<button>Get Outer Height of div</button>

</body>
</html>
raja
Published on 17-Dec-2017 11:29:52
Advertisements