Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to change font in HTML

Understanding Fonts in HTML

Before we delve into changing fonts in HTML, let's understand what a font is. With a simple analogy, think of a font as the "outfit" that your text wears. It can be bold, italic, small or big, and has a specific style or design.

HTML (HyperText Markup Language) provides a way to change these "outfits" using various properties. This means you can change how the text looks on your webpage.

The Font Family Property

To change a font in HTML, we use the font-family property. This property allows you to pick the "outfit" for your text. Each font family includes a variety of fonts, similar to how a clothing brand might have different styles of shirts.

Here's a simple example:

<p style="font-family: Arial;">This is a paragraph.</p>

In this example, the text "This is a paragraph" will be displayed in the Arial font.

Adding More Font Families

Sometimes, the user's computer may not support the font family you chose. In such cases, you can provide a list of font families, where the browser will use the first available one. This is like having backup outfits in case your first choice isn't available.

Here's how you can do it:

<p style="font-family: Arial, Verdana, sans-serif;">This is a paragraph.</p>

In this example, if Arial is not available, the browser will try to use Verdana. If Verdana is also not available, it will use any sans-serif font.

Changing Font Size

Now, let's learn how to change the size of our text. HTML uses the font-size property for this. It's like choosing the size of your "outfit".

Here's an example:

<p style="font-family: Arial; font-size: 20px;">This is a paragraph.</p>

In this example, the text will be displayed in Arial font and the size will be 20 pixels.

Changing Font Color

To change the color of the font, we use the color property. This is like choosing the color of your outfit.

Here is an example:

<p style="font-family: Arial; color: red;">This is a paragraph.</p>

In this example, the text will be displayed in Arial font and the color will be red.

Using CSS for Fonts

The examples so far have used inline CSS (Cascading Style Sheets). CSS is a language used for styling HTML elements. You can think of it as a more comprehensive wardrobe for your text, where you can specify outfits (styles) for different elements.

Instead of applying font properties to each paragraph separately, you can use CSS to apply them to all paragraphs at once. Here's how you can do it:

<style>
p {
  font-family: Arial;
  font-size: 20px;
  color: red;
}
</style>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

In this example, both paragraphs will have the same font family, size, and color.

Conclusion

Changing fonts in HTML is like choosing outfits for your text. You can decide the font family (style), size, and color. If your first choice is not available, you can provide alternatives.

Remember, practice is essential in programming. Try changing fonts in your HTML documents and see the changes live. This will not only help you understand the concepts better but also make your web pages more engaging and readable.

Just like how the right outfit can make a person stand out, the right font can make your webpage stand out. Happy coding!