- Services
- Case Studies
- Technologies
- NextJs development
- Flutter development
- NodeJs development
- ReactJs development
- About
- Contact
- Tools
- Blogs
- FAQ
CSS Color Enhancing Web Design with Colorful Text
Simplify and enhance your web design.

Introduction
The color
property in CSS is essential for web design. It sets the text color and decorations like underlines. This property helps ensure your text is readable and matches your design.
You can use different values for the color
property, such as named colors, hexadecimal codes, RGB/RGBA, HSL/HSLA, and more. Understanding these values will help you make your website look great and be accessible to all users.
CSS color
Property
The color
property in CSS sets the text color within an element. It also affects text decorations like underlines. Using this property, you can make your text stand out, readable, and match your design.
Syntax
The color
property is simple to use. Here’s the basic syntax:
color: value;
The value
can be:
- Named colors (e.g.,
red
,blue
) - Hexadecimal values (e.g.,
#ff0000
) - RGB values (e.g.,
rgb(255, 0, 0)
) - RGBA values (e.g.,
rgba(255, 0, 0, 0.5)
) - HSL values (e.g.,
hsl(0, 100%, 50%)
) - HSLA values (e.g.,
hsla(0, 100%, 50%, 0.5)
) initial
(resets the property to its default value)inherit
(inherits the property from its parent element)
Property Values
The color
property accepts various values, giving you flexibility in setting text colors. Here are the main types:
- Keyword Values
color: currentcolor;
- Named Colors
color: red;color: orange;color: tan;color: rebeccapurple;
- Hexadecimal Colors
color: #090;color: #009900;color: #090a;color: #009900aa;
- RGB and RGBA Values
color: rgb(34, 12, 64);color: rgb(34, 12, 64, 0.6);color: rgba(34, 12, 64, 0.6);color: rgb(34 12 64 / 0.6);color: rgba(34 12 64 / 0.6);color: rgb(34.6 12 64 / 60%);color: rgba(34.6 12 64 / 60%);
- HSL and HSLA Values
color: hsl(30, 100%, 50%);color: hsl(30, 100%, 50%, 0.6);color: hsla(30, 100%, 50%, 0.6);color: hsl(30 100% 50% / 0.6);color: hsla(30 100% 50% / 0.6);color: hsl(30.2 100% 50% / 60%);color: hsla(30.2 100% 50% / 60%);
- HWB Values
color: hwb(90 10% 10%);color: hwb(90 10% 10% / 0.5);color: hwb(90deg 10% 10%);color: hwb(90.0deg 10% 10%);color: hwb(0.25turn 0% 40% / 50%);
- Global Values
color: inherit;color: initial;color: revert;color: revert-layer;color: unset;
Examples
Here are various ways to set text colors using the color
property:
Named Colors
color: red;color: green;color: blue;color: yellow;color: black;color: white;color: orange;color: purple;color: pink;color: brown;color: gray;color: cyan;color: magenta;color: lime;color: maroon;color: navy;color: olive;color: teal;color: aqua;color: silver;color: gold;
Hexadecimal Values
color: #FF0000; /* Red */color: #00FF00; /* Green */color: #0000FF; /* Blue */color: #FF000080; /* Red with 50% opacity */color: #00FF0040; /* Green with 25% opacity */color: #F00; /* Red */color: #0F0; /* Green */color: #00F; /* Blue */color: #F008; /* Red with 50% opacity */color: #0F04; /* Green with 25% opacity */
RGB and RGBA Values
color: rgb(255, 0, 0); /* Red */color: rgb(0, 128, 0); /* Green */color: rgb(0, 0, 255); /* Blue */color: rgba(255, 0, 0, 0.5); /* Red with 50% opacity */color: rgba(0, 128, 0, 0.7); /* Green with 70% opacity */color: rgba(0, 0, 255, 0.3); /* Blue with 30% opacity */color: rgb(100%, 0%, 0%); /* Red */color: rgb(0%, 50%, 0%); /* Green */color: rgb(0%, 0%, 100%); /* Blue */color: rgba(100%, 0%, 0%, 0.5); /* Red with 50% opacity */color: rgba(0%, 50%, 0%, 0.7); /* Green with 70% opacity */color: rgba(0%, 0%, 100%, 0.3); /* Blue with 30% opacity */
HSL and HSLA Values
color: hsl(0, 100%, 50%); /* Red */color: hsl(120, 100%, 50%); /* Green */color: hsl(240, 100%, 50%); /* Blue */color: hsla(0, 100%, 50%, 0.5); /* Red with 50% opacity */color: hsla(120, 100%, 50%, 0.7); /* Green with 70% opacity */color: hsla(240, 100%, 50%, 0.3); /* Blue with 30% opacity */
Global Values
color: inherit;color: initial;color: revert;color: revert-layer;color: unset;
Practical Examples
Using Named Colors
<!DOCTYPE html><html><head> <title>CSS Named Colors Example</title> <style> h1 { color: black; } p { font-size: 20px; color: green; } .gfg1 { font-size: 20px; color: red; } .gfg2 { font-size: 20px; color: blue; } </style></head><body> <h1>CSS Color Property</h1> <p>Website: A computer science portal</p> <p class="gfg1">Website: A computer science portal</p> <p class="gfg2">Website: A computer science portal</p></body></html>
Using Hexadecimal Values
<!DOCTYPE html><html><head> <title>CSS Hexadecimal Values Example</title> <style> body { background-color: rgb(200, 200, 200); } h1 { color: #000000; /* Black */ } p { color: #00AA00; /* Green */ } .red { color: #FF0000; /* Red */ } .blue { color: #0000FF; /* Blue */ } .transparent { color: #FF000080; /* Red with 50% opacity */ } </style></head><body> <h1>CSS Hexadecimal Values</h1> <p>#00AA00 - Green</p> <p class="red">#FF0000 - Red</p> <p class="blue">#0000FF - Blue</p> <p class="transparent">#FF000080 - Red with 50% opacity</p></body></html>
Using RGB and RGBA Values
<!DOCTYPE html><html><head> <title>CSS RGB and RGBA Values Example</title> <style> h1 { color: rgb(255, 0, 0); } p { font-size: 20px; color: rgb(0, 128, 0); } .gfg1 { font-size: 20px; color: rgba(0, 0, 255, 0.5); } .gfg2 { font-size: 20px; color: rgba(255, 165, 0, 0.7); } </style></head><body> <h1>CSS RGB/RGBA Property</h1> <p>Website: A computer science portal</p> <p class="gfg1">Website: A computer science portal</p> <p class="gfg2">Website: A computer science portal</p></body></html>
Using HSL and HSLA Values
<!DOCTYPE html><html><head> <title>CSS HSL and HSLA Values Example</title> <style> h1 { color: hsl(0, 100%, 50%); /* Red */ } p { color: hsl(120, 100%, 50%); /* Green */ } .blue { color: hsl(240, 100%, 50%); /* Blue */ } .transparent { color: hsla(0, 100%, 50%, 0.5); /* Red with 50% opacity */ } </style></head><body> <h1>CSS HSL and HSLA Values</h1> <p>HSL(120, 100%, 50%) - Green</p> <p class="blue">HSL(240, 100%, 50%) - Blue</p> <p class="transparent">HSLA(0, 100%, 50%, 0.5) - Red with 50% opacity</p></body></html>
Using initial
and inherit
Values
Using initial
<!DOCTYPE html><html><head> <title>CSS initial Value Example</title> <style> body { color: blue; } h1 { color: initial; /* Resets to default color */ } p { color: initial; /* Resets to default color */ } </style></head><body> <h1>CSS Color Property with initial Value</h1> <p>This paragraph uses the initial value to reset the text color to the default.</p></body></html>
Using inherit
<!DOCTYPE html><html><head> <title>CSS inherit Value Example</title> <style> body { color: blue; } h1 { color: inherit; /* Inherits color from body */ } p { color: inherit; /* Inherits color from body */ } </style></head><body> <h1>CSS Color Property with inherit Value</h1> <p>This paragraph uses the inherit value to match the text color of its parent element.</p></body></html>
Ensuring Accessibility
It’s crucial to make your web content accessible to all users, including those with low vision. The color contrast ratio between text and background is key to achieving this.
Importance of Color Contrast
When text and background colors have insufficient contrast, it can be hard for users with low vision to read. The Web Content Accessibility Guidelines (WCAG) recommend specific contrast ratios:
- Normal Text: A contrast ratio of at least 4.5:1.
- Large Text: A contrast ratio of at least 3:1. Large text is defined as 18.66px and bold, or 24px and larger.
Tools for Checking Contrast
Several tools can help you check and adjust color contrast:
- WebAIM Color Contrast Checker: Input foreground and background colors to check their contrast ratio.
- MDN Understanding WCAG: Offers explanations and examples of WCAG guidelines.
- Understanding Success Criterion 1.4.3 | W3C Understanding WCAG 2.0: Detailed information on the color contrast success criterion.
Practical Tips
- Use High-Contrast Colors: Choose colors with a high contrast ratio for readability.
- Avoid Color-Only Indicators: Don’t rely solely on color to convey information. Use additional indicators like icons or text.
- Test on Different Devices: Check contrast on various devices and screen sizes.
- Consider Different Lighting Conditions: Test your design under different lighting conditions.
Formal Definition of the color
Property
The color
property sets the foreground color of an element’s text and text decorations. It also sets the currentcolor
value, which can be used for other properties.
Initial Value | canvastext |
---|---|
Applies to | All elements and text. It also applies to ::first-letter and ::first-line . |
Inherited | Yes |
Computed Value | Computed color |
Animation Type | By computed value type |
Related Properties
The color
property is closely related to several other CSS properties that control the appearance of text and other elements:
background-color
: Sets the background color of an element.background-color: #ffffff;border-color
: Sets the color of an element’s border.border-color: #000000;outline-color
: Sets the color of an element’s outline.outline-color: #ff0000;text-decoration-color
: Sets the color of text decorations, such as underlines.text-decoration-color: #00ff00;text-emphasis-color
: Sets the color of emphasis marks.text-emphasis-color: #ff00ff;text-shadow
: Adds shadow to text, with options for color, blur, and offset.text-shadow: 2px 2px 5px #000000;caret-color
: Sets the color of the text insertion caret.caret-color: #00ff00;column-rule-color
: Sets the color of the rule (line) between columns in a multi-column layout.column-rule-color: #0000ff;print-color-adjust
: Controls the use of color in print media.print-color-adjust: economy;
Example of Related Properties
Here’s how you can use these related properties in CSS:
/* Set text color */color: #333333;
/* Set background color */background-color: #ffffff;
/* Set border color */border-color: #000000;
/* Set outline color */outline-color: #ff0000;
/* Set text decoration color */text-decoration-color: #00ff00;
/* Set text emphasis color */text-emphasis-color: #ff00ff;
/* Add text shadow */text-shadow: 2px 2px 5px #000000;
/* Set caret color */caret-color: #00ff00;
/* Set column rule color */column-rule-color: #0000ff;
/* Control print color adjustment */print-color-adjust: economy;
Browser Support
The color
property is widely supported across all major web browsers. Here’s a summary of browser compatibility:
Supported Browsers
- Google Chrome: Supported since version 1.0 (December 2008).
- Mozilla Firefox: Supported since version 1.0 (November 2004).
- Internet Explorer/Edge: Supported since version 3.0 (August 1996).
- Opera: Supported since version 3.5 (November 1998).
- Safari: Supported since version 1.0 (June 2003).
Compatibility Table
Browser | Version | Release Date |
---|---|---|
Google Chrome | 1.0 | December 2008 |
Mozilla Firefox | 1.0 | November 2004 |
Internet Explorer/Edge | 3.0 | August 1996 |
Opera | 3.5 | November 1998 |
Safari | 1.0 | June 2003 |
Conclusion
The color
property in CSS is a powerful tool for controlling the appearance of text. By understanding its specifications, related properties, and browser compatibility, you can ensure your web content is visually appealing and accessible. Use named colors, hexadecimal values, RGB/RGBA values, HSL/HSLA values, and global values like initial
and inherit
to create a consistent and user-friendly experience.
By following best practices and adhering to web standards, you can create a professional and inclusive web experience for all users.






Talk with CEO
We'll be right here with you every step of the way.
We'll be here, prepared to commence this promising collaboration.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.