OneBite.Dev - Coding blog in a bite size

use custom font css

Code snippet for how to use custom font css with sample and detail explanation

Title: Using Custom Font CSS

Welcome to this simple guide on how you can make your webpage more aesthetically pleasing with custom fonts in CSS. This tutorial will provide step by step explanation on the topic.

Code Snippet for Using Custom Font CSS

Here’s a simple snippet of code that demonstrates how you can include a custom font in your CSS:

/* Import the font */
@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');

/* Use the font */
body {
    font-family: 'Roboto', sans-serif;
}

Code Explanation for Using Custom Font CSS

Let’s break down the code fragment step by step.

  1. The @import directive is used to import an external CSS file. In our case, we’re importing the ‘Roboto’ font from Google Fonts. This URL is simply the link to the ‘Roboto’ font on Google Fonts. When inserting the URL for your chosen typeface, be sure to replace ‘Roboto’ with the name of your selected font.

  2. The font-family property is used within the body selector to set ‘Roboto’ as the default font for all elements within the body of the document. The sans-serif value is a fallback font, which will be used in case the ‘Roboto’ font fails to load for some reason.

Remember, you can replace ‘body’ with any other selector to apply the custom font to different elements on your webpage.

That’s it! With this simple approach, you can give your website a unique identity through the use of custom fonts in CSS. Happy coding!

css