- color
- Try it
- Syntax
- Values
- Accessibility concerns
- Formal definition
- Formal syntax
- Examples
- Making text red
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- CSS color Property
- Browser Support
- CSS Syntax
- Property Values
- More Examples
- Example
- Example
- Example
- Example
- Example
- Related Pages
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
- CSS Font Color – How to Style Text in HTML
- How to Set Text Color in HTML
- Named Colors
- Hexadecimal Colors (or just Hex Colors)
- RGB Colors
- HSL Colors
- Should You Use Named Colors, Hex Colors, RGB Colors, or HSL Colors to Assign Colors?
- Conclusion
color
The color CSS property sets the foreground color value of an element’s text and text decorations, and sets the currentcolor value. currentcolor may be used as an indirect value on other properties and is the default for other color properties, such as border-color .
Try it
For an overview of using color in HTML, see Applying color to HTML elements using CSS.
Syntax
/* Keyword values */ color: currentcolor; /* values */ color: red; color: orange; color: tan; color: rebeccapurple; /* values */ color: #090; color: #009900; color: #090a; color: #009900aa; /* values */ 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.3); color: rgb(34 12 64 / 60%); color: rgba(34.6 12 64 / 30%); /* values */ 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 100% 50% / 60%); color: hsla(30.2 100% 50% / 60%); /* values */ color: hwb(90 10% 10%); color: hwb(90 10% 10% / 0.5); color: hwb(90deg 10% 10%); color: hwb(1.5708rad 60% 0%); color: hwb(0.25turn 0% 40% / 50%); /* Global values */ color: inherit; color: initial; color: revert; color: revert-layer; color: unset;
The color property is specified as a single value.
Note that the value must be a uniform color . It can’t be a , which is actually a type of .
Values
Sets the color of the textual and decorative parts of the element.
Sets the color to the element’s color property value. However, if set as the value of color , currentcolor is treated as inherit .
Accessibility concerns
It is important to ensure that the contrast ratio between the color of the text and the background the text is placed over is high enough that people experiencing low vision conditions will be able to read the content of the page.
Color contrast ratio is determined by comparing the luminosity of the text and background color values. In order to meet current Web Content Accessibility Guidelines (WCAG), a ratio of 4.5:1 is required for text content and 3:1 for larger text such as headings. Large text is defined as 18.66px and bold or larger, or 24px or larger.
Formal definition
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 |
Formal syntax
Examples
Making text red
The following are all ways to make a paragraph’s text red:
p color: red; > p color: #f00; > p color: #ff0000; > p color: rgb(255, 0, 0); > p color: rgb(100%, 0%, 0%); > p color: hsl(0, 100%, 50%); > /* 50% translucent */ p color: #ff000080; > p color: rgba(255, 0, 0, 0.5); > p color: hsla(0, 100%, 50%, 0.5); >
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Jul 17, 2023 by MDN contributors.
Your blueprint for a better internet.
CSS color Property
Tip: Use a background color combined with a text color that makes the text easy to read.
Default value: | not specified |
---|---|
Inherited: | yes |
Animatable: | yes. Read about animatable Try it |
Version: | CSS1 |
JavaScript syntax: | object.style.color=»#0000FF» Try it |
Browser Support
The numbers in the table specify the first browser version that fully supports the property.
CSS Syntax
Property Values
Value | Description | Demo |
---|---|---|
color | Specifies the text color. Look at CSS Color Values for a complete list of possible color values. | Demo ❯ |
initial | Sets this property to its default value. Read about initial | |
inherit | Inherits this property from its parent element. Read about inherit |
More Examples
Example
Set the text color with a HEX value:
Example
Set the text color with an RGB value:
Example
Set the text color with an RGBA value:
Example
Set the text color with a HSL value:
Example
Set the text color with a HSLA value:
Related Pages
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.
CSS Font Color – How to Style Text in HTML
Kolade Chris
Setting text color on a website you’re building might be confusing at first. But in this article, you’ll learn how to do it.
How to Set Text Color in HTML
In CSS, the background-color property is pretty straightforward for setting the background color of anything.
So what if you want to set the foreground color of something on the page? Especially text, which under normal conditions you wouldn’t want to set a background color for.
There’s no foreground-color property in CSS, so what makes this possible is the color property.
In this article, I will walk you through how to set the color of text using the color property. We’ll also look at the various ways it takes values.
The color property takes values in 4 different ways: named color, hexadecimal color, RGB color, and HSL color. Let’s look at each one now.
Named Colors
As the name implies, you bring in the color property and apply the value by naming the color you want. This may be red, green, blue, orange, crimson, cyan, or any other named color. There are around 147 named colors recognized by browsers.
The basic syntax looks like this:
Hexadecimal Colors (or just Hex Colors)
Hex values are used to represent colors with a total of 6 characters. They start with the pound/number sign (#), then any number from 0 to 9, and finally any letter from A to F.
The first 2 values stand for red, the next two stand for green, and the last 2 represent blue. With hex values, there’s no limit to the shades of colors you can use.
RGB Colors
RGB stands for red, green, and blue. With RGB colosr, you specify the color in terms of how much red, green, and blue you want. All three are expressed with numbers between 0 and 255.
There is a type of RGB called rgba . The extra ‘a’ stands for alpha, which lets you specify the opacity of the color. It takes a value from 0.0 to 1.0 – 0.0 means 0% opacity, 0.5 means 50% opacity, and 1.0 means 100% opacity.
The basic syntax is rgba(amountOfRed, amountOfGreen, amountOfBlue, alpha) .
You can limit it to rgba(amountOfRed, amountOfGreen, amountOfBlue) if you don’t want an alpha value.
Here’s the syntax for the regular RGB values:
And here it is demonstrating the alpha value in action with 50% (0.5) opacity:
HSL Colors
HSL stands for hue, saturation, and lightness. It is another way of specifying color for text (and anything else that takes color) in CSS.
- Hue represents the color wheel in 360°. So, 0° is red, 120° is green and 240° is blue.
- Saturation is the amount of gray in the color, expressed as a percentage. 0% is the shade of gray and 100% is the color itself.
- Lightness is the amount of darkness and lightness in the color expressed as a percentage. 0% is black and 100% is white.
Just like the RGB colors, you can also set the opacity of the color. So, there’s also hsla.
The full basic syntax is hsl(colorDegree, saturationPercentage, lightnessPercentage, alpha) . You can limit it to hsl(colorDegree, saturationPercentage, lightnessPercentage) in case you don’t want an alpha value.
You can apply a particular opacity to the hsl color like this:
Should You Use Named Colors, Hex Colors, RGB Colors, or HSL Colors to Assign Colors?
One of the wonderful things about CSS is that there are multiple ways of doing the same thing. You’ve seen this by applying colors to text.
Since you can apply colors in 4 different ways, you must be wondering which is the best to use.
When you use named colors, you’re kind of limited in how far you can go in applying different shades of colors. Red, green, blue, yellow, or any other named color has a lot of variations you won’t get access to with named colors. You’ll only have access to around 147 predefined colors recognized by browsers.
Hexadecimal colors are very dynamic. They are the most commonly used among developers because your limit is your creativity. With hex colors, you can use various shades and even use a color no one has ever used.
RGB colors are as dynamic as hex colors. Apart from being able to specify how much red, green, and blue you want from 0 to 255, you can also set how transparent you want the color to be with the extra alpha value.
HSL is the most dynamic of all. You get to specify the exact color you want in degrees from 0 to 360 within the color wheel, set how saturated and dark you want it to be in percentages, and also set an opacity from 0.0 to 1.0.
So really, it’s up to you and your use case – and just how creative or specific you want to get.
Conclusion
Applying colors to text helps make your website more attractive to visitors. The right color combo can also help your content become more readable too.
In this article, you have learned how to apply colors to text with the 4 different kinds of values you can use with the color property.
Thank you for reading, and keep coding.