The font-style
property in CSS is primarily used to specify an italic or oblique face for a font. While italic
is a specially designed italic version of the font, oblique
is a slanted version of the normal font. If an italic version of a font is not available, browsers will typically substitute an oblique version.
Example 1: Using the italic
Keyword
/* This rule targets all emphasis elements. */
em {
/* It applies an italic style to the text. */
font-style: italic;
}
Explanation
This code sets the font-style
to italic
, which is the most common way to italicize text for emphasis.
Example 2: Using the normal
Keyword
/* This rule targets an italic element within a heading. */
h1 i {
/* It removes the italic styling, making the text appear normal. */
font-style: normal;
}
Explanation
The normal
keyword is used to reset the font style to its default, non-italic, non-oblique appearance.
Example 3: Using the oblique
Keyword
/* This rule applies to elements with the class "slanted-text". */
.slanted-text {
/* It applies an oblique (slanted) style to the text. */
font-style: oblique;
}
Explanation
The oblique
value will display a slanted version of the regular font. This is a good fallback if a true italic font is not available.
Example 4: Specifying an Angle for oblique
/* This rule targets elements with the class "custom-slant". */
.custom-slant {
/* It applies an oblique style with a specific 15-degree slant. */
font-style: oblique 15deg;
}
Explanation
You can specify an angle for the oblique
style, giving you more precise control over the slant of the text.
Example 5: Combining with font-weight
/* This rule targets blockquote elements. */
blockquote {
/* It creates a bold and italic style for the text. */
font-style: italic;
font-weight: bold;
}
Explanation
font-style
can be combined with other font properties like font-weight
to create more complex text styling.
Example 6: Styling an Entire Section
/* This rule applies to the footer of the page. */
footer {
/* It sets the entire footer text to be italic. */
font-style: italic;
}
Explanation
This example demonstrates how you can apply a font-style
to a larger container element, affecting all the text within it.
Example 7: Using oblique
with a Negative Angle
/* This rule targets elements with the class "reverse-slant". */
.reverse-slant {
/* It applies an oblique style with a negative angle, slanting the text to the left. */
font-style: oblique -10deg;
}
Explanation
A negative angle with the oblique
value will cause the text to slant to the left instead of the more traditional right.