askvity

How to change text color in HTML using CSS?

Published in CSS Styling 3 mins read

You can change text color in HTML using CSS by applying the color property to an HTML element.

Here's a breakdown of how to do it using different CSS methods:

Inline Styles

This method directly applies the style to the HTML element.

<p style="color: red;">This text is red.</p>
  • Advantages: Quick and easy for simple, one-off changes.
  • Disadvantages: Not recommended for larger projects due to poor maintainability and separation of concerns (mixing content and presentation).

Internal Styles (Embedded CSS)

You define styles within the <style> tag in the <head> section of your HTML document.

<!DOCTYPE html>
<html>
<head>
<style>
p {
  color: blue;
}
</style>
</head>
<body>

<p>This text is blue.</p>

</body>
</html>
  • Advantages: Keeps CSS within the HTML file, making it portable.
  • Disadvantages: Less maintainable than external stylesheets for larger projects.

External Stylesheets (Recommended)

This is the preferred method for managing styles in larger projects. You create a separate .css file and link it to your HTML document.

  1. Create a CSS file (e.g., styles.css):

    p {
      color: green;
    }
    
    .highlight {
        color: #FFD700; /* A vibrant gold */
    }
    
    #important-message {
        color: red;
    }
  2. Link the CSS file in your HTML document:

    <!DOCTYPE html>
    <html>
    <head>
      <link rel="stylesheet" href="styles.css">
    </head>
    <body>
    
    <p>This text is green.</p>
    <p class="highlight">This text is highlighted gold.</p>
    <p id="important-message">This text is an important message in red.</p>
    
    </body>
    </html>
  • Advantages: Best for maintainability, reusability, and separation of concerns. Changes to the CSS file apply to all linked HTML pages.
  • Disadvantages: Requires an extra file.

CSS Color Values

You can use various color values with the color property:

  • Named Colors: red, blue, green, black, white, etc. (limited selection)
  • Hexadecimal (Hex) Colors: #RRGGBB (e.g., #FF0000 for red, #00FF00 for green, #0000FF for blue). You can also use shorthand hex codes: #RGB (e.g. #F00 is the same as #FF0000)
  • RGB Colors: rgb(red, green, blue) where red, green, and blue are values from 0 to 255 (e.g., rgb(255, 0, 0) for red).
  • RGBA Colors: rgba(red, green, blue, alpha) where alpha is a value from 0.0 (fully transparent) to 1.0 (fully opaque) (e.g., rgba(255, 0, 0, 0.5) for semi-transparent red).
  • HSL Colors: hsl(hue, saturation, lightness) where hue is a value from 0 to 360, saturation and lightness are percentages.
  • HSLA Colors: hsla(hue, saturation, lightness, alpha) includes an alpha channel for transparency.

Specificity

CSS rules are applied based on their specificity. Inline styles have the highest specificity, followed by IDs, classes, and then element selectors. If multiple rules apply to the same element, the most specific rule will be used. For example, an inline style would override a style defined in an external stylesheet.

Related Articles