In HTML, there isn't a specific text color tag. Instead, you use CSS (Cascading Style Sheets) to change the color of text. This can be done in several ways, such as inline styles, internal CSS, or external CSS. Below are the common methods to change text color in HTML:
1. Inline CSS (Using the style
Attribute)
You can directly apply styles to an HTML element using the style
attribute. For text color, use the color
property.
<p style="color: red;">This text is red!</p>
<h1 style="color: #00FF00;">This heading is green!</h1>
<span style="color: rgb(0, 0, 255);">This text is blue!</span>
2. Internal CSS (Using <style>
Tag)
You can define styles within a <style>
tag in the <head>
section of your HTML document. This allows you to reuse styles for multiple elements.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Text Color Example</title>
<style>
.r-cl-t {
color: red;
}
.b-cl-t {
color: blue;
}
.g-cl-t {
color: green;
}
</style>
</head>
<body>
<p class="r-cl-t">This text is red!</p>
<h1 class="b-cl-t">This heading is blue!</h1>
<span class="g-cl-t">This text is green!</span>
</body>
</html>
3. External CSS (Using a Separate .css
File)
For larger projects, it’s best to use an external CSS file. You link the CSS file to your HTML document using the <link>
tag in the <head>
section.
HTML File:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Text Color Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<p class="r-cl-t">This text is red!</p>
<h1 class="b-cl-t">This heading is blue!</h1>
<span class="g-cl-t">This text is green!</span>
</body>
</html>
CSS File (styles.css
):
.r-cl-t {
color: red;
}
.b-cl-t {
color: blue;
}
.g-cl-t {
color: green;
}
4. Using Color Values
You can specify colors in different formats:
- Color Names:
red
,blue
,green
, etc. - Hex Codes:
#FF0000
(red),#00FF00
(green),#0000FF
(blue). - RGB Values:
rgb(255, 0, 0)
(red),rgb(0, 255, 0)
(green),rgb(0, 0, 255)
(blue). - RGBA Values:
rgba(255, 0, 0, 0.5)
(red with 50% opacity).
Example with All Methods
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Text Color Examples</title>
<style>
.p-cl-t {
color: purple;
}
</style>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<!-- Inline CSS -->
<p style="color: red;">This text is red (inline CSS).</p>
<!-- Internal CSS -->
<p class="p-cl-t">This text is purple (internal CSS).</p>
<!-- External CSS -->
<p class="b-cl-t">This text is blue (external CSS).</p>
</body>
</html>
Key Points:
- Use the
color
property in CSS to change text color. - Inline styles are quick but not reusable.
- Internal and external CSS are better for larger projects.
- You can use color names, hex codes, RGB, or RGBA values to define colors.