views:

37

answers:

4

Hello,

I've got a blog with lots of div.contents, each of which I'd like to have a height as a multiple of 22px (so all the text lines up with a background image of a grid). I'm imagining you'd probably do something like:

    // loop for each div.content   
    // var height =  $('div.content').height()
    // var modulus = height%22
    // var padding = 22 - modulus
    // $('div.content').css({'padding-left': 'PADDINGpx'})

Does that sound about right? I'm not too great with JS. How do you get the padding variable into the jquery function?

Thanks in advance!

+1  A: 

It looks fine to me. You need to use string concatenation to join the variable with the string:

$('div.content').css({'padding-left': padding+'px'});

One point to make is that if the div is a multiple of 22, you'll still be adding 22px to it. If that's not desired, use an if statement to conditionally add the padding:

// loop for each div.content   
var height =  $('div.content').height();
var modulus = height%22;
var padding = 22 - modulus;

if (modulus)
    $('div.content').css({'padding-left': padding+ 'px'});

ps, don't forget your semicolon line terminators.

Andy E
Ace, that works perfectly. Thanks!
Rik
A: 

try $('div.content').css({'padding-left': padding+'px'})

calin014
A: 

You would simply just do the following to get the padding in

var_padding = 22 - modulus;
$('div.content').css({'padding-left': var_padding})
danrichardson
A: 

I think giving it a line-height:22px should do the trick automatically..

(maybe i am understanding something wrong, though ..)

Gaby
If there is a picture or a video in a post then all the nicely set up line heights get thrown out.
Rik
@rik, indeed. I did not take into consideration other elements that affect the sizing .. scrap my answer :)
Gaby