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?
When you set font-size on an element, the element certainly does not inherit font size. This is about the effect of the relative nature of the em unit.
Percent (%) as CSS font size Percents are also scalable like ems. However, 100% is equal to the current font size. Think of it this way: 1.5em is 1.5 times larger, and 150% is 150 percent of the font size. Percents are also good for mobile development because of their scalability.
Using CSS Reset, or specifically font: inherit means that on browsers supporting the inherit value, all such elements are rendered in copy text font, unless otherwise specified in a style sheet. So this is about a particular methodology (or, as some people might say, ideology or religion) of authoring and design.
For example, suppose the font-size of the <body> of the page is set to 16px . If the font-size you want is 12px , then you should specify 0.75em (because 12/16 = 0.75). Similarly, if you want a font size of 10px , then specify 0.625em (10/16 = 0.625); for 22px , specify 1.375em (22/16).
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 achieve what you want (the buttons would then always have a font size that is 70% of its parent object). Happy coding!
2014 edit:
It's worth pointing out that browser support for the Root EM unit is now so good* that if you're not already using it it is worth looking into. The Root EM (rem) is tied to the root (document) font size, and unlike the "normal" EM it is unaffected by nesting - it always relates to the root font size. While em
's are still very useful for most text sizing, precisely because it does respect nesting, the rem
is great for things like margins and padding, which you may not want to change size with nesting (this is a common cause for misaligned left margins), but which you do want to change size along with the root html
font size (typically using media queries).
You can read more about EMs vs. REMs over on Design Shack.
*) IE8 is the only common browser (~5%) which does not support it - if you need to support IE8, simply include an equivalent size in pixels before the rem
declaration.
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'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With