CSS | @font-face rule

In the CSS @font-face rule is used to associate a font name that can be used in a style sheet. A font-family descriptor is used within the rule to name the font and an src descriptor is associated with an external font name. This can be used with downloadable fonts. This CSS @font-face rule can contain more than one properties (max 24).

Syntax:

@font-face { font-family: fontname; src: url(fontfile path); 
font-stretch: font-stretch Property; font-weight: font-weight Property;
font-style: font-style Property; } 

Parameter: The @font-face rule accepts four parameter as mentioned above and described below:



  • font-family: It is used to hold the fonat-family.
  • src: It is used to hold the src file path(url)
  • font-stretch: It is used to set the text wider or narrower.
  • font-weight: It is used to hold the font-weight.
  • font-style: It is used to hold the font-weight.

Few hosted font services: This services will provide you various types of fonts.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>CSS @font-face rule</title>
  
    <style>
        @font-face {
            font-family: myFirstFont;
            src: url(sansation_light.woff);
        }
          
        @font-face {
            font-family: myFirstFont;
            src: url(sansation_bold.woff);
            font-weight: bold;
        }
          
        div {
            font-family: myFirstFont;
        }
          
        h1 {
            color: green;
        }
    </style>
</head>
  
<body>
    <center>
        <h1>GeeksforGeeks</h1>
  
        <div>A Computer Sciecne Portal fo Geeks</div>
  </center>
  
</body>
  
</html>   

chevron_right


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html lang="en">
<head>
<title>CSS @font-face rule</title>
<style type="text/css">
    @font-face {
        font-family: "OpenSans";
        src: url("/examples/fonts/OpenSans-Regular.eot");
        src: url("/examples/fonts/OpenSans-Regular.ttf") format("truetype");
        font-stretch: normal;
        font-weight: normal;
        font-style: normal;
    }
    @font-face {
        font-family: "OpenSansBold";
        src: url("/examples/fonts/OpenSans-Bold.eot");
        src: url("/examples/fonts/OpenSans-Bold.ttf") format("truetype");
        font-stretch: normal;
        font-weight: normal;
        font-style: normal;
    }
       /* Specify the OpenSans bold font */
    h1 {
        font-family: "OpenSansBold", Arial, sans-serif; 
        color:green;
          
    }
    p {
        font-family: "OpenSans", Arial, sans-serif;
    }
</style>
</head>
<body>
    <center>
    <h1>GeeksforGeeks</h1>
    <p>A Computer Science Portal for Geeks</p>
    </center>
</body>
</html>                    

chevron_right


Output:

Note: There are five Font format those are: TTF/OTF, WOFF, WOFF2, SVG and EOT.
Supported Browsers: The browsers supported by CSS | @font-face rule are listed below:

  • Google Chrome (TTF/OTF, WOFF, WOFF2 and SVG)
  • Interenet Explorer (TTF/OTF, WOFF and EOT)
  • Firefox (TTF/OTF, WOFF and WOFF2)
  • Safari TTF/OTF, WOFF and SVG)
  • Opera TTF/OTF, WOFF, WOFF2 and SVG


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.