Web safe fonts are typefaces that come pre-installed as default fonts on the majority of computers and devices, regardless of operating system.
Using a web safe font gives you the best chance of having your text appear as intended.
Here are some examples of web safe fonts:
The font family property specifies the font stack.
Syntax:
selector{
font-family: font1, font2, font3;
}
Example:
<html lang="en">
<head>
<style>
h1{
font-family: 'Courier New', Courier, monospace;
}
h2{
font-family: 'Times New Roman', Times, serif;
}
</style>
</head>
<body>
<h1>CodeWithHarry</h1>
<h2>Hello World</h2>
</body>
</html>
Tip: It is recommended to end the font family with any of these generic font family names.
To use custom fonts, we will be using Google Fonts.
Follow the steps:
The font style property sets the style of the font.
Example:
<html lang="en">
<head>
<style>
[[p1]] {
font-style: italic;
}
[[p2]] {
font-style: normal;
}
[[p3]] {
font-style: oblique;
}
</style>
</head>
<body>
<p id="p1">font-style: italic</p>
<p id="p2">font-style: normal</p>
<p id="p3">font-style: oblique</p>
</body>
</html>
Defines the colour of the text or typography.
selector{
color: red;
}
For an in-depth explanation of colours, follow the CSS Color Tutorial.
The font size property sets the size of the fonts.
<html>
.Example:
<html lang="en">
<head>
<style>
[[p1]] {
font-size: small;
}
[[p2]] {
font-size: medium;
}
[[p3]] {
font-size: large;
}
[[p4]] {
font-size: larger;
}
[[p5]] {
font-size: 25px;
}
[[p6]] {
font-size: 2em;
}
[[p7]] {
font-size: 2rem;
}
[[p8]] {
font-size: 50%;
}
</style>
</head>
<body>
<p id="p1">font-size: small</p>
<p id="p2">font-size: medium</p>
<p id="p3">font-size: largr</p>
<p id="p4">font-size: larger</p>
<p id="p5">font-size: 25px</p>
<p id="p6">font-size: 2em</p>
<p id="p7">font-size: 2rem</p>
<p id="p8">font-size: 50%</p>
</body>
</html>
The CSS font variant property helps to toggle with the variations of the text.
Example:
<html lang="en">
<head>
<style>
[[p1]] {
font-variant: normal;
}
[[p2]] {
font-variant: small-caps;
}
</style>
</head>
<body>
<p id="p1">font-variant: normal;</p>
<p id="p2">font-variant: small-caps;</p>
</body>
</html>
The font-weight property controls the thickness or boldness of the text.
Example:
<html lang="en">
<head>
<style>
[[p1]] {
font-weight: 100;
}
[[p2]] {
font-weight: 200;
}
[[p8]] {
font-weight: 800;
}
[[p9]] {
font-weight: 900;
}
[[p10]] {
font-weight: lighter;
}
[[p11]] {
font-weight: bold;
}
[[p12]] {
font-weight: bolder;
}
</style>
</head>
<body>
<p id="p1">font-weight: 100</p>
<p id="p2">font-weight: 200</p>
<p id="p8">font-weight: 800</p>
<p id="p9">font-weight: 900</p>
<p id="p10">font-size: lighter</p>
<p id="p11">font-size: bold</p>
<p id="p12">font-size: bolder</p>
</body>
</html>