CSS Relative Font Size
In this article, we look at several different ways in which front-end web developers can manipulate the font fo their web page using CSS.
Join the DZone community and get the full member experience.
Join For FreeOne of the biggest confusions in web design is caused by none other than the font-size property.
Alongside with this property, confusion is commonly caused by CSS Columns.
If you are interested in layout properties, check out this article about the property that is used often - position.
In CSS, there are multiple units that can be used which can only cause the designer additional headache. In this article, we will clarify the usage of those units and any misconceptions.
PX Unit
The most common and popular unit is the pixel (px) unit. Most people start with using the pixel unit because it gives you full control over the text size. If the font size is not specified, the default size for normal text, like a paragraph, is 16px.
The main problem with using pixel units is that they are not scalable and making changes in the font size on different screen sizes can be challenging.
Also, the px unit is not related to anything. So if you want to change the size of, for example, the entire page based on the screen size, you would have to change the font size of each element individually.
EM Unit
The em unit is a scalable font size unit. It is related to the font size of the parent container. One em (1em) is equal to the current font size. So for example, if the parent element has the font size of 16px than 1em is equal to 16px, 2em is equal to 32px, and so on.
Making your design responsive becomes much easier if you use em units instead of px.
But, there is one thing that needs to be looked after. When using ems you should be careful with nesting.
For example, let's say you created a section and set its font size to 2ems. Now, you want to add a paragraph within the section that has a font size of 1em. The paragraph's font size is related to the font size of the section. If the nesting continues, or if it is used on multiple segments of the project, you can quickly lose sight of what is relative to what, and get completely lost.
Now, it is time to introduce the rem unit.
REM Unit
The rem unit is another scalable font size, but unlike the em unit, it is related to the root element (HTML) instead of the parent element. That's where it got its name (root em = rem).
This means that, if you use the rem unit on elements, you can quickly change the font size of the entire project, just by adjusting the root font size. This way is fast, easy, and avoids any nesting complications you might get while using the em unit.
So, Which Unit Should You Use?
The best way to answer this question is by using an example.
First, we will use the px unit.
html { font-size: 100% } //usually 16px
div { font-size: 16px; }
div>p { font-size: 14px; }
Here, you can see that the font size is set individually for each element, and they have no relation between each other.
Next, we'll use the em unit for the same code segment.
html { font-size: 100% }
div { font-size: 0.875em; } // 14px
div>p { font-size: 2em; } // 28px
In this example, the difference between the px and em units is clear. And, the relativeness of the em unit is clear. Just by changing the font size of the container div, we can see that the paragraph font size updated accordingly.
Finally, using the rem unit.
html { font-size: 100% }
div { font-size: 1rem; } // 16px
div>p { font-size: 1.5rem; } // 24px
When using the rem unit, it is clear that all font sizes are related to the root font size. Both the div and the paragraph font sizes are related to the root, despite the div being the parent of the paragraph.
Conclusion
There are no right or wrong units. It all depends on your skill level, project type, and size and personal preference.
If you would like to have complete control over the font size, the pixel unit is for you. If you would like a little bit more flexibility when manipulating the web page than the em or rem units are the best choices. Either way, the best result will be achieved when the units are fully understood.
Check out other detailed articles related to CSS properties such as this one: CSS Positions, SASS and LESS Nesting.
Published at DZone with permission of Tatjana Boskovic, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments