How to add color in HTML without CSS ?
Last Updated :
04 Mar, 2024
In HTML, adding color to text and elements is typically done using CSS. However, there are several approaches to add color directly in HTML, without using CSS.
We will discuss the following approaches to add color without CSS in HTML Elements.
Approach 1: Using Font Tag
Use <font> tag to specify the color of text within the tag. In font tags, we have to use color attributes to provide color names or codes.
Syntax
<font color="value">
Example: Illustration ofaddingd color in HTML without CSS Using Font Tag.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Adding color in HTML without CSS</title>
</head>
<body>
<h3>Using font tag with color attribute</h3>
<h1>
<font color="green">Welcome to GeekForGeeks </font>
</h1>
<h2>
<font color="pink">It is an online learning platform</font>
</h2>
</body>
</html>
Output:
OutputApproach 2: Using JavaScript
Define a JavaScript function called changeColor that is used to change the color of an HTML element with the id "myElement". Here we used <script> tagto add javascript code in HTML.
Syntax
<script>
<!-- javascript body -->
</script>
Example: Illustration of adding color in HTML without CSS Using JavaScript
HTML
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Example</title>
<script>
function changeColor() {
var element = document.getElementById("myElement");
element.style.color = "green";
}
</script>
</head>
<body onload="changeColor()">
<h1 id="myElement">Using javascript to change color of this text</h1>
</body>
</html>
Output:
OutputApproach 3: Using SVG tag with fill attribute
SVG tag allows us to create shapes or text in HTML along witha fill attribute which is used to assign the color to that shape or text. In the below example we have created some shapes with different colors.
Syntax
<svg width="value" height="value">
<tag fill="value">Text or Shape</tag>
</svg>
Example: Illustration ofaddingd color in HTML without CSS using SVG tag with fill attribute.
HTML
<!DOCTYPE html>
<html>
<head>
<title>SVG Example</title>
</head>
<body>
<h3>Using SVG</h3>
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" fill="red" />
</svg>
<svg width="100" height="100">
<rect x="10" y="10" width="80" height="80" fill="blue" />
</svg>
<svg width="100" height="100">
<ellipse cx="50" cy="50" rx="40" ry="20" fill="green" />
</svg>
<svg width="100" height="100">
<polygon points="50,10 90,90 10,90" fill="yellow" />
</svg>
</body>
</html>
Output:
OutputApproach 4: Using Text Color Attribute
The text attribute in the <body> tag sets the default text color for all text within the body of the HTML document.
Syntax
<body text="value">
Example: Illustration of add color in HTML without CSS using Text Color Attribute
HTML
<!DOCTYPE html>
<html>
<head>
<title>Text Attribute Example</title>
</head>
<body text="green">
<h1>Welcome to GeekForGeeks</h1>
<h3>Adding color in HTML without CSS
using Text Color Attribute
</h3>
</body>
</html>
Output:
OutputApproach 5: Using the bgcolor Attribute
The bgcolor attribute is used to setthe background color ofan element or entire page. Use the attribute inside the <body> tag.
Syntax
<body bgcolor="value">
Example: Illustration of adding color in HTML without CSS Using the bgcolor Attribute
HTML
<!DOCTYPE html>
<html>
<head>
<title>Adding color in HTML without CSS</title>
</head>
<body bgcolor="yellow">
<h3>Add color in HTML without CSS
using the bgcolor Attribute
</h3>
<h1>GeekForGeeks is the best learning platform</h1>
</body>
</html>
Output:
Output
Similar Reads
How to Change Background Color in HTML without CSS ? In HTML, you can change the background color of an element using the bgcolor attribute. However, it's important to note that the bgcolor attribute is deprecated in HTML5 and should be avoided in favor of using CSS for styling. This article will cover various methods to change the background color of
2 min read
How To Add Opacity To A Color in CSS? To add opacity to a color in CSS, you can use the rgba() function, which stands for red, green, blue, and alpha (opacity). The rgba() function allows you to specify the opacity of a color by adding a fourth parameter called the "alpha channel." The alpha value ranges from 0 (completely transparent)
3 min read
How To Add Border In HTML? Adding Borders to HTML elements is a common way to enhance the presentation and layout of web pages. Borders can be added to divs, paragraphs, images, and tables to separate or highlight content visually. CSS can provide several properties to customize the borders, such as color, style, and width.Th
4 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 add color picker in a form using HTML ? In this article, we will learn how to add a color picker in a form using HTML. As we know, a color picker is also known as a color-chooser. It is an interface in which a user can select a single color from multiple collections of background colors. When we submitted the color, the browser convert it
1 min read