tags:

views:

132

answers:

2

Hi,

I want to use relative size instead of fixed size. I want to use em.

My CSS is:

body{
     font:10px;
}

#wrap {
   font:1.2em;
}

#wrap ul li {
   padding-left:2em;
}

What is value of the li's padding in px? I would have guessed it's 2.0*10 = 20px, but it looks like it's taking 1em = 12px, I mean it taking it parent size.

I would like for it to take 1x the parent fontsize in px (that means font-size of body not wrap).

I appreciate your suggestions.

+1  A: 

ems are always relative to their parent element, as you stated. Additionally, an element's non-font dimensions are always calculated relative to its font dimensions - i.e. if you say font-size:1.2em;padding:2em - that element's padding will be 2x the font size which is 1.2x the size of its next defined parent element.

The solution is to use more math. If your wrapper element is 1.2em, and you want an inner element padding to be double the baseline size, you should use t=i*x, where t is target size, i is inherited size, and x is the desired size. So for our case, 2em is the target size; 1.2em is the inherited size:

2em = 1.2em * x => x = 1.67

So padding should be 1.67em.

Rex M
and this is why I've stopped using `em`'s...
peirix
A: 

the font size of #wrap ul li is 10px*1.2em*2em, thus padding-left:24px;

it always takes the font size of the parent element (in your case #wrap)

knittl
Thanks for your reply, is em calculated parent font size or parent that has font size in px. Because some where i read 1em = 1/parent font-size in PX.
vinay
1em is the width of the letter “M”
knittl
That's correct, but width of that 'M' depends up on font size. Then 1em = 1/parent font-size in PX
vinay
why 1/font-size oO? 1em = width of letter M in font-size of parent element
knittl
ya that's only my question. Is 1em is width of letter M in font-size of parent element OR is it width of letter M in font-size of parent element/grandparent (in case font-size is not defined in PX) which has font-size in PX.
vinay
em is derived from the first element with a set size (by default, body gets a size set from the browser). then it calculates all font sizes from body down to your element. if you don’t specify a font-size it is the same (=1em) as the parent element
knittl