tags:

views:

1060

answers:

4

Is there a way to specify the font size for a class to be, say, 70% of the inherited font size?

I have a general "button" class that sets up my buttons with the appropriate borders, background, etc. I use it in multiple places, including one where the font size is fairly small and another where the font size is quite large.

<div style="font-size: 26px;">
Push this: <input class="button" type="submit" value="Go">
</div>
<div style="font-size: 10px;">
Push this too: <input class="button" type="submit" value="Go">
</div>

In both instances I'd like the button font-size to be about 70% of the font size of the span or div the button is in.

Can I do this with pure CSS?

+2  A: 

Try:

font-size: 0.7em;

Here's some more information: How to Size Text in CSS at A List Apart

Jason Anderson
I fail to see how 0.7em relates to the parent size?
Tom
@Tom: em is relative to the current font size.
Chris Jester-Young
em is always relative to the parent font size. But 0.7em does NOT give you 70% of it.
Treb
@Treb: [citation needed] (to steal a reference from Wikipedia)
R. Bemrose
+7  A: 

In CSS, if you give a relative unit, it is by default relative to the size you inherit from. That means, you could just define the font-size of the button as "70%".

There are also two other relative units handy for font sizes: em and ex. 1 em is the width of the letter 'M', 1 ex the height of the letter 'x' -- obviously also inherited.

You should not use px as font-size, as in your example. px doesn't obey the DPI of the screen. For example on my screen, both 10px and 26px would be small. You should use 'pt' instead, or even start with 'em'.

ypnos
To add to this - I've always found it best to set the font-size to 62.5% for the body element and then work in em's everywhere else. 62.5% works out at 10pt at the default browser font size (in most cases).
iAn
62.5% sounds very harsh to me. Do you have an example URL? I would be interested how it looks here. Will get screenshot in return.
ypnos
more specifically 1 em is the width of the _upper case_ letter 'M'
Henrik Paul
forgot that, thanks for pointing out!
ypnos
+1  A: 
<input style="font-size: 70%" class="button" type="submit" value="Go">

?

Will Kemp
Always avoid placing code on the page, try and keep it to stylesheets for maintainability and seperation
John_
+1  A: 

EMs do work like percentages in that the base font size is always 1em and .7em would be 70% of that (in the same way 1.2em would be equivalent of 120% of base font size). For this to work properly though you need to define a base font-size on the document body. Through experimentation I've found that font-size: 77%; gives you a nice base size in all browsers (that is it makes 1em render in a "normal" and readable size). You may want to try other values between 75% and 80% depending on what font-family you want to use. Also bear in mind that relative font sizes are inherited accumulatively - for example:

<style>
small { font-size: .8em; }
span.tiny { font-size: .8em } 
</style>

<small>This text is 80% of base size where as 
    <span class="tiny">this text is 80% of 80% (or 64%) of base size</span>
</small>

This works in your favour as you would only need to give your button class a font-size of .7em to acheive what you want (the buttons would then always have a font size that is 70% of its parent object). Happy coding!

Ola Tuvesson