Get width of a div container with JavaScript/jQuery
This post will discuss how to get the width of a div container in JavaScript and jQuery.
1. Using JavaScript
In pure JavaScript, you can use the clientWidth property to get the width of the div container. It returns the actual space used by the displayed content, including its horizontal padding. For example, the following code returns 410 value.
JS
|
1 2 |
var clientWidth = document.getElementById('container').clientWidth; alert(clientWidth); |
HTML
|
1 |
<div id="container" class="main"></div> |
CSS
|
1 2 3 4 5 6 |
.main { border: 1px solid lightblue; width: 400px; height: 100px; padding: 5px; } |
Alternatively, you can use the offsetWidth property if you need the total amount of space the div container occupies. It returns the width of the div container, including the padding, borders, and scrollbars. For example, the following code returns 412 value.
JS
|
1 2 |
var offsetWidth = document.getElementById('container').offsetWidth; alert(offsetWidth); |
HTML
|
1 |
<div id="container" class="main"></div> |
CSS
|
1 2 3 4 5 6 |
.main { border: 1px solid lightblue; width: 400px; height: 100px; padding: 5px; } |
When the container’s width is set by the style attribute, you can use the width property to get width with units intact.
JS
|
1 2 |
var width = document.getElementById('container').style.width; alert(width); |
HTML
|
1 2 |
<div id="container" style="border: 1px solid lightblue; height: 100px; width: 400px;"> </div> |
Another plausible way is to use the getBoundingClientRect() method, which returns the size of the div container. The returned object contains the width property, whose value includes padding and border. For example, the following code returns 412 value.
JS
|
1 2 |
var width = document.getElementById('container').getBoundingClientRect().width; alert(width); |
HTML
|
1 |
<div id="container" class="main"></div> |
CSS
|
1 2 3 4 5 6 |
.main { border: 1px solid lightblue; width: 400px; height: 100px; padding: 5px; } |
2. Using jQuery
With jQuery, you can use the .width() method to get the div container’s content width. For example, the following code returns 100.
JS
|
1 2 3 4 |
$(document).ready(function() { var width = $("#container").width(); alert(width); }); |
HTML
|
1 |
<div id="container" class="main"></div> |
CSS
|
1 2 3 4 5 6 |
.main { border: 1px solid lightblue; width: 400px; height: 100px; padding: 5px; } |
The .width() method returns a unit-less value. If you need width with units intact like 400px, use .css("width") instead.
JS
|
1 2 3 4 |
$(document).ready(function() { var width = $("#container").css("width"); alert(width); }); |
HTML
|
1 |
<div id="container" class="main"></div> |
CSS
|
1 2 3 4 5 6 |
.main { border: 1px solid lightblue; width: 400px; height: 100px; padding: 5px; } |
Alternatively, if you need to include padding and borders (and optionally the margin), consider using the .outerWidth() method. For example, the following code returns 412.
JS
|
1 2 3 4 |
$(document).ready(function() { var width = $("#container").outerWidth(); alert(width); }); |
HTML
|
1 |
<div id="container" class="main-class"></div> |
CSS
|
1 2 3 4 5 6 |
.main-class { border: 1px solid gray; width: 400px; height: 100px; padding: 5px; } |
That’s all about getting the width of a div container in JavaScript and jQuery.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)