How to Change Font Color with CSS

Introduction

Changing the font color of text on a webpage is one of the easiest tasks in CSS. It helps improve the look of your content, highlight important details, and match the design to a brand's color palette. 

CSS offers several methods to change font color, including named colors, HEX codes, RGB, and HSL values. Understanding these options will give you more control over the design of your website.

In this tutorial, you'll learn how to change the font color of text using different CSS color formats. These examples will help you style text effectively for any type of project.

Problem Statement

Create a CSS code that:

  • Changes the font color of a specific text element.
  • Demonstrates multiple ways to assign colors (named, HEX, RGB, and HSL).

Example:

  • Input: A paragraph element with the text "Hello, World!".
  • Output: The text appears in different colors based on the specified CSS properties.

Solution Steps

  1. Target the Text Element: Identify the HTML element (e.g., paragraph or heading) whose font color you want to change.
  2. Apply the color Property: Use the color property in CSS to set the desired font color.
  3. Use Different Color Formats: Demonstrate how to set font colors using named colors, HEX codes, RGB, and HSL.

HTML Structure

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Change Font Color with CSS</title>
    <style>
        /* Step 2: Change font color using a named color */
        .color-name {
            color: red;
        }

        /* Step 3: Change font color using HEX value */
        .color-hex {
            color: #4CAF50; /* green */
        }

        /* Step 4: Change font color using RGB value */
        .color-rgb {
            color: rgb(255, 165, 0); /* orange */
        }

        /* Step 5: Change font color using HSL value */
        .color-hsl {
            color: hsl(240, 100%, 50%); /* blue */
        }
    </style>
</head>
<body>
    <h1>Change Font Color with CSS</h1>
    
    <!-- Step 1: Target the text element -->
    <p class="color-name">This text is red (named color).</p>
    <p class="color-hex">This text is green (HEX value).</p>
    <p class="color-rgb">This text is orange (RGB value).</p>
    <p class="color-hsl">This text is blue (HSL value).</p>
</body>
</html>

Explanation

Step 1: Target the Text Element

  • The text elements are simple <p> (paragraph) tags.
  • Each paragraph is given a class (color-name, color-hex, color-rgb, color-hsl) to show different ways of applying font colors.

Step 2: Change Font Color Using a Named Color

  • The first paragraph uses the class .color-name, which sets the font color to red.
  • Named colors like red, blue, and green are predefined in CSS.

Step 3: Change Font Color Using HEX Value

  • The second paragraph uses the class .color-hex, which sets the color to #4CAF50. HEX values are a common way to define colors using hexadecimal notation.

Step 4: Change Font Color Using RGB Value

  • The third paragraph uses the class .color-rgb, with the value rgb(255, 165, 0), which represents the color orange. RGB stands for Red, Green, and Blue and allows you to mix colors by specifying values between 0 and 255.

Step 5: Change Font Color Using HSL Value

  • The final paragraph uses the class .color-hsl to set the color to blue using the HSL format (hsl(240, 100%, 50%)). HSL stands for Hue, Saturation, and Lightness, providing an easy way to select colors based on the color wheel.

Output

Create an index.html file, add the above content and open it in the local browser. You will see the output:
How to Change Font Color with CSS

Conclusion

This tutorial shows how to change font color using CSS. By using named colors, HEX codes, RGB, and HSL values, you have several options to style text in any web project. Understanding these methods will help you improve the design and readability of your websites.

Comments