Welcome to The Coding College! One of the fundamental aspects of web design is ensuring your text is legible and visually appealing. The font-size
property in CSS allows you to control the size of your text, creating a better user experience.
In this guide, we’ll explore the font-size
property, its syntax, units, and best practices for responsive and accessible typography.
What is CSS font-size
?
The font-size
property sets the size of the text. It can be defined using various units, offering flexibility to fit different layouts and devices.
Syntax
selector {
font-size: value;
}
Units for Font Size
CSS offers several units for specifying font size. Each has its unique characteristics and use cases:
1. Absolute Units
Unit | Description | Example |
---|---|---|
px | Pixels. A fixed size. Ideal for precise control. | 16px |
pt | Points. Commonly used in print. | 12pt |
cm , mm , in | Measurements for print media. | 1cm |
2. Relative Units
Unit | Description | Example |
---|---|---|
% | Relative to the parent element’s font size. | 150% |
em | Relative to the font size of the parent element. | 1.5em |
rem | Relative to the root element’s font size (usually <html> ). | 1.5rem |
vw | Relative to 1% of the viewport’s width. | 5vw |
vh | Relative to 1% of the viewport’s height. | 5vh |
3. Keywords
Keyword | Description | Example |
---|---|---|
xx-small | Extra extra small font size. | |
small | Small font size. | |
medium | Default size (typically 16px). | |
large | Large font size. | |
xx-large | Extra extra large font size. |
Practical Examples
1. Using Pixels (Fixed Size)
p {
font-size: 18px;
}
This sets the font size to a fixed 18 pixels.
2. Using Relative Units for Flexibility
h1 {
font-size: 2em; /* 2 times the parent element's size */
}
3. Using Root-relative Units
body {
font-size: 16px; /* Base size */
}
h1 {
font-size: 2rem; /* 2 times the root size */
}
4. Responsive Sizing with Viewport Units
h1 {
font-size: 5vw; /* 5% of the viewport width */
}
5. Scaling Text with Percentages
div {
font-size: 150%; /* 1.5 times the parent element's size */
}
Choosing the Right Units
- Pixels (
px
): Use for precise and consistent control. Best for small UI elements like buttons. - Relative Units (
em
,rem
): Ideal for responsive designs. Ensure consistency across devices. - Viewport Units (
vw
,vh
): Great for fluid layouts that adapt to screen size. - Keywords: Use for quick and basic setups or testing.
Accessibility Best Practices
- Use Relative Units
- Prefer
rem
overpx
to respect user-defined font sizes.
- Prefer
- Avoid Overly Small Fonts
- Ensure a minimum size of 16px for body text to maintain readability.
- Enable User Control
- Avoid locking font sizes in absolute units to allow users to zoom.
Setting a Base Font Size
It’s a common practice to set a base font size on the <html>
or <body>
tag for consistency:
html {
font-size: 16px;
}
Then, use em
or rem
units for scalable typography.
Media Queries for Responsive Font Sizes
You can adjust font sizes for different devices using media queries:
@media (max-width: 600px) {
body {
font-size: 14px;
}
}
@media (min-width: 1024px) {
body {
font-size: 18px;
}
}
Browser Compatibility
The font-size
property is supported by all major browsers. However, the behavior of relative units (vw
, vh
, etc.) can vary slightly on older browsers. Always test across devices.
Conclusion
The font-size
property is a cornerstone of web design, enabling you to craft readable, scalable, and user-friendly text. By leveraging the right units and adhering to accessibility principles, you can create typography that enhances your site’s design and usability.
For more insights on CSS and web development, visit The Coding College.
Scale your text, scale your impact!