×

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.

How to create a link with no underline in HTML

In HTML, an anchor tag (<a>) is a Styled Container tag, which means when a hyperlink is created using this, browsers, by default, display the link by applying some styles...

5 minutes read.

HTML <samp> tag

The Samp tag in HTML is used for defining the sample output from the computer program. This tag is a phrase type tag and a container tag that means the...

2 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 <br> tag

HTML is referred to as Hyper Text Markup Language, which is the most popular language for creating Web pages. HTML explains the construction of a Web page. It contains a...

4 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 <select> tag

This tag helps in making the drop-down list on the web page. This is highly used in HTML forms for taking the input from user. You can use the id attribute...

3 minutes read.

Html &lt;datalist >Tag

The autocomplete feature in HTML files is provided by the <datalist> tag. It can be used in conjunction with an input tag to enable users to quickly fill out form...

4 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.

HTML Images

HTML has <img> tag to display image on the web pages or web application. This tag is an empty tag i.e. we don't have to close this tag while using. SYNTAX: <img src="source address" attributes="alt, height, width"> Example: <html>   <body>   <center>Image tag Example</center>   <img src="https://www.tutorialandexample.com/resources/images/favicon.png"    alt="tutorialandexample"/>   </body>   </html> Output: Image...

1 minute 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 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 5 Tutorial

What is HTML5? HTML5 is a programming language which stands for the acronym Hyper Text Markup Language 5. It is program that enables the appearance of web pages to be changed, as well as changes...

5 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.

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 Ordered List

We use <ol> tag to make an ordered list in html. An ordered list is made to make a list of items in ordered form, ordered list can be either...

3 minutes read.

HTML Form Input Types

HTML Form Input Types An important element of the HTML form is < input type= "> in HTML. The type attribute of the input element can be of different types, which...

8 minutes read.

Caption Tag in HTML

Caption Tag in HTML: The < caption > tag in HTML describes the heading of a table in the HTML text. Browsers typically make the text found above the table...

4 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.

How to add Google Translate Button on your Webpage?

Google translator is a multi-language translator used for translating the webpage. Google develops it. In today's digital age, it is essential because information is available worldwide in some specific language...

5 minutes read.