The font-size Property
Thefont-size
property can be set for any HTML tag (even if it would not normally contain textual content like br
). It can be assigned a variety of absolute, relative, or length size parameters.
An element will inherit the font-size of its parent unless you override it. This is especially important when you specify relative sizes.
Absolute Font Sizing Keywords
Several absolute font-sizing keywords are available. The font size is determined from a browser preset and the element will not inherit its parent’s size.- font-size: xx-small;
- font-size: x-small;
- font-size: small;
- font-size: medium;
- font-size: large;
- font-size: x-large;
- font-size: xx-large;
Relative Font Sizing Keywords
Two relative font-sizing keywords are available. The font is sized according to its parent element:- font-size: smaller;
- font-size: larger;
Absolute Lengths
Thefont-size
property can be assigned an absolute length:
- mm: millimeters, e.g. 10mm.
- cm: centimeters, e.g. 1cm ( = 10mm).
- in: inches, e.g. 0.39in ( ~= 10mm).
- pt: point, where 1pt is generally assumed to be 1/72 inch e.g. 12pt.
- pc: pica, where 1pc is 12pt
- px: pixel, e.g. 14px.
Relative Lengths
Thefont-size
property can be assigned a unit that it relative to its parent’s font size:
- em: 1em is equivalent to the current font size, so 2em is twice as large.
- %: 100% is equivalent to the current font size, so 200% is twice as large.
- ex: 1ex is equivalent to the height of the letter ‘x’ in the current font.
Text Sizing and Page Zooming
This is where additional complexity creeps in. Most browsers allow the user to:- increase or decrease the base text size (image dimensions are not changed)
- zoom the page in or out so all the text and graphics change accordingly, or
- allow both text sizing and page zooming.
CSS Font Sizing Recommendations
The general consensus is that ’em’ or ‘%’ is the best solution in most situations. Web fonts can be finely scaled relative to each other and browser text sizing is supported. I would also recommend using a percentagefont-size
on the body tag; it results in better text-sizing in some older browsers.
There are a couple of other recommendations I would suggest when you’re developing a site:
- reset the font size and page zoom to their default values in all your browsers before testing (it’s caught me out a few times!)
- try reasonable combinations of text sizing and page zooming in a variety of browsers to ensure the text remains readable.
Frequently Asked Questions (FAQs) on CSS Font Sizing
What is the difference between absolute and relative font sizes in CSS?
In CSS, font sizes can be set using absolute or relative values. Absolute values are fixed and do not change based on the size of the parent element. They are defined using units like pixels (px), points (pt), or centimeters (cm). On the other hand, relative values are dynamic and change based on the size of the parent element. They are defined using units like em, rem, or percentages (%). The choice between absolute and relative font sizes depends on the design requirements and responsiveness of the webpage.
How does the ’em’ unit work in CSS font sizing?
The ’em’ unit is a scalable unit in CSS used for font sizing. It is relative to the font size of its closest parent element. For instance, if the parent element has a font size of 20px, then “1em” would be equal to 20px for that element’s children. If no font size is defined, the default is usually 16px, so “1em” would be 16px.
How can I use the ‘rem’ unit for font sizing?
The ‘rem’ unit stands for “root em”. It is relative to the root element (html) rather than the parent element. This means that 1rem equals the font size of the root element. If the root element’s font size is 16px (the default size for most browsers), then 1rem would be equal to 16px.
What is the ‘vw’ unit in CSS font sizing?
The ‘vw’ unit stands for viewport width. It is relative to the width of the viewport, where 1vw is equal to 1% of the viewport’s width. This unit allows the font size to adjust based on the width of the screen, making it a great tool for responsive design.
How can I make my font size responsive using CSS?
To make your font size responsive, you can use relative units like em, rem, or vw. These units adjust the font size based on the size of the parent element, the root element, or the viewport width, respectively. This allows the font size to change dynamically based on the screen size or the size of the parent element.
How does the ‘calc()’ function work in CSS font sizing?
The ‘calc()’ function in CSS allows you to perform calculations to determine the font size. It can be used with different units, making it a powerful tool for creating responsive designs. For example, you can use ‘calc()’ to set a font size that is a mix of a fixed pixel value and a relative viewport value.
What is the impact of the ‘font-size-adjust’ property in CSS?
The ‘font-size-adjust’ property in CSS allows you to adjust the x-height (the height of lowercase letters) of a font. This is useful when you are using a fallback font, as it ensures that the x-height is preserved, keeping the readability consistent regardless of the font used.
How can I use the ‘ch’ unit in CSS font sizing?
The ‘ch’ unit in CSS is relative to the width of the “0” (zero) character of the font being used. This unit is useful when you want to set the width of elements with respect to the characters contained in them, such as setting the width of a button based on the length of the text inside it.
What is the ‘lh’ unit in CSS font sizing?
The ‘lh’ unit stands for “line height”. It is relative to the line height of the element. This unit is useful when you want to set the height of elements based on the line height, such as creating a vertical rhythm in your design.
How can I use the ‘ex’ unit in CSS font sizing?
The ‘ex’ unit in CSS is relative to the x-height of the current font. The x-height is typically the height of lowercase letters like ‘x’. This unit is useful when you want to set the height of elements based on the x-height, such as aligning elements vertically based on the text inside them.
Craig is a freelance UK web consultant who built his first page for IE2.0 in 1995. Since that time he's been advocating standards, accessibility, and best-practice HTML5 techniques. He's created enterprise specifications, websites and online applications for companies and organisations including the UK Parliament, the European Parliament, the Department of Energy & Climate Change, Microsoft, and more. He's written more than 1,000 articles for SitePoint and you can find him @craigbuckler.