CSS Colors


HTML elements colours are controlled using the CSS Color attribute. This attribute is used to control the colour of the font, background, etc.

There are several ways to define an element's colour:

  • Internal Color.
  • Format RGB.
  • Format RGBA.
  • The hexadecimal system.
  • HSL.
  • HSLA.

1. Internal Color :

These predetermined colours are utilised as indicated by their name. Red, blue, green, etc. are a few examples.

Syntax

Example

Preview

2. RGB Format :

By specifying the R, G, and B values range between 0 and 255, an HTML element's colours can be defined using the RGB(Red, Green, Blue) format. For instance, the RGB values of the colours red are (255, 0, 0), green are (0, 255, 0), and blue are (0, 0, 255).

Syntax

Example

Preview

3. RGBA Format :

Although the RGBA format is similar to the RGB, it differs in that it includes the letter A (Alpha), which designates the transparency of elements. The range of alpha values is 0.0 to 1.0, with 0.0 denoting complete transparency and 1.0 denoting complete lack of transparency.

Syntax

Example

Preview

4. The Hexadecimal System :

The # symbol precedes the first six characters in the hexadecimal format, each of which ranges from 0 to F. Examples include the colours red (FF0000), green (00FF00), blue (0000FF), etc.

Syntax

Example

Preview

5. HSL :

HSL is an acronym for hue, saturation, and lightness. The cylindrical coordinate system is used in this format.

Hue: The hue of the colour wheel is its intensity. Its value ranges from 0 to 360, with 0 being red, 120 denoting green, and 240 denoting blue.  
Saturation: It uses a percentage number, with 100% denoting that the area is totally saturated and 0% denoting that the area is completely unsaturated (gray).  
Lightness: It is measured as a percentage, with 100% being white and 0% representing black.

Syntax

Example

Preview

6. HSLA :

While the HSLA colour property is identical to the HSL property, it differs in that it contains the letter A (Alpha), which designates an element's transparency. The range of alpha values is 0.0 to 1.0, with 0.0 denoting complete transparency and 1.0 denoting complete lack of transparency.

Syntax

Example

Preview

FAQs

Yes, you can animate color changes in CSS using the transition property. You can set the transition property for the element to specify the duration and timing function of the animation. Then, you can change the color of the element using CSS :hover or JavaScript. For example, you can create a button that changes color when hovered over by setting the background-color property in both the default and hover states, and then specifying the transition property.

To adjust text color in CSS, you can use the color property and specify a color value. For example, to set the text color to red, you would use:

color: red;	

To adjust link colors, you can use the a selector and set the color property to the desired color value. For example, to set link colors to blue, you would use:

a {
  color: blue;
}

You can also adjust link colors for specific states, such as when the link is being hovered over or is active. To do this, you can use the :hover and :active pseudo-classes, like so:

a:hover {
  color: green;
}

a:active {
  color: purple;
}

Ensuring color accessibility for users with visual impairments is essential. Here are a few tips to keep in mind:

  • Use high-contrast color combinations between the text and background.
  • Avoid using color alone to convey important information.
  • Use descriptive text alternatives for non-text content like images.
  • Use clear and easy-to-read fonts.
  • Provide sufficient color contrast for links and buttons.
  • Test color accessibility using tools like WebAIM's Contrast Checker.

By following these tips, you can ensure that your website is accessible to all users, regardless of their visual abilities.