To get the padding of an element using jQuery we can simply use the jQuery css() method to retrieve the padding property of the element.

$("#div1").css('padding');

Let’s see a simple example of this below.


Let’s say we have the following HTML:



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

var thePadding = $("#div1").css('padding');

console.log(thePadding);

#Output
40px

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

var thePadding = jQuery("#div1").css('padding');

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

Get Different Padding Values of an Element Using jQuery

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



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

var thePadding = $("#div1").css('padding');

console.log(thePadding);

#Output
40px 30px 20px 10px

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

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

var paddingTop = $("#div1").css('padding-top');
var paddingBottom = $("#div1").css('padding-bottom');
var paddingLeft = $("#div1").css('padding-left');
var paddingRight = $("#div1").css('padding-right');

console.log(paddingTop);
console.log(paddingBottom);
console.log(paddingLeft);
console.log(paddingRight);

#Output
40px
20px
10px
30px

Hopefully this article has helped you understand how to get padding using jQuery.

Categorized in:

jQuery,

Last Update: March 11, 2024