×

How to change font size in CSS

How to change font size in CSS?

Font Size: -

Users may attract attention to content on a website page in various ways. One element in a sentence can be highlighted. You could, for example, make it orange. It might be bolded or underlined.

One can also vary the font and size of content to draw the reader's eye and generate a visual hierarchy in addition to these formatting options.

We don't have to use HTML headings to modify the size of the text on the website. Let's imagine we wish to adjust the size of the text of other items on the page or downsize or increase the default header sizes. In that situation, you can use CSS to modify the font size. Let's go over the steps in detail.

The font-size property determines the text's size.

In web design, the ability to control font size is crucial. Font size alterations should not be used to create sections that look like headings or headers that look like paragraphs.

Always use HTML tags for headers and paragraphs, such as h1> - h6>.

The font-size parameter can be either absolute or relative.

The type size The CSS attribute controls the font size. The font size can be specified in various methods, including using terms or numeric data for pixels or ems. Based on the requirements of the specific web page, select a suitable solution.

The font-size property provides many techniques for changing the text size on the website. The site's demands and objectives will determine each one we choose.

Absolute dimensions:

Sets the text size to a specific value.

In some browsers, the user cannot modify the text size (bad for accessibility reasons)

Whenever the basic details of the output are known, absolute size is useful.

Relative dimensions:

Sets the size concerning the items around it.

In browsers, this feature allows users to modify the font size.

Syntax: -

font-size: medium|large|x-large|xx-large|xx-small|x-small|small|;  

If no font size is specified, the default size for typical text, such as paragraphs, is 16px, equivalent to 1em.

Using Absolute dimensions:

The default font size is used for absolute-size keywords. The default font size is usually medium (equating to 16 pixels or 1em), though this might vary depending on the browser and font family. The standard font size in HTML is 16 pixels.

The following are absolute-size keywords:

  • xx-small
  • sx-small
  • ssmall
  • smedium
  • slarge
  • sx-large
  • sxx-large
  • sxxx-large

Example: -

<!DOCTYPE html>
<html>
<head>
<style>
div.xxs { font-size: xx-small; }
div.xs { font-size: x-small; }
div.s { font-size: small; }
div.m { font-size: medium; }
div.l { font-size: large; }
div.xl { font-size: x-large; }
div.xxl { font-size: xx-large; }
div.xxxl { font-size: xxx-large; }
</style>
</head>
<body>
<h1>The Example of font-size Property</h1>
<div class="xxs">Showing behaviour of xx-small</div>
<div class="xs">Showing behaviour of x-small</div>
<div class="s">Showing behaviour of small</div>
<div class="m"> Showing behaviour of medium</div>
<div class="l">Showing behaviour of large</div>
<div class="xl">Showing behaviour of x-large</div>
<div class="xxl">Showing behaviour of xx-large</div>
<div class="xxxl">Showing behaviour of xxx-large</div>
</body>
</html>

Output: -

How to change font size in CSS

Absolute dimensions simplify resizing text and construct a font hierarchy on your page. However, not all browsers allow users to modify the text size, making this a poor choice for inclusive design.

Using relative dimensions:­-

The relative-size keywords make the text larger or smaller concerning the parent element's font size. The proportion of the absolute dimension is used to calculate this relative dimension

A child element with a stated relative dimension of lower will have a medium text size if the primary component has a large text size. 

Example: -

<!DOCTYPE html>
<html>
<head>
<style>
.eg_relative { font-size: smaller; }
</style>
</head>
<body>
<h1>The Example of font-size Property using relative </h1>
<div style="font size: large">
  <h2>this heading is Parent element</h2>
  <h2 class="eg_relative">this heading is Child Element</h2>
</div>
</body>
</html>

Output: -

How to change font size in CSS

Font-size using pixels: -

Setting pixels (px) for your size value allows you to determine the font size regardless of the visitor's browser. You can give the exact number of pixels in height for which we want your text to be rendered in a browser. 

Example: -

<!DOCTYPE html>
<html>
<head>
<style>
div.x { font-size: 10px; }
div.y { font-size: 18px; }
div.z { font-size: 24px; }
div.w { font-size: 35px; }
div.r { font-size: 75px; }
</style>
</head>
<body>
<h1>The Example of font-size Property using pixels </h1>
<div class="x">The text is of 10px.</div>
<div class="y">The text is of 18px.</div>
<div class="z">The text is of 24px.</div>
<div class="w">The text is of 35px.</div>
<div class="r">The text is of 75px.</div></body></html>

Output: -

How to change font size in CSS

Font-size using ems: -

The em unit adjusts the font size concerning the parent element's font size. Setting content to a text size of 2em would make it twice as large as the surrounding text.

For an inclusive design, selecting text size in em units is essential. Users can alter the size of the text in all browsers because ems are a relative measurement.

The ems compound is the only disadvantage. Let's assume a 2em font size span> element contains another span> element. The size of the nested< span> element would be doubled, or 4em.

Example: -

<!DOCTYPE html>
<html>
<head>
<style>
span { font-size: 3em; }
</style>
</head>
<body>
<h1>The Example of font-size Property using ems </h1>
<div>This text is not in proper format. <span>So The text will be twice the size of the not formated text. The word which will next be is <span>larger</span> than the rest of the sentence.</span> And the text is unformatted again.</div>
</body>
</html>

Output: -

How to change font size in CSS

Related Topics

CSS Images

Images in CSS: Images are a critical part of a web application. It includes various types of images in the web application that cannot be recommended. However, it is essential...

4 minutes read.

CSS Animation

CSS Animation: The animation property of CSS is applied to make animation over any webpage. It is used as the animation replacement made by JavaScript and Flash. Rules of CSS3 @keyframes The...

3 minutes read.

CSS Pagination

CSS Pagination The pagination in CSS is an advantageous approach for indexing of a website’s distinct pages over the homepage. When our site contains multiple pages, we must insert a few...

13 minutes read.

CSS :root selector

CSS :root selector This CSS pseudo-class matches any document’s root element. It chooses the parent of the highest-level inside the document DOM or tree. An element of <html> is always a...

2 minutes read.

CSS User Interface

Introduction User Interface can be defined as series of visual elements like screens, buttons, forms that help the user interact with the software application or hardware device. CSS User Interface allows the...

6 minutes read.

Internal CSS

What is CSS? CSS stands for Cascading Style Sheet.CSS is used to apply styling the HTML elements.With CSS user can add or change color, font-size and font-family. It also helps user...

4 minutes read.

CSS Order

CSS Order CSS order property in CSS describes the flex item’s order inside the flex or any grid container. It is applied to order any flex item. Note: If any component is...

3 minutes read.

CSS Overlay

CSS Overlay: It measures to cover-up something’s surface along with a coating. CSS overlay sets anything over the top area of other things. It creates a web-page more attractive. The...

8 minutes read.

Divi in CSS

Divi is popular WordPress themes due to its versatility and user-friendly interface. It includes a built-in page builder for sophisticated design options and an assortment of customization features to help...

9 minutes read.

CSS Units

CSS Units In CSS, there are so many units available to show the length’s measurement. A single unit of CSS is applied to examine the size of the property, which the...

5 minutes read.

CSS Browser Support

The desk underneath lists of all CSS properties and the way each belongs to it is supported in the generally unique browsers: - The quantity to the right of the browser...

2 minutes read.

CSS Position

CSS Position: The position property in CSS is applied to set the position of the elements. It can be used to position the backside of an element. It is also...

3 minutes read.

CSS @keyframes rule

CSS @keyframes rule It describes the rule of animation that depicts the properties of CSS for the components at every state along with the timeline. We may make the properties of complex...

4 minutes read.

What is a CSS File?

What is a CSS File and How to use it in Html? CSS: - CSS stands for Cascading Style Sheet. CSS is a file of style sheets that describe the presentation of...

4 minutes read.

CSS Counters

CSS Counters: The CSS counter property is similar to the variables. CSS counters are controlled via CSS, and its values are incremented via rules of CSS to record how much...

4 minutes read.

CSS Loader

CSS Loader The loader is the animation that can alert a visitor regarding the loading of the page. We need to wait for some time. It will be helpful if any...

4 minutes read.

CSS Radio Button

Radio Button A radio button has been defined as an element of HTML. It can support us to take any input from the user. But it is challenging to design a...

4 minutes read.

CSS Word Wrap

Word Wrap in CSS: The word-wrap property in CSS is referred to break any lengthy word and wrap towards the next or following line. It is employed for preventing overflow...

1 minute read.

CSS Links

CSS Link is used to create styled link. There are various ways to implement it. Styling Links Link can be styled with CSS properties like color, font-family, background etc. Example: <!DOCTYPE html>   <html>   <head>   <style>   a {       color: red;   }   </style>   </head>   <body>   <p><b><a href="https://www.tutorialandexample.com"    target="_blank">This is a link</a></b></p>   </body>   </html> Try Now There are four...

2 minutes read.

CSS Gradient

CSS Gradient: The CSS gradient property is applied to show the smooth transition inside two specified colors or more than two specified colors. Need of CSS Gradient The following are a few...

4 minutes read.