Open In App

CSS font-display property

Last Updated : 10 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The CSS font-display property controls how a custom web font is displayed while it's loading. It determines whether to show a fallback font, hide text temporarily, or swap to the custom font once it's fully loaded.

Font-display allows customizing how web fonts are displayed when the page is being rendered.

It is applied using @font-face rule which defines custom fonts in a style sheet.

Syntax

@font-face {
font-family: Sample;
src: url(samplefont.woff) format('woff'),
url(samplefontbold.woff) format('woff');
font-weight: normal;
font-style: normal;
font-display: optional;
}

Example: In this example we demonstrates the usage of the @font-face rule to import the "Roboto" font with bold and italic styles, utilizing the font-display property to control loading behavior.

html
<!DOCTYPE html>
<html>

<head>
    <title>CSS font-display</title>
    <meta name='viewport' content="width=device-width, initial-scale=1">
    <style>
        @font-face {
            font-family: Roboto;
            src: url(Roboto\Roboto-BoldItalic.ttf) format('truetype');
            font-style: italic;
            font-weight: bold;
            font-display: block;
        }

        div {
            font-family: Roboto;
            font-style: italic;
            font-weight: bold;
        }
    </style>
</head>

<body>
    <h1>Usage of font-display</h1>
    <div>Downloaded font</div>
    <p>Normal font</p>
</body>

</html>

Output:


Next Article

Similar Reads