Font size controls how big or small text appears on a webpage. It helps make text easy to read and looks good on different devices.
Font size in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
selector {
font-size: value;
}The value can be in units like rem, em, px, or percentages.
Using relative units like rem or em helps make your design responsive and accessible.
p {
font-size: 16px;
}h1 {
font-size: 2rem;
}span.highlight { font-size: 120%; }
body {
font-size: 1em;
}This example shows a big heading, normal paragraph text, and smaller text for less important details. The font sizes use rem units to scale nicely on different devices.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Font Size Example</title> <style> body { font-size: 1rem; font-family: Arial, sans-serif; padding: 1rem; } h1 { font-size: 2.5rem; color: #2a7ae2; } p { font-size: 1rem; color: #333333; max-width: 40rem; line-height: 1.5; } .small-text { font-size: 0.75rem; color: #666666; } </style> </head> <body> <h1>Welcome to Font Size Demo</h1> <p>This paragraph uses the default font size of 1rem, which is easy to read on most screens.</p> <p class="small-text">This smaller text shows how you can make less important info smaller but still readable.</p> </body> </html>
Avoid using only px units because they do not scale well on different devices or user settings.
Use rem for consistent sizing based on the root font size, improving accessibility.
Test your font sizes on small and large screens to ensure readability everywhere.
Font size controls how big text appears on a webpage.
Use relative units like rem or em for better accessibility and responsiveness.
Adjust font size to improve readability and highlight important content.
Practice
font-size control on a webpage?Solution
Step 1: Understand the role of
Thefont-sizefont-sizeproperty in CSS sets how big or small the text appears on the screen.Step 2: Compare with other text properties
Other properties like color or spacing control different aspects, not size.Final Answer:
The size of the text displayed -> Option DQuick Check:
font-size controls text size [OK]
- Confusing font-size with color or spacing properties
- Thinking font-size changes background color
- Mixing font-size with letter-spacing
Solution
Step 1: Recall CSS property syntax
CSS uses a colon (:) to assign values, and units like px must be without quotes.Step 2: Check each option
font-size: 16px; uses correct syntax: property, colon, value with unit, and semicolon.Final Answer:
font-size: 16px; -> Option CQuick Check:
Correct CSS syntax uses colon and units without quotes [OK]
- Using equal sign instead of colon
- Putting units inside quotes
- Omitting units like px
p { font-size: 2rem; }Solution
Step 1: Understand the unit
remremmeans "root em" and is relative to the root (html) font size.Step 2: Interpret
Setting font-size to 2rem means text will be twice as big as the root font size.2remFinal Answer:
The paragraph text will be twice the root font size -> Option BQuick Check:
2rem doubles root font size [OK]
rem units scale relative to root font size [OK]- Thinking rem is pixels
- Confusing rem with em
- Assuming no change in size
h1 { font-size: 20; }Solution
Step 1: Check the font-size value format
CSS requires a unit like px, em, rem after numeric values for font-size.Step 2: Identify the missing unit
The code uses "20" without any unit, which is invalid.Final Answer:
Missing unit after the number -> Option AQuick Check:
Font size needs units like px or rem [OK]
- Omitting units like px or rem
- Assuming numbers alone are valid
- Confusing semicolon errors
p { font-size: ?; }Solution
Step 1: Understand responsiveness and user settings
To respect user font preferences (e.g., browser font size settings), use units relative to the root element.Step 2: Compare options for responsiveness
pxis fixed and doesn't scale with user changes.emand%are relative to parent and can compound in nesting.remis relative to root font size, scaling perfectly with user settings.Step 3: Choose the best option
1.2remincreases size by 20% relative to root, ideal for accessibility and responsiveness.Final Answer:
font-size: 1.2rem; -> Option AQuick Check:
remunits scale with root font size and user settings [OK]
- Using fixed px units ignoring user preferences
- Using em which can compound unexpectedly
- Confusing em/% with root-relative rem
