×

How to change the font in CSS

How to change the font in CSS?

Professionals can't deny typefaces — how users organize and style text — if we want to completely customize the website's design. One may wish to change the font's style, size, or color based on the designs, business, themes, specialty, and audience.

For example, if the color scheme favors dark grey over black, we want to adjust the text's default font color. We may use a sans serif font if we work in the media, which is usually the simplest to read type. A beautiful font like Morris Troy would be a better choice when we're in the creative area. These are a few reasons why one may wish to change the typeface on the website.

Identifying the right typeface has a massive effect on how visitors interact with a website.

The appropriate typeface can help you establish a powerful brand identity.

It is critical to have an easy-to-read typeface. Your text will be more valuable because of the typeface. It is also critical to select the appropriate font color and size.

Font Families:

There are five generic font families in CSS:

  1. Each letter in a serif font has a tiny stroke at the edge. They give off a stately and elegant vibe.
  2. Clean lines characterize sans-serif fonts (no small strokes attached). They give off a modern, minimalistic vibe.
  3. Monospace typefaces have the same set width for all letters. They give off a mechanical vibe.
  4. Cursive fonts are designed to seem like human handwriting.
  5. Fantasy typefaces are fun and decorative.

The various font names are members of one of the generic font families.

Begin with the typeface we desire to work down to a general collection (to let the browser know it has to select a similar font from the generic family if no other fonts are available). To achieve optimal interoperability amongst browsers/operating systems, the font-family field should hold many font names as a "fallback" mechanism. A comma should be used to separate the font names.

Syntax: -

font-family: “font_name”,”font_family_name”;

This CSS property is a shorthand property that combines the typefaces, font-variant, font-weight, font-stretch, font-size, line-height, and font-family sub-properties into a statement. In CSS, the font property is then used to manage the appearance of text. We may modify the font size, color, style, and more by using it.

The font size and font-family values of the abbreviated font property are required. If either of these attributes is missing, the declaration will be overlooked. The font-family value must likewise be declared last among all values; otherwise, the declaration will be neglected. The remaining five parameters are optional.

Font-style, font-variant, and font-weight must be stated before the declaration's font size if used in the font shorthand property. If they're not, they will be overlooked, resulting in a lack of understanding of necessary values.

The CSS Font property can be applied using inline CSS, internal CSS, or external CSS. Also, it can be applied to any HTML element like id, class, or tag.

We can also apply different font families and font-size to different HTML elements within the same HTML code.

Example: -

<!DOCTYPE html>
<html>
<head>
<style>
h1,h2
{
	font-family : "Brush Script MT","Cursive";
    font-size : 36px;
}
#id_time {
  font-family: "Georgia", Times, serif;
  font-size : 20px;
}
.class_arial {
  font-family: Arial, Verdana, sans-serif;
  font-size : 18px;
}
.mono {
  font-family: "Monaco","Lucida Console", "Courier New", monospace;
  font-size: 25px;
}
</style>
</head>
<body>
<h1>CSS font-family Example</h1>
<p id="id_time">This paragraph is an example of Georgia Font which belongs to serif family.</p>
<p class="class_arial">This paragraph is an example of Arial,Verdana Font which belongs to sans serif family</p>
<p class="mono">This his paragraph is an example of Lucida Console,Monaco,Courier New Font which belongs to sans serif family</p>
<h2>End Of example of Font Family</h2>
</body>
</html>

Output: -

How to change the font in CSS

So now let’s understand the above example. Here, we have applied different font styles to different elements of HTML.

As for h1, an h2 tag font from the cursive family with font-size 36px is applied.

The next paragraph tag, which has id id_time, has applied Georgia font from the serif font family with a font size of 20px.

For a paragraph which has a class named class_arial is applied with Arial, Verdana font from sans serif font family.

In the third paragraph, we have applied font Lucida Console, Monaco, Courier New for class mono which belongs to the sans serif font family.


Related Topics

CSS Zoom

CSS Zoom: Zoom property in CSS extents the content. This property controls the content’s level of magnification. Rather than the use of this CSS property, we may also apply the...

3 minutes read.

Untitled Reusable Block

  ...

1 minute 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 Dropdowns

Dropdowns are one of the maximum crucial parts of an interactive internet site. CSS is a kind used to design the drop-down menus. A drop-down essentially is a group of...

5 minutes read.

CSS Functions

CSS functions are used for different CSS properties. Every front-end developer should have knowledge of all CSS functions. It makes the coding easier. FunctionsExplanationattr().It returns the value of an attribute of...

5 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 Background-clip

CSS Background-clip: The background-clip property describes the background’s painting area. It limits an area where the image or color of the background appears by using any clipping box. Everything will...

2 minutes read.

CSS Background-blend-mode

Background-blend-mode The background-blend-mode property sets a blend mode of the component’s every background layer (color/image). It illustrates how the element background image blends well with the element background color. We can...

9 minutes read.

Css Box Model

Introduction The Box Model property of CSS is used for design and layout of an html page. In CSS, all the elements has a rectangular box around it, you have to...

5 minutes read.

What is Bootstrap CSS?

Bootstrap: - Bootstrap is a CSS framework that is free to use and is used for the front-end development of the website in a faster and easier way. Bootstrap was released as...

3 minutes read.

CSS Specificity

CSS Specificity: If one or more conflicting rules of CSS express a similar element, a browser will pursue a few rules to examine the specific one. Specificity can be specified...

3 minutes read.

CSS Image Gallery

Image galleries are used to display and store collections of images. Example Let’s discuss an example. CSS was used to create the following image gallery: Code is as follows: <html> <head> <style> div.gallery {   margin: 6px;   border: 1px...

5 minutes read.

CSS Lists

CSS Lists: Several properties are available in CSS that are used to manage the lists. Lists are distributed as ordered and unordered kind of lists. The ordered lists provide the...

7 minutes read.

How to center a button in CSS

How to center a button in CSS? In the tutorial, we will learn how to center a button in CSS. CSS (Cascading Style Sheets) gives an HTML web page the greatest possible...

4 minutes read.

CSS Box-shadow

CSS Box-shadow This property is referred to include effects as shadow-like around an element’s frame. Syntax box-shadow: h-offset v-offset blur spread color |inset|inherit|none; Property values Following are the various property values which can be used...

3 minutes read.

CSS Text-Overflow

Introduction: The text-overflow property of CSS is used to format the text which is overflowing the boundaries of the container. This property helps user to clip the text, to display the...

5 minutes read.

CSS Comments

CSS Comments: Generally, comments in CSS are used to describe our code. Further, it will provide support to many users who are concerned with the code. Somewhat, these are ignored...

2 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 Page-break-before Attribute

Page-break-before Attribute As its name implies, the page-break-before property is applied to include page break before any element, at the time of printing any document. It adds the page break before...

5 minutes read.

CSS Width

CSS Width The width property in CSS sets the content width of area of the element. These properties do not provide margin and border features. It is used to set the...

1 minute read.