25
loading...
This website collects cookies to deliver better user experience
Find the demo site for the examples explained in this post here
font-size
.Let's use the image below to understand px better.
div.pixel p {
font-size: 20px;
width: 300px;
}
<p>
has a font-size: 20px
. Now look at this image below that shows the same paragraph in a device with smaller screen size.
font-size
remains 20px
even when the viewport size decreases. This is the issue with using pixels for any of the properties that takes a length. It is non-responsive, and does not scale according to the viewport size.
r
in rem stands for root.
<html>
.Let's understand rem with an example. Consider the code below.
div.rem p {
font-size: 2rem; /* 2 * 16 = 32px */
}
<html>
, it will have a font-size of 16px
.
<p>
, in the above example, would be 2 times the size of the root element. That is: 2 * 16 = 32px
.
Another example could be overriding the default 16px
of the root element.
html {
font-size: 20px;
}
div.rem p {
font-size: 0.5rem; /* 0.5 * 20 = 10px */
}
browser settings
appearance
, select Font size
Font size
of your browser, and look at the computed result for the tag who's unit length is defined using rem
NOTE: If you override the default font-size of root element, changing the font-size of the browser will have no effect on the font-size of the content defined.
Let's use the same paragraph example and follow the code below.
div.em {
font-size: 15px;
}
div.em p {
font-size: 2em; /* 2 * 15 = 30px */
}
<div>
is the parent of the <p>
, the font-size of the content within <p>
would be twice that of font-size defined for <div>
.`
30px would be the font-size of
`.
This is the HTML code
<div class="em">
<ul>
<li>1. Lorem</li>
<ul>
<li>1.1. Lorem</li>
<ul>
<li>1.1.1. Lorem</li>
</ul>
</ul>
</ul>
</div>
This is the CSS code
div.em {
font-size: 20px;
}
ul {
font-size: 2rem;
}
NOTE: If you override the default font-size of root element, changing the font-size of the browser will have no effect on the font-size of the content defined.
<p>
element within a <div>
, and the font-size of <div>
is 12px, <p>
is 2em
.<p>
as 2em. Then, the padding of<p>
would be 48px
and NOT 24px
.Using a similar scenario like we did for em, let's assume we have a <p>
within a <div>
.
html {
font-size: 20px;
}
div.percentage {
font-size: 150%;
}
div.percentage p {
font-size: 200%;
}
Let's see what the font-size of <p>
will be.
<p>
depends on <div>
, and the font-size of the <div>
depends on it's parent. (In this case: <html>
)<html>
is 20px, the font-size of <div>
should be 1.5 * 20 = 30px
.<p>
would be 30 * 2 = 60px
.Look at the image below to see the results of the above code:
Find the demo site for the examples explained in this post here
Moreover, it does not get exponentially multiplied (unlike em) when you define font-size for nested elements.
em can be used when you want the child element to inherit font-size and behave like the parent. Just beware of the inheritance.
As for %, it is very similar to em. But the main difference is that em scales faster than %.
Most people use px as well for font-size. But I do not recommend using it at all as it's not user friendly and not responsive.
rem > percentage > em > px