How To Change Font in HTML?
Last Updated :
17 Oct, 2024
Changing fonts in HTML can significantly improve the readability and appearance of your website. There are several methods for adjusting the style, size, or type of font. Let's see each approach in detail.
How to Change Font Using CSS
1. Changing Font Using Inline CSS
Inline CSS is applied directly to individual HTML elements using the style attribute. This method is quick but not recommended for large projects because it can make your code messy and harder to manage.
Syntax:
<element style="property: value;">Content</element>
- element: The HTML tag where the style can be applied (like <p>, <h2>, etc..).
- style: The attribute that specifies the style rule.
- property: value: The CSS rule. In this case, the property is font-family (to change the font) and the value is the font name.
Example: In this example, the <p> element has the inline style where the font family is set to Arial. The text "This is a paragraph with the Arial font." will be displayed using the Arial font.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Inline CSS Example</title>
</head>
<body>
<h1 style="color: green; font-family: 'Arial'; font-size: 16px;">
GeeksforGeeks
</h1>
<p style="font-family: 'Arial'; font-size: 16px; color: blue;">
This is a paragraph with Arial font,
blue color, and 16px font size.
</p>
</body>
</html>
Output: The text can be displayed in the Arial font with the font size of the 16px and blue color.
Using the Inline CSS2. Changing Font Using Internal CSS
Internal CSS is written inside the <style> tag within the <head> section of your HTML file. This method is useful when you want to apply styles to the whole page without using an external stylesheet.
Syntax:
<head>
<style>
selector {
property: value;
}
</style>
</head>
Example: In this example, we can use the <style> tag inside the <head> section to apply the styles to the <p> element. The font-family can be set to the "Times New Roman" and the font-size is set to the 18px for all the <p> elements in the document.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Internal CSS Example</title>
<style>
p {
font-family: 'Times New Roman';
font-size: 18px;
color: green;
}
</style>
</head>
<body>
<h1 style="color: green; font-family: 'Arial';
font-size: 16px;">GeeksforGeeks</h1>
<p>This is a paragraph with Times New Roman font,
green color, and 18px font size.
</p>
</body>
</html>
Output: The text will be displayed in the Times New Roman font with the font size of the 18px and green color.
Using the Internal CSS3. Changing Font Using External CSS
External CSS is the most efficient way to style your HTML document. You create a separate .css file that contains all your styling rules, and then link that file to your HTML document. This approach allows you to reuse the same styles across multiple pages.
Syntax:
In HTML:
<head>
<link rel="stylesheet" href="styles.css">
</head>
In style.css:
selector {
property: value;
}
Example: In this example, the HTML file links to the external CSS file named as the style.css using the <link> tag. In the style.css file, the font-family for the <h1> element is set to the "Georgia" and the color is set to the purple.
HTML
<!DOCTYPE html>
<html>
<head>
<title>External CSS Example</title>
<link rel="stylesheet"
href="styles.css">
</head>
<body>
<h1 style="color: green;">GeeksforGeeks</h1>
<h1>This heading uses Georgia font with external CSS</h1>
</body>
</html>
CSS
h1 {
font-family: 'Georgia';
color: purple;
}
Output: The heading will be displayed in the Georgia font, with the font size and the purple color.
Using the External CSSHow to Change Font Using Google Fonts
Google Fonts is the free library of the web fonts provided by the Google. We can easily integrate these fonts into the website by including the link to the desired font in the HTML, and then applying it using CSS.
Syntax:
In HTML
<head>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=Font+Name&display=swap" rel="stylesheet">
</head>
In CSS
selector {
font-family: 'Font Name', sans-serif;
}
Example: In this example, the HTML file links to the Google Fonts using the <link> tag. The font-family for the <p> element can be set to the "Roboto" and the font from Google Fonts, with the fallback to the sans-serif if Roboto is unavailable.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Google Fonts Example</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
<style>
p {
font-family: 'Roboto', sans-serif;
color: darkred;
}
</style>
</head>
<body>
<h1 style="color: green;">GeeksforGeeks</h1>
<p>This paragraph uses Roboto font from Google Fonts and dark red color.</p>
</body>
</html>
Output: The paragraph text can be displayed in the Roboto font and dark red color.
Using the Google FontsHow to Change Font Using @font-face Rule
The @font-face can allows you to include the custom fonts in the CSS by specifying the URL to the font file. This method can gives you the flexibility to use the any font you want, as long as you have the font file.
Syntax:
@font-face {
font-family: 'CustomFont';
src: url('fonts/customfont.woff2') format('woff2');
}
selector {
font-family: 'CustomFont';
}
Example: In this example, the @font-face rule can be used to define the custom font called "MyCustomFont", which is located in the fonts/folder. The font-family for the div element is set to the MyCustomFont.
HTML
<!DOCTYPE html>
<html>
<head>
<title>@font-face Example</title>
<style>
@font-face {
font-family: 'MyCustomFont';
src: url('fonts/MyCustomFont.woff2') format('woff2');
}
div {
font-family: 'MyCustomFont';
font-size: 20px;
color: navy;
}
</style>
</head>
<body>
<h1 style="color: green;">GeeksforGeeks</h1>
<div>This text uses a custom font loaded with
@font-face, with navy color and 20px font size.
</div>
</body>
</html>
Output: The text inside the div can be displayed using the custom font defined with the @font-face, navy color and the 20px font size.
Using the @font-face RuleHow to Change Font Using the HTML <font> Tag
The <font> tag is an old and deprecated method of changing fonts. It’s no longer recommended because it mixes content with style, which makes your code harder to maintain. However, it’s still worth knowing for historical reasons.
Syntax:
<font face="Courier" size="Number" color="ColorName">Content</font>
Example: In this example, the <font> tag changes the font to Courier, the size to 4, and the color to blue. Despite being simple, this method is outdated and lacks flexibility, which is why CSS is now the preferred way to change fonts.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Change Font Example</title>
</head>
<body>
<font face="Courier" size="4" color="blue">This text uses the Courier font.</font>
</body>
</html>
Output:
Using the HTML <font> Tag
Similar Reads
How to Change Font Size in HTML ?
To change the font size of any text we can use the CSS font-size Property, or some HTML keywords have some fixed font size but we change them by using this CSS property. We can use style attributes in the HTML. As we know, HTML has no <font> tag, so we are using CSS style to add font size. The
2 min read
How to Change Font Color in HTML?
We can use <font> tag to change the text color in HTML. This tag was used in older versions of HTML but is deprecated in HTML5. So we can use inline CSS as the best and quickest way to change the font color. 1. Change Font Color using <Font> tagThe <font> tag was used to change tex
2 min read
How to change the font size in HTML ?
Change the font size means increase or descrease the font size using HTML. The <font> size attribute was used to change the font size in HTML. The <font> tag is depreciated in HTML5 an not in use. Now, CSS font-size property is used to change the fint size. Table of Content Using HTML
2 min read
How to Get Custom Fonts in HTML?
Using custom fonts in HTML can make your web pages look more unique and visually appealing. It helps improve the overall design and user experience by adding a personal touch to your site. These are the following approaches to get the custom Fonts in HTML: Table of Content Using Google FontsUsing @f
3 min read
How to change the Font Style in CSS ?
Font style is an important element that enhances the readability of the text. CSS provides several properties to change the text appearance and font style. The font style property allows you to define how the text should appear normal, italic, or oblique. Table of Content Different Methods to Change
3 min read
How to Change the Button Color in HTML ?
Styling of elements enhances the visual appearance and improves the overall user interface. We can change the button color in HTML using different approaches as listed below. Table of Content Using Inline StylingUsing Internal StylingChange the Button Color Using Inline StylingIn this approach, we a
3 min read
How to Change Button Size in HTML?
To change the size of a button in HTML, there are several methods such as using the style attribute, external CSS, or adjusting the padding and font size. Modifying the buttonâs size enhances both the visual design and the user experience of a website. Here are three approaches to changing the size
2 min read
How to Align Text in HTML?
We can use align attribute in HTML to align text to the left, right, and center. This attribute was used in older versions of HTML but it is deprecated in HTML5. We can change the alignment of the text using the text-align property for modern and flexible text alignment. 1. Align Text using align at
2 min read
How to Use Font Awesome Icons In HTML?
Icons play an important role in our modern applications. If we use good icons in our website then it can attract the user's attention and also help the user to identify which purpose it is used for. PrerequisitesHTMLCSSFont Awesome iconsApproach To Use Font Awesome Icons First, you need to add the F
3 min read
Change the Font Used in htmlwidgets for R
In this article, we will see how to change the font used in htmlwidgets for R programming language. What are Htmlwidgets in R The htmlwidgets is a package in R language that allows the creation of interactive HTML widgets using R code. It provides a framework for creating widgets using HTML, CSS, an
2 min read