When you build a website, the first thing that is important to notice is how the font size is shown on different screens. Whether you’re designing a blog, making an online store, or building a personal portfolio, font size is important. This is the place where the CSS font-size property becomes important. The font size property in CSS is used to define the size of the text on a webpage. In this blog, you will learn everything about the CSS font-size property, including syntax, examples, and best practices for using font size in CSS.
Table of Contents:
What is CSS?
CSS stands for Cascading Style Sheets. It refers to the language that is used to style and design the HTML elements. In simple words, CSS is used to make the user interface interactive for users. CSS provides different properties to style HTML elements like headings (<h1>), paragraphs (<p>), buttons (<button>), and hyperlinks (<a>).
The font size Property in CSS
The font-size property in CSS is used to define the size of the text that is displayed on the browser. It is one of the most important CSS properties because it directly affects the readability of the content. Here is the syntax of the font size property in CSS.
Syntax:
selector {
font-size: 16px;
}
Here are some common values that are used to set the value of the font size of the texts displayed on the webpage:
Values |
Description |
medium (Default) |
This is the default value of the font-size property in CSS. It sets the font size to medium. |
larger |
It can provide a large font size to the element as compared to the parent element. |
smaller |
It can provide a small font size to the element as compared to the parent element. |
xx-large |
It set the font size to xx-large. |
x-large |
It set the font size to x-large. |
small |
It set the font size too small. |
x-small |
It set the font size to extra small as compared to a small. |
xx-small |
It set the font size to xx-small. |
length |
You can use px, em, rem to set the font size of the element. |
initial |
It sets this property to its default value. |
inherit |
It allows the elements to inherit the font size of the parent element. |
% |
You can set the font size in percent. The size of the font will be relative to the parent element. |
vh and vw |
These units are used in making texts responsive. |
Launch Your Web Development Career
Join Today
How to Change Font Size in CSS?
If you’re building a website, you may find a need to make your text bigger or smaller. This is the place where the font-size property in CSS comes in. The CSS font-size property allows you to control the size of the text in multiple ways. Here are some of the important ways to set the font size of the text on the website.
1. By Using Absolute Font Sizes
CSS provides multiple keywords like small, medium, large, etc., which refer to some predefined font sizes. These are called absolute font sizes, and they do not depend on the font size of the parent element. Here is the list of all the values that you can use to set font sizes.
- Small
- xx-small
- x-small
- Medium
- large
- x-large
- xx-large
Syntax:
p {
font-size: large;
}
2. By Using Relative Font Sizes
Relative font sizes adjust the font size of the HTML element based on the size of the parent element. This offers keywords like smaller and larger, which are relative in nature, and compares the current element’s font size to its parent’s.
Syntax:
div {
font-size: larger;
}
3. By Using Length Units
You can define font size by using units like pixels (px), points (pt), centimeters (cm), em (1em means 100% size of the font size of the parent element), and rem (1rem means 100% size of the root element). This will provide full control over the size of the text, and you can adjust the size of the text according to your needs.
Syntax:
p {
font-size: 18px;
}
4. By Using a Percentage Value
You can also set the font size by using a percentage. The percentage is measured relative to the font size of the parent element. If the user sets the font size to 100%, it means that the font size of the HTML element is the same as the font size of the parent element.
Syntax:
p {
font-size: 120%;
}
5. By Using Units like vw and vh
For creating responsive designs, you can set the font size of the HTML elements by using the viewport width (vw) or viewport height (vh). This helps the text to grow or shrink according to the size of the screen.
Syntax:
h1 {
font-size: 5vw;
}
Example: All Font Size Method
Output:
Best Practices for Using font-size
Choosing the right font size according to the device is important because it helps people to read the content easily, no matter what device they’re using. Here are some best practices that you need to follow while using the font-size property in CSS:
- Instead of using fixed units like px (pixels) for setting font sizes, try to use relative units like em, rem, or % to set how big or small text is visible to users.
- Always use large font sizes for headings, this makes the heading clearly visible to the readers.
- Try to test your font size on different devices and browsers to make sure that it is easily readable to users.
Get 100% Hike!
Master Most in Demand Skills Now!
Conclusion
The CSS font-size property is a small but important part of website design. Using the right font size helps in improving readability. It also ensures that your content is visible to different devices. There are many ways to change font size in CSS, from using keywords like small or large to using units like px, em, or %. Choosing the right font size is helpful in enhancing the User Interface of your website.
To learn more about Cascading Style Sheet (CSS), check out this Web Development course and also explore CSS Interview Questions prepared by industry experts.
CSS Font Size Property – FAQs
Q1. What is CSS?
CSS stands for Cascading Style Sheets. It is defined as the styling language that is used to style HTML elements on the webpage.
Q2. What is font size?
The font size property in CSS is used to set the size of the text on the webpage.
Q3. How can I change font size in CSS?
You can easily change the font size by using the font-size property in CSS.
Q4. What is font-size: 100% in CSS?
The font-size: 100% means the text will be of the same size as the parent element’s font size.
Q5. What is font style?
The font style is used to apply styling on the text, like normal, oblique, italic, etc.