2

How could I detect if an element has padding applied to it?

So something like:

if($('div.MiddleColumn') has padding of 5px) {
do something
}
else
{
do something else
}

5 Answers 5

1

This will return the total horizontal padding of your element :

var hz_padding = $("el").innerWidth() - $("el").width();

.innerWidth() returns the same value as .width(), plus the padding. So hz_padding will be > 0 if $("el") has any padding.

The interesting thing here is that you will have the computed styles, which might be different from those in the stylesheet.

More on .innerWidth() and .width().

Use .outerWidth() if you want to include borders, and .outerWidth(true) to include margin.

And there are obviousy the same methods for the height.

Sign up to request clarification or add additional context in comments.

Comments

0
var hasPadding = false,
    element = $('div.MiddleColumn');

$.each(['top', 'right', 'bottom', 'left'], function(i, side) {
   if (parseInt(element.css('padding-' + side))) {
       hasPadding = true;
       return false;
   }
}

Comments

0

I would try the following:

if($('div.MiddleColumn').css('padding') == "5px") {

Comments

0

Use the .css method

if ( $('div.MiddleColumn').css('padding-left') === '5px' ) {
   do something
}
else {
   do something else
}

Comments

0

Try this :

if($('div.MiddleColumn').css("padding") == "5px")
{
    /*
      do something
    */
}
else 
{
    /*
      do something else
    */
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.