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:
- Each letter in a serif font has a tiny stroke at the edge. They give off a stately and elegant vibe.
- Clean lines characterize sans-serif fonts (no small strokes attached). They give off a modern, minimalistic vibe.
- Monospace typefaces have the same set width for all letters. They give off a mechanical vibe.
- Cursive fonts are designed to seem like human handwriting.
- 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: -
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.