×

How to change font in HTML

To give style to the content of your website, you may change the font of the text and make your website look attractive to the user.

There are several methods which help to change the font of text:

Use of the Style attribute:

We can use the inline style attribute to change the font of the text on the webpage. We can use the style attribute inside any element to change its text.

Syntax:

<element_name style="font-family: font_name;"> Some text </p>

It is important always to write the style attribute inside the starting tag.

Example:

We are using the font-family property of the style attribute to change the text font. We will use four different font families in different paragraphs to make you understand better. You can try the below code to change the text of your web page.

<!DOCTYPE html>
<html>
<head>
    <title> Example 1 of Changing the Font of the Text </title>
</head>


<body>
  <h1> Changing text font using the style attribute with font-family property. </h1>
  <p style="font-family: Arial;"> Font style 1 </p>
  <p style="font-family: sans-serif;"> Font style 2 </p>
  <p style="font-family: 'Segoe UI';"> Font style 3 </p>
  <p style="font-family: 'Times New Roman';"> Font style 4 </p>
</body>


</html>

Output:

The output shows the heading, and then it shows the different text fonts applied to different paragraphs. You can see the difference between these font styles. Each font looks different from the others.

How to change font in HTML

Use of the Font Tag:

You can change the font of the text by using the <font> tag. To change the text font, we need to use the face attribute inside the <font> tag to specify the font type.

Syntax of font tag:

<font face="arial"> Some text </font>

It is necessary to write the starting tag first. In the above syntax, the <font> tag is the starting tag containing the face attribute to specify the font type. It ends with the ending tag. A tag whose name is preceded by a forward slash is called an ending tag. In the above case </font> is the ending tag. The text to which you want to apply the font must be written between the opening and ending tags.

Example:

This example demonstrates the use of the font tag. We will apply two different types of fonts to the text. The one is Arial font, and the other is courier font.

<!DOCTYPE html>
<html>
<head>
    <title> Example 2 of Changing the Font of the Text </title>
</head>


<body>
  <h3> Changing text font by using the Font Tag with the face attribute. </h3>
  <font face="Arial"> Text on which we are applying Arial font. </font> <br> <br>
  <font face="Courier"> Text on which we are applying Courier font. </font> <br> <br>
 </body>


</html>

Output:

As you can see, the output shows a heading and two lines with different fonts. The difference in the font can be easily seen. The first line of text is in Arial font, and the other line of text is in courier font.

How to change font in HTML

Embedding CSS using <style> tag inside the head section:

We can embed CSS in the HTML document using <style> tag. We can add the CSS code inside the style tag and put this style tag inside the head section.

Example:

This example illustrates how to embed CSS into an HTML document. We will change the font of paragraph elements. We will provide the class name to both the paragraphs and apply the font to the text written inside these classes inside the <style> tag.

<!DOCTYPE html>
<html>
<head>
    <title> Example 3 of Changing the Font of the Text </title>
    <style>
        .text1{
            font-family:  Courier;
        }
        .text2{
            font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
        }
   
    </style>
</head>


<body>
  <h3> Changing text font by embedding CSS. </h3>
 <p class="text1"> Text on which we are applying Courier font. </p>
  <p class="text2"> Text on which we are applying multiple fonts. </p>
 </body>


</html>


Output:

The output shows the heading and two paragraphs of different text fonts. You can see that the text in both paragraphs looks different.

How to change font in HTML

Related Topics

HTML Audio

HTML AUDIO: HTML5 provides a method to embed the audio file into a webpage by using HTML5. Before HTML5 audio files is used to play on the browser with the help of...

1 minute read.

HTML Event Attributes

HTML Event Attributes If a browser responds to the user behavior, an event is called. For example, if we click on the submit button, a box of details will appear on...

4 minutes read.

HTML <sup> Tag

The HTML sup element is used to describe one or more superscript contents. The sup tag shows a text which is presented above the regular line and half the length...

2 minutes read.

HTML5 Tags

HTML5 Tags A list of newly included tags is provided in HTML5. These HTML 5 tags (elements) provide a better structure for the document. This list shows description of all HTML...

3 minutes read.

HTML <source> tag

For media attributes, such as <video> tag, <audio> tag, and <picture> tag, we use the <source> tag in html for adding such type of multiple media html elements in our...

4 minutes read.

HTML <script> Tag

For embedding a client -side script, we use script tag in html tag. This <script> tag can be used for containing scripting statements. It can also point another external script file...

2 minutes read.

Responsive HTML5 and CSS3 Tutorial

Introduction to HTML5 Tutorial HTML5 is the latest and upgraded version of HTML. HTML is a hypertext markup language. Technically, it is not a programming language. It is a markup language. HTML...

17 minutes read.

HTML SVG

HTML SVG: SVG stands for Scalable Vector Graphics which defines the graphics for the web. It is a language for describing 2-D graphics in XML. Each drawn shape in SVG is...

1 minute read.

HTML Id Attribute

The HTML id attribute represents the unique identifier for an element of the HTML document. Each element has its unique identifier. The id attribute is a global attribute that can be...

12 minutes read.

HTML5 Attribute

HTML Attribute HTML attributes are special words that provide additional information about the HTML element or attributes.The element or tags have attributes, which define the element's behavior.It also adds attributes with a start...

3 minutes read.

Design a tribute page using HTML and CSS

A tribute page is a webpage based on a person who inspired us in our life. In this article, we will create a webpage of Mahatma Gandhi. We are going...

5 minutes read.

CSS Clearfix

CSS Clearfix: A clearfix (or clear float) in CSS is for a component to clear or fix the child component, so we don’t need to insert additional markup. The clear...

4 minutes read.

How to Create a Portfolio Gallery using HTML and CSS?

First, before starting this, the user should know some of the basics of HTML and CSS to create a portfolio gallery. To add more functionality to the website, we can also...

7 minutes read.

HTML Font Color

The HTML <font> tag has an attribute called color, which defines the color of the text. We use the color attribute inside the <font> tag to specify the color of...

6 minutes read.

Html Wbr Tag

The Line Break Opportunity element A word break opportunity is a place in text from where the browser breaks a line. This opportunity is represented by the element wbr in the...

2 minutes read.

Font tag in HTML

HTML stands for Hypertext Mark-up Language and is used to create web pages and other information that can be later displayed in a web browser. It also consists of a...

4 minutes read.

HTML &lt;body> alink Attribute

HTML <body> alink Attribute What is HTML? The HTML or HyperText Markup Language is used to design web pages. The file extension name for HTML file is ".html". It was released in...

2 minutes read.

HTML <summary> tag

When we simply want to show the primary heading or topic name of hidden detailed material to users, then we can achieved this by using the summary tag. This makes it...

2 minutes read.

HTML Font Size

The HTML <font> tag uses the size attribute to specify the size of the text. We can set the size attribute’s value from 1 to 7. The default size of...

4 minutes read.

How to append HTML code to a div using JavaScript?

In HTML, we have a div tag which is used to create a box where we can put a lot of other tags. So it works like a container. So,...

4 minutes read.