The font-size
property in CSS controls the size of the text. There are various units you can use to specify the size, including pixels (px
), em units (em
), rem units (rem
), and percentages (%
). Choosing the right unit is important for creating scalable and accessible web designs.
Example 1: Using Pixels
/* This rule targets all paragraph elements. */
p {
/* It sets a fixed font size of 16 pixels. */
font-size: 16px;
}
Explanation
This code sets an absolute font size of 16 pixels for all <p>
elements. This size will not change based on the user's browser settings.
Example 2: Using Em Units
/* This rule applies to a div with the id "container". */
#container {
font-size: 20px;
}
/* This rule targets paragraphs inside the container. */
#container p {
/* The font size of the paragraph will be 1.2 times the font size of its parent, which is 24px (20px * 1.2). */
font-size: 1.2em;
}
Explanation
This example uses em
units, which are relative to the font size of the parent element. This allows for scalable text sizes within a specific section of your page.
Example 3: Using Rem Units
/* This rule sets the base font size for the entire document on the root HTML element. */
html {
font-size: 16px;
}
/* This rule targets all h1 elements. */
h1 {
/* The font size of the h1 will be 2 times the root font size, which is 32px (16px * 2). */
font-size: 2rem;
}
Explanation
rem
units are relative to the font size of the root <html>
element. This makes it easier to manage and scale font sizes across your entire website consistently.
Example 4: Using Percentages
/* This rule applies to the body of the document. */
body {
font-size: 100%; /* This is often equivalent to the browser's default of 16px. */
}
/* This rule targets elements with the class "large-text". */
.large-text {
/* The font size will be 150% of the parent's font size. */
font-size: 150%;
}
Explanation
Percentages work similarly to em
units, as they are relative to the parent element's font size. A value of 100%
is typically the browser's default font size.
Example 5: Using Viewport Width (vw)
/* This rule targets all h2 elements. */
h2 {
/* The font size will be 5% of the viewport's width. */
/* This creates responsive text that scales with the browser window size. */
font-size: 5vw;
}
Explanation
Viewport width (vw
) units make the font size responsive to the width of the browser window. This is a powerful tool for creating fluid and responsive typography.
Example 6: Using Keywords
/* This rule applies to elements with the class "small-text". */
.small-text {
/* It uses a predefined keyword to set the font size. */
font-size: small;
}
Explanation
CSS provides several keywords like small
, medium
, and large
for setting font sizes. These are relative to the user's default font size.
Example 7: Using the calc()
Function
/* This rule targets the main content area of a page. */
main {
/* It calculates the font size by combining a relative unit (rem) with a fixed unit (px). */
font-size: calc(1rem + 2px);
}
Explanation
The calc()
function allows you to perform calculations to determine a CSS property value. In this case, it creates a flexible font size that has a base rem
value with a small px
adjustment.