If anyone want to get the width and height of a div then he/she can do this with the help of jQuery width() or height() method.
The width() method will find the width of that element which we want to find. It do not take the padding or margin of the element.
The height() method will find the height of that element which we want to find. just like width() method it doesn't take the padding or margin of the element.
Below is the html code for height() method and width() method:-
<div id="div"></div>
<br>
<button>Get the height and width of the div</button>
Below is the jQuery code for height() method and width() method:-
$(document).ready(function(){
$("button").click(function(){
var txt = "";
txt += "Width of div: " + $("#div").width() + "</br>";
txt += "Height of div: " + $("#div").height();
$("#div").html(txt);
});
});
Below is the CSS for above HTML code:-
#div1 {
height: 200px;
width: 400px;
padding: 15px;
margin: 5px;
border: 2px solid #000;
background-color: grey;
color:#fff;
}
Working demo:- https://jsfiddle.net/0b3xsyco/1/
0 Comment(s)