• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / jQuery / Using jQuery to Get the Border Width of an Element

Using jQuery to Get the Border Width of an Element

July 11, 2022 Leave a Comment

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:


<style>#div1 { padding: 40px; margin: 40px; border: 2px solid #000; background: #7bbfa2; width: 100px; height: 100px; }</style>
<div id="div1"></div>

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:


<style>#div1 { margin: 40px; padding: 40px; border-top: 4px solid #000; border-bottom: 3px solid #000; border-left: 2px solid #000; border-right: 1px solid #000; background: #7bbfa2; width: 100px; height: 100px; }</style>
<div id="div1"></div>

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.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Change Background Color
  • 2.  jQuery focusout – How to Use the focusout() Method on an HTML Form
  • 3.  jQuery keydown Method
  • 4.  jQuery Get Last Child
  • 5.  jQuery hover – An Interactive Example of the hover Method
  • 6.  Using jQuery to Set Select to the First Option
  • 7.  Set the Height of a Div Using jQuery
  • 8.  Using jQuery to Remove Background Color
  • 9.  Using jQuery to Check if Checkbox is Checked
  • 10.  Using jQuery to Set Visibility

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · The Programming Expert · About · Privacy Policy