• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / jQuery / Get Padding of an Element Using jQuery

Get Padding of an Element Using jQuery

July 11, 2022 Leave a Comment

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:


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

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:


<style>#div1 { padding: 40px 30px 20px 10px; margin: 40px; border: 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 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.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Scroll to Div
  • 2.  Using jQuery to Remove All Options From Select
  • 3.  Using jQuery to Get the Margin of an Element
  • 4.  Changing the Background Image of a div Using jQuery
  • 5.  Using jQuery to Change Text of HTML Element
  • 6.  Resizing an Image Using jQuery
  • 7.  jQuery hover – An Interactive Example of the hover Method
  • 8.  Input Mask Phone Number Using jQuery
  • 9.  Using jQuery to Set the Width of a Div
  • 10.  Using jQuery to Swap an Image on Hover

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 *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

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