How to change the color of the text html

Learn how to change the color of text in HTML using the "color" attribute with this simple example.

Changing the color of text in HTML is a simple and effective way to add visual appeal to your web page. There are a few different methods for changing text color, and I'll walk you through each one.

Using the style attribute

One way to change the color of text is by using the "style" attribute within an HTML element. You can use this method to change the color of individual elements, such as paragraphs or headings. The "style" attribute allows you to apply inline CSS to the specific element.


<p style="color: blue;">This text is blue</p>
<h2 style="color: red;">This heading is red</h2>

Using CSS

Another way to change the color of text is by using CSS. This method is more flexible and allows you to apply the same color to multiple elements throughout your web page.


<style>
  p {
    color: green;
  }
  h2 {
    color: #ff0000;
  }
</style>

<p>This text is green</p>
<h2>This heading is red</h2>

Using inline CSS

You can also use inline CSS within the <style> tag to change the color of text. This method is similar to using the "style" attribute, but it allows you to apply the same color to multiple elements without repeating the style attribute for each one.


<style>
  .blue-text {
    color: blue;
  }
  .red-text {
    color: #ff0000;
  }
</style>

<p class="blue-text">This text is blue</p>
<h2 class="red-text">This heading is red</h2>

In conclusion, there are several methods for changing the color of text in HTML. Whether you choose to use the "style" attribute, CSS, or inline CSS, you can easily add a pop of color to your web page and enhance its visual appeal.

h

Answers (0)