HTML Font Color

By utilizing HTML tools, we can enhance the appearance of our web content through design and styling. Text formatting is a fundamental aspect of web development, enabling us to customize the look of text within our content. Modifying font color is a key method for altering text appearance. Below is the essential syntax for implementing font color changes in HTML.

Syntax:

Example

<element style="color: color_value;">

  Text content goes here

</element>

In the above code:

  • The <element> tag represents all the HTML elements such as <h1>, <p>, or <span>.
  • style="color: color_value;" is the inline property of the CSS in which we can provide the font color to the text of the web content. We can use the color name in the form of color names, hexadecimal color codes, RGB values, or HSL values.

Let's explore the various techniques that can be utilized for setting the color of text in CSS.

Using Color Names

When working with HTML, we can specify the color of text using color names. These names are simple to recall and apply. Below are a few illustrations.

Example

<p style="color: red;">This text is red.</p>

<p style="color: blue;">This text is blue.</p>

<p style="color: green;">This text is green.</p>

Let's delve into a program and thoroughly examine the color attribute.

Example 1:

Example

<!DOCTYPE html>

<html lang="en">

<head>

 <meta charset="UTF-8">

 <meta name="viewport" content="width=device-width, initial-scale=1.0">

 <title>Wider Rainbow with Different Shapes</title>

 <style>

  .rainbow {

   width: 400px; /* Increased container width */

   height: 80px; /* Increased container height */

   position: relative;

   overflow: hidden;

  }



  .rainbow div {

   height: 100%;

   position: absolute;

  }



  .red { background-color: red; border-radius: 50% 50% 0 0; }

  .orange { background-color: orange; }

  .yellow { background-color: yellow; }

  .green { background-color: green; }

  .blue { background-color: blue; }

  .indigo { background-color: indigo; }

  .violet { background-color: violet; border-radius: 0 0 50% 50%; }

 </style>

</head>

<body>

 <div class="rainbow">

  <div class="red" style="width: 100%;"></div>

  <div class="orange" style="width: 85%;"></div>

  <div class="yellow" style="width: 70%;"></div>

  <div class="green" style="width: 55%;"></div>

  <div class="blue" style="width: 40%;"></div>

  <div class="indigo" style="width: 25%;"></div>

  <div class="violet" style="width: 10%;"></div>

 </div>

</body>

</html>

Output:

Explanation:

Within the provided code snippet, a set of seven div elements have been generated, each distinguished by a unique background hue specified by color names such as red, orange, yellow, green, blue, indigo, and violet. Employing the border-radius attribute imparts a circular shape to these div elements, resulting in a rainbow-themed visual effect. By applying the inline-block display style, the rainbow colors are positioned sequentially in a horizontal arrangement.

Hexadecimal Color Codes

Utilizing hexadecimal values to specify font colors is widely favored in the developer community. Each hexadecimal color is denoted by a # symbol followed by a six-digit code. This code offers an extensive spectrum of color options. Here are a few illustrations:

Example

<p style="color: #FF5733;">This text is a shade of orange.</p>

<p style="color: #3498DB;">This text is a shade of blue.</p>

<p style="color: #27AE60;">This text is a shade of green.</p>

Example 2:

Let's analyze a program in detail and delve into the intricacies of the color attribute.

Code:

Example

<!DOCTYPE html>

<html lang="en">

<head>

 <meta charset="UTF-8">

 <meta name="viewport" content="width=device-width, initial-scale=1.0">

 <title>Document</title>

 <style>

  body {

    background: linear-gradient(to right, #ff0000, #ff7f00, #ffff00, #00ff00, #0000ff, #4b0082, #9400d3);

    height: 100vh;

    margin: 0;

    display: flex;

    justify-content: center;

    align-items: center;

  }



  h1 {

    font-size: 3rem;

    color: white;

    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);

  }

</style>

</head>

<body>

 <h1>Welcome to the C# Tutorial!</h1>

</body>

</html>

Output:

Explanation:

Within the provided code snippet, the linear-gradient attribute has been implemented to style the background of the body component. By setting the direction to right, the gradient is configured to flow horizontally from left to right, generating a rainbow-like visual effect. Seven hexadecimal color values have been designated to represent each color of the rainbow spectrum, including red, orange, yellow, green, blue, indigo, and violet.

RGB and RGBA Values

Another way to specify colors in CSS is by utilizing the color property using RGB or RGBA values. RGB values denote the amount of red, green, and blue present, ranging from 0 to 255. Conversely, RGBA values include an alpha value, determining the transparency level (0 for fully transparent, 1 for fully opaque). This enables us to create varying levels of transparency when defining colors. Below are a few illustrations for better understanding.

Example

<p style="color: rgb(255, 0, 0);">This text is red.</p>

<p style="color: rgba(0, 128, 255, 0.5);">This text is semi-transparent blue.</p>

Example 3:

This serves as an illustration of how RGB color can be implemented.

Code:

Example

<!DOCTYPE html>

<html lang="en">

<head>

 <meta charset="UTF-8">

 <meta name="viewport" content="width=device-width, initial-scale=1.0">

 <title>Document</title>

 <style>

  .colored-box {

    width: 200px;

    height: 200px;

    background-color: rgb(255, 0, 0); /* Red color using RGB values */

  }

</style>

</head>

<body>

 <div class="colored-box"></div>

 

</body>

</html>

Output:

Explanation:

Within the provided code snippet, an HTML element has been established as a colored box with a class designation of "colored-box." Subsequently, within the CSS stylesheet, the .colored-box class is specified, and the background-color attribute is utilized to designate a red hue through RGB values. The RGB notation rgb(255, 0, 0) signifies the color red, characterized by the highest intensity of red (255), absence of green (0), and lack of blue (0). The resultant display showcases a square measuring 200x200 pixels, exhibiting a red background tint.

Example 4:

This serves as a demonstration of how RGBA color can be implemented.

Code:

Example

<!DOCTYPE html>

<html lang="en">

<head>

 <meta charset="UTF-8">

 <meta name="viewport" content="width=device-width, initial-scale=1.0">

 <title>Document</title>

 <style>

  .semi-transparent-box {

    width: 200px;

    height: 200px;

    background-color: rgba(0, 0, 255, 0.5); /* Blue color with 50% transparency */

  }

</style>

</head>

<body>

 <div class="semi-transparent-box"></div>

 

</body>

</html>

Output:

Explanation:

The code snippet provided showcases an HTML element called <div> with the specified class semi-transparent box. Subsequently, the .semi-transparent-box class is established, where the background-color attribute is utilized to designate a blue hue employing the RGBA color model (0, 0, 255, 0.5). Here, the RGBA values (0, 0, 255, 0.5) correspond to a shade of blue characterized by maximal intensity in the red and blue color channels (0), absence of green (0), and a transparency level of 50% (0.5).

HSL and HSLA Values

Various techniques are available to specify the font color on a webpage. In the context of HSL, the hue denotes the color, saturation determines the vividness, and lightness influences the luminance. HSLA introduces an extra alpha parameter to indicate transparency. Below are a few illustrations.

Example

<p style="color: hsl(0, 100%, 50%);">This text is red.</p>

<p style="color: hsla(210, 100%, 50%, 0.5);">This text is semi-transparent blue.</p>

Example 5:

This is a demonstration showcasing the utilization of HSL color model.

Code:

Example

<!DOCTYPE html>

<html lang="en">

<head>

 <meta charset="UTF-8">

 <meta name="viewport" content="width=device-width, initial-scale=1.0">

 <title>Document</title>

 <style>

  .hsl-box {

    width: 200px;

    height: 200px;

    background-color: hsl(120, 100%, 50%); /* Green color using HSL values */

  }

</style>

</head>

<body>

 <div class="hsl-box"></div>

 

</body>

</html>

Output:

Explanation:

Within the provided code snippet, an HTML <div> tag is present with a designated class of hsl-box. Subsequently, it is necessary to establish the .hsl-box class and employ the background-color attribute to specify a green background utilizing hsl(120, 100%, 50%). Within the HSL color model, the initial value (such as 120 in this instance) denotes the hue, dictating the color type (e.g., 0 for red, 120 for green, 240 for blue). The second value (100% in this case) signifies the saturation, regulating the color's intensity or richness (100% for fully saturated, 0% for grayscale). The third value (50% in this scenario) denotes the lightness, influencing the color's brightness or darkness (0% for black, 100% for white).

Example 6:

This is a demonstration showcasing the utilization of an HSLA color.

Code:

Example

<!DOCTYPE html>

<html lang="en">

<head>

 <meta charset="UTF-8">

 <meta name="viewport" content="width=device-width, initial-scale=1.0">

 <title>Document</title>

 <style> after 

  .hsla-box {

    width: 200px;

    height: 200px;

    background-color: hsla(210, 100%, 50%, 0.5); /* Semi-transparent blue color using HSLA values */

  }

</style>

</head>

<body>

 <div class="hsla-box"></div>

 

</body>

</html>

Output:

Explanation:

Within the provided code snippet, an HTML <div> tag is present with the specified class of hsla-box. Following this, it is necessary to declare the hsla-box class and utilize the background-color attribute to establish a semi-transparent blue background using hsla(210, 100%, 50%, 0.5). The hsla function requires four parameters. In this context, the initial parameter (210 in the given instance) denotes the hue, defining the color type. Subsequently, the second parameter (100% in this case) signifies the saturation, which regulates the color's strength. The third parameter (50% in this scenario) denotes the lightness, which governs the color's luminosity. Lastly, the fourth parameter (0.5 here) symbolizes the alpha channel, managing the color's opacity, ranging between 0 (completely transparent) and 1 (fully opaque).

Combining Font Color with Other Styles

To create visually appealing styles, it is essential to merge various style attributes. For instance, adjusting the font size, font family, and incorporating diverse CSS properties can enhance the typography aesthetics on your websites.

Here are some examples,

Example

<h1 style="color: #FF5733; font-size: 36px; font-family: 'Arial', sans-serif; text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);">Welcome to our tutorial</h1>

Example 7:

This serves as an instance showcasing the integration of font color with additional styles.

Code:

Example

<!DOCTYPE html>

 <html lang="en">

 <head>

  <meta charset="UTF-8">

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>Document</title>

  <style>

   h1 {

     color: #ff5733; /* Hexadecimal color code for a reddish-orange color */

     font-size: 36px;

     font-weight: bold;

     text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);

   }

 </style>

 </head>

 <body>

  <h1>Welcome to our tutorial</h1>

  

 </body>

 </html>

Output:

Explanation:

Within the provided code snippet, an HTML <h1> is utilized to display the message "Welcome to our tutorial." Following this, the color attribute is employed to specify a reddish-orange hue using the hexadecimal value #ff5733. Additionally, adjustments are made to the font-size setting to increase the text size to 36 pixels, the font weight is modified to bold, and a text-shadow effect is applied to enhance the legibility of the text.

Input Required

This code uses input(). Please provide values below: