We can use jQuery to get the border width of an element simply by using the jQuery css() method to retrieve the border-width property of the element.

$("#div1").css('border-width');

Let’s see a simple example of this below.


Let’s say we have the following HTML:



If we want to get the border width of #div1, we will use the jQuery css() method and retrieve the border width in the following jQuery code.

var theBorderWidth = $("#div1").css('border-width');

console.log(theBorderWidth);

#Output
2px

If you are using WordPress, don’t forget to change the $ to jQuery as below:

var theBorderWidth = jQuery("#div1").css('border-width');

Note that we can also get the padding and get the margin of an element using the css() method.

Get Different Border Values of an Element Using jQuery

Let’s say in our example above, that our div has different border values for each side. Here is our HTML for that:



We can still use our same jQuery code to get the border width of the element with different values for each side.

var theBorderWidth = $("#div1").css('border-width');

console.log(theBorderWidth);

#Output
4px 1px 3px 2px

We can also get the individual border width values for each side using jQuery. We just have to change the value in our css() method.

Here is how to get the border width for each side using jQuery:

var borderWidthTop = $("#div1").css('border-top-width');
var borderWidthBottom = $("#div1").css('border-bottom-width');
var borderWidthLeft = $("#div1").css('border-left-width');
var borderWidthRight = $("#div1").css('border-right-width');

console.log(borderWidthTop);
console.log(borderWidthBottom);
console.log(borderWidthLeft);
console.log(borderWidthRight);

#Output
4px
3px
2px
1px

Hopefully this article has helped you understand how to use jQuery to get the border width of an element.

Categorized in:

jQuery,

Last Update: March 11, 2024