jQuery: how to get an element's margin and padding 

Joined:
02/21/2009
Posts:
172

August 08, 2014 12:17:50    Last update: August 08, 2014 12:17:50
Simple answer: use $(elem).css('margin-top'), $(elem).css('padding-left'), etc., don't use $(elem).css('margin') or $(elem).css('padding'), they return empty strings.

Example:
// set margin to 8px
$('#test-div').css('margin', '8px')

// try to get margin
$('#test-div').css('margin') // returns ""
$('#test-div').css('margin-top') // returns "8px"

// set padding to 10px
$('#test-div').css('padding', '10px')

// get padding
$('#test-div').css('padding') // returns ""
$('#test-div').css('padding-left') // returns "10px"
Share |
| Comment  | Tags