CSS font properties allow you to style and control the appearance of text. Below are the frequently used properties with detailed descriptions and examples:
font-familyDefines the font typeface for an element.
Syntax:
font-family: "Arial", "Helvetica", sans-serif;
Details:
serif, sans-serif, monospace) as a fallback.Example:
p {
font-family: "Times New Roman", Times, serif;
}
font-sizeSets the size of the text.
Syntax:
font-size: value;
Units:
px, pt, cm (fixed size).%, em, rem, vw, vh (responsive size).Example:
h1 {
font-size: 24px;
}
p {
font-size: 1.2em;
}
font-styleSpecifies whether the font should be italic, oblique, or normal.
Values:
normal: Default text.italic: Italicized text.oblique: Slanted text.Example:
em {
font-style: italic;
}
font-variantUsed to display text in small-caps or other stylistic variants.
Values:
normal: Default text.small-caps: Small capital letters.Example:
h2 {
font-variant: small-caps;
}
font-weightDefines the thickness of the font characters.
Values:
normal (default).bold.lighter, bolder.100 to 900 (e.g., 400 for normal, 700 for bold).Example:
strong {
font-weight: bold;
}
p {
font-weight: 300; /* Light */
}
line-heightSets the space between lines of text.
Syntax:
line-height: normal | number | length | percentage;
Details:
normal: Default spacing based on the font size.1.5 multiply the font size to calculate spacing.Example:
p {
line-height: 1.6; /* 1.6 times the font size */
}
letter-spacingDefines the spacing between characters.
Values:
normal: Default spacing.2px, -1em).Example:
h1 {
letter-spacing: 0.1em;
}
word-spacingDefines the spacing between words.
Values:
normal: Default spacing.5px, -1em).Example:
p {
word-spacing: 5px;
}
text-transformControls the capitalization of text.
Values:
none: Default text.capitalize: Capitalizes the first letter of each word.uppercase: Converts all text to uppercase.lowercase: Converts all text to lowercase.Example:
h2 {
text-transform: uppercase;
}
font (Shorthand Property)A shorthand for multiple font-related properties.
font: font-style font-variant font-weight font-size/line-height font-family;
p {
font: italic small-caps bold 16px/1.5 "Arial", sans-serif;
}
letter-spacing and word-spacing to see how they affect readability.em and rem for font-size and line-height to ensure text adjusts well on different devices.