- Home
- /
- Tutorials
- /
- CSS Tutorial
- /
- CSS Colors
CSS Colors and Backgrounds
CSS Colors
Choose CSS colors for text, backgrounds, borders, and interface states while preserving contrast, inheritance, and consistent design tokens.
CSS can color almost anything: text, backgrounds, borders, outlines, shadows. You can write a color as a name like
red, a HEX code like#ff0000, or a function likergb()orhsl(). They all describe the same colors in different ways.
Each format has its own lesson: CSS HEX Colors and CSS RGB Colors, plus the newer formats in Modern Colors.
CSS Colors Syntax
css
selector {
color: value;
}CSS Colors Example
css
p {
color: red;
}That makes all paragraph text red.
Color formats
| Format | Example | Good for |
|---|---|---|
| Name | color: tomato; | Quick tests; about 140 names exist |
| HEX | color: #ff6347; | Copying exact colors from a design tool |
| RGB | color: rgb(255 99 71); | Mixing red, green and blue values |
| HSL | color: hsl(9 100% 64%); | Making lighter and darker versions by changing one number |
| With transparency | color: rgb(255 99 71 / 50%); | Overlays and soft backgrounds |
Where you can use them
| Property | What it colors |
|---|---|
| color | Text |
| background-color | The background |
| border-color | Borders |
| outline-color | Focus outlines |
| box-shadow, text-shadow | Shadows |
Color must not carry meaning alone
Use color together with text, icons, patterns, or shape changes when communicating errors, success, selection, or required fields. Test foreground and background combinations in every interactive state, including focus and disabled states. Custom properties with semantic names such as --color-text-muted are easier to theme safely than tokens named after a particular hue.
Example
CSS Colors Complete Example
html
<!DOCTYPE html>
<html>
<head>
<title>CSS Colors Example</title>
<style>
body {
background-color: lightgray;
}
h1 {
color: blue;
}
p {
color: green;
}
div {
border: 2px solid red;
padding: 10px;
}
</style>
</head>
<body>
<h1>CSS Colors</h1>
<p>This is a paragraph with green text.</p>
<div>This box has a red border.</div>
</body>
</html>The page has a light grey background, a blue heading, green text and a box with a red border.
Browser Support
Feature | Chrome | Edge | Firefox | Safari |
|---|---|---|---|---|
| CSS color values | 1 | 12 | 1 | 1 |
Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8.
Good to know
- Check contrast between text and background. Aim for at least 4.5:1 for normal text.
- Store the colors you reuse as variables, like
--brand: #0f766e;, so you can change them in one place. currentColormeans "the current text color", handy for borders and icons that should match the text.
CSS Colors exercises
Color the text
Make the title red.
The HTML your code works on
<h2 class="title">Sale</h2>
<p class="text">All shoes half price.</p>
<div class="box">Box</div>Result
