Skip to content
DeveloperMemos

Changing Font Size in CSS

CSS, Web Development, Front-end2 min read

When it comes to web design, one of the essential aspects is typography. The font size plays a crucial role in determining readability and aesthetics. Fortunately, with CSS (Cascading Style Sheets), you have complete control over the font size of your web page. In this article, we will explore various methods to change font size in CSS and provide examples to demonstrate their usage.

Using the font-size Property

The primary method to adjust the font size in CSS is by utilizing the font-size property. This property allows you to specify the size of the text within an HTML element. You can set the font size in various units such as pixels (px), ems (em), or percentages (%). Here's an example:

1p {
2 font-size: 16px;
3}

In the above example, the font size for all <p> elements will be set to 16 pixels. Remember to apply the CSS rule to the appropriate selector based on your desired target elements.

Relative Font Size with em Units

An alternative approach to setting font size is by using relative units like em. The em unit is based on the font size of the parent element. By specifying a font size in ems, you create a proportional relationship with the parent element's font size.

Consider the following example:

1h1 {
2 font-size: 2em;
3}
4
5p {
6 font-size: 1.2em;
7}

In this case, the heading (<h1>) will have a font size twice as large as its parent element, while the paragraph (<p>) will have a font size 1.2 times larger than its parent element.

Using Percentage for Font Size

Another way to adjust the font size in CSS is by utilizing percentage units. Percentages allow you to set the font size relative to the default or inherited size. For instance:

1p {
2 font-size: 120%;
3}

With this example, the text within the <p> element will have a font size 20% larger than the default size.

Adjusting Font Size for Different Screen Sizes

Responsive web design aims to provide an optimal viewing experience across different devices and screen sizes. To ensure that your font size adapts accordingly, you can use media queries in combination with CSS.

Here's an example of how you can change the font size when the screen width is below 600 pixels:

1p {
2 font-size: 16px;
3}
4
5@media (max-width: 600px) {
6 p {
7 font-size: 14px;
8 }
9}

In this case, the font size inside the <p> element will be 16 pixels on larger screens but reduced to 14 pixels on screens with widths of 600 pixels or less.

Wrapping Up

In this article, we explored various methods to change font size using CSS. By adjusting the font size, you can enhance the readability and aesthetics of your web page. Remember to utilize the font-size property and consider using relative units (em) or percentages for more flexible sizing. Additionally, employing media queries allows you to adapt the font size based on different screen sizes, ensuring a better user experience.