cmac cmac - 4 months ago 13
CSS Question

Does jQuery .css() method add the 'px' for you on top, right, bottom, left properties if you give it an integer?

Does jQuery add the +'px' for me?

$('#id').css({
position: absolute;
top: 23,
right: 24,
left: 25,
bottom: 26
});


So I don't have to do this:

$('#id').css({
position: absolute;
top: 23 +'px',
right: 24 +'px',
left: 25 +'px',
bottom: 26 +'px'
});


It seems to work the first way, but if I try to do it like that in css file:

.example{
top:55
}


It doesn't work. I just want to be sure jquery is adding it and it's not some browser indifference or something.

Answer

Yes, it does. jQuery assumes, and adds, .px if no unit is given.

As you've discovered, this does not happen in native CSS, where a unit is required for non-zero numerical property values.

Comments