1. css
  2. /properties
  3. /color

color

Definition

The color property in CSS sets the text color for an element. It can be used to change the color of text inside an element such as a heading, paragraph, or list item.

Examples

Setting the text color of all headings to red:

h1, h2, h3, h4, h5, h6 {
  color: red;
}

Setting the text color of all links to blue:


a {
 color: blue;
}

Setting the text color of a specific element with an id of "my-element" to green:

#my-element {
  color: green;
}

Setting the text color of all elements with a class of "highlight" to yellow:

.highlight {
  color: yellow;
}


Setting the text color of all elements of a specific type, such as "p" to gray:

```css
p {
  color: gray;
}

Values

NameHex CodeRGB Value
Black#000000rgb(0, 0, 0)
White#FFFFFFrgb(255, 255, 255)
Red#FF0000rgb(255, 0, 0)
Green#00FF00rgb(0, 255, 0)
Blue#0000FFrgb(0, 0, 255)
Yellow#FFFF00rgb(255, 255, 0)
Orange#FFA500rgb(255, 165, 0)
Purple#800080rgb(128, 0, 128)
Cyan#00FFFFrgb(0, 255, 255)
Pink#FFC0CBrgb(255, 192, 203)
Gray#808080rgb(128, 128, 128)

Best Practices

  • Ensure sufficient color contrast between text and background colors. This is especially important for users with visual impairments and other conditions. The Web Content Accessibility Guidelines (WCAG) provide guidelines for color contrast, which can be checked using online tools such as the WebAIM Contrast Checker.
  • Use semantic color names when possible, such as "red" or "green" instead of hex or RGB values. This makes your code more readable and maintainable.
  • Use color sparingly and only when it adds value. Overuse of color can be distracting and overwhelming for users.
  • Use color consistently throughout your website. This helps to create a cohesive visual style and makes it easier for users to navigate and understand your content.
  • Be mindful of cultural context. Different cultures may have different associations with certain colors, so be careful when using color to convey meaning or emotion.
  • Use color to highlight important information. Use color to draw attention to important elements such as call-to-action buttons or links.
  • Use color to create visual hierarchy. Use color to indicate the relative importance of different elements on the page, such as headings or body text.
  • Test your color choices on different devices and in different lighting conditions to ensure that they are legible and effective for all users.
  • Use CSS Variables to set the color property, that way you can easily change the color scheme of your website by modifying a single value.
  • Use accessibility friendly color, you can use color-adjust property to adjust the color according to the user preference.

Browser Compatibility

ChromeFirefoxSafariInternet ExplorerMicrosoft EdgeOpera
YesYesYesYesYesYes