In this tutorial, you will learn-

CSS Font Size

The CSS font-size property defines the size of the font.

Font Size

The font-size property sets the size of the text.

Having the option to deal with the text size is significant in website design. Be that as it may, you ought not use font size adjustments to make paragraphs look like headings, or headings look like paragraphs.

Continuously use the appropriate HTML tags, as <h1>- <h6> for headings and <p> for paragraphs.

The font-size value can be an absolute, or relative size.

Absolute size:

• Sets the text to a specified size

• Does not allow a user to change the text size in all browsers (bad for availability reasons)

• Absolute size is useful when the physical size of the output is known

Relative size:

• Sets the size relative to surrounding elements

• Allows a user to change the text size in browsers

Note: If you don’t specify a font size, the default size for normal text, like paragraphs, is 16px (16px=1em).

Set Font Size With Pixels

Setting the text size with pixels gives you full control over the text size:

Example

<!DOCTYPE html>
<html>
<head>
<style>
h1 {
  font-size: 40px;
}

h2 {
  font-size: 30px;
}

p {
  font-size: 14px;
}
</style>
</head>
<body>

<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

</body>
</html>

Tip: If you use pixels, you can in any case use the zoom tool to resize the whole page.

Set Font Size With Em

To allow users to resize the text (in the browser menu), numerous developers use em rather than pixels.

1em is equivalent to the current font size. The default text size in browsers is 16px. In this way, the default size of 1em is 16px.

The size can be calculated from pixels to em using this formula: pixels/16=em

Example

<!DOCTYPE html>
<html>
<head>
<style>
h1 {
  font-size: 2.5em; /* 40px/16=2.5em */
}

h2 {
  font-size: 1.875em; /* 30px/16=1.875em */
 }

p {
  font-size: 0.875em; /* 14px/16=0.875em */
}
</style>
</head>
<body>

<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<p>This is a paragraph.</p>
<p>Specifying the font-size in em allows all major browsers to resize the text.
Unfortunately, there is still a problem with older versions of IE. When resizing the text, it becomes larger/smaller than it should.</p>

</body>
</html>

In the example above, the text size in em is equivalent to the previous example in pixels. Be that as it may, with the em size, it is feasible to adjust the text size in all browsers.

Unfortunately, there is still an issue with more seasoned variants of Internet Explorer. The text becomes larger than it should when made larger, and smaller than it should when made smaller.


Use a Combination of Percent and Em

The solution that works in all browsers, is to set a default font-size in percent for the <body> element:

<!DOCTYPE html>
<html>
<head>
<style>
body {
  font-size: 100%;
}

h1 {
  font-size: 2.5em;
}

h2 {
  font-size: 1.875em;
}

p {
  font-size: 0.875em;
}
</style>
</head>
<body>

<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<p>This is a paragraph.</p>
<p>Specifying the font-size in percent and em displays the same size in all major browsers, and allows all browsers to resize the text!</p>

</body>
</html>

Our code presently works great! It shows a similar text size in all browsers, and allows all browsers to zoom or resize the text!


Responsive Font Size

The text size can be set with a vw unit, which means the “viewport width”.

That way the text size will follow the size of the browser window:

<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<body>

<h1 style="font-size:10vw;">Responsive Text</h1>

<p style="font-size:5vw;">Resize the browser window to see how the text size scales.</p>

<p style="font-size:5vw;">Use the "vw" unit when sizing the text. 10vw will set the size to 10% of the viewport width.</p>

<p>Viewport is the browser window size. 1vw = 1% of viewport width. If the viewport is 50cm wide, 1vw is 0.5cm.</p>

</body>
</html>

Viewport is the browser window size. 1vw = 1% of viewport width. If the viewport is 50cm wide, 1vw is 0.5cm.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your projects with us.


0 Comments

Leave a Reply

Avatar placeholder