Skip to main content

CSS Colors and Backgrounds

CSS Colors

Written by Updated CSS Colors exercises

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 like rgb() or hsl(). 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

FormatExampleGood for
Namecolor: tomato;Quick tests; about 140 names exist
HEXcolor: #ff6347;Copying exact colors from a design tool
RGBcolor: rgb(255 99 71);Mixing red, green and blue values
HSLcolor: hsl(9 100% 64%);Making lighter and darker versions by changing one number
With transparencycolor: rgb(255 99 71 / 50%);Overlays and soft backgrounds

Where you can use them

PropertyWhat it colors
colorText
background-colorThe background
border-colorBorders
outline-colorFocus outlines
box-shadow, text-shadowShadows

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 values11211

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.
  • currentColor means "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

Check your answer to see it here.