Tillitsdone
down Scroll to discover

CSS Color Enhancing Web Design with Colorful Text

Learn about CSS color property, its use cases, and available options such as named colors, hexadecimal, RGB, and HSL values.

Simplify and enhance your web design.
thumbnail

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:

  1. Keyword Values
    color: currentcolor;
  2. Named Colors
    color: red;
    color: orange;
    color: tan;
    color: rebeccapurple;
  3. Hexadecimal Colors
    color: #090;
    color: #009900;
    color: #090a;
    color: #009900aa;
  4. 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%);
  5. 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%);
  6. 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%);
  7. 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:

Practical Tips

  1. Use High-Contrast Colors: Choose colors with a high contrast ratio for readability.
  2. Avoid Color-Only Indicators: Don’t rely solely on color to convey information. Use additional indicators like icons or text.
  3. Test on Different Devices: Check contrast on various devices and screen sizes.
  4. 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 Valuecanvastext
Applies toAll elements and text. It also applies to ::first-letter and ::first-line.
InheritedYes
Computed ValueComputed color
Animation TypeBy 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

BrowserVersionRelease Date
Google Chrome1.0December 2008
Mozilla Firefox1.0November 2004
Internet Explorer/Edge3.0August 1996
Opera3.5November 1998
Safari1.0June 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.

icons/css-4.svg CSS Blogs
CSS3 is the latest version of Cascading Style Sheets, offering advanced styling features like animations, transitions, shadows, gradients, and responsive design.
icons/logo-tid.svg

Talk with CEO

Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.