CSS Colors

Text, background and border colours.

✍️ 3 exercises + code challenge in this lesson

CSS colours can be set for text (color), backgrounds (background-color) and borders (border-color).

Colour formats

  • Names: red, teal, navy
  • HEX: #2563EB (or the short form #09f)
  • RGB / RGBA: rgb(37 99 235), rgba(37, 99, 235, 0.5)
  • HSL / HSLA: hsl(221 83% 53%)

The opacity property makes a whole element (including its content) transparent.

Key Takeaways: CSS Colors

Mastering CSS Colors helps you write clean and reliable CSS code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

CSS Colors Code Example

CSS
<style>
  div { padding: 12px; margin: 6px 0; border-radius: 8px; border: 3px solid; }
  .a { color: white; background-color: #2563EB; border-color: #1D4ED8; }
  .b { color: #071A2F; background-color: rgba(20, 184, 166, 0.3); border-color: teal; }
  .c { color: white; background-color: hsl(16, 77%, 52%); border-color: hsl(16, 77%, 35%); }
  .d { background: #071A2F; color: white; opacity: 0.5; border-color: transparent; }
</style>

<div class="a">HEX colours</div>
<div class="b">RGBA background</div>
<div class="c">HSL colours</div>
<div class="d">opacity: 0.5</div>
Free sign-in required · Google login available

CSS Colors Practice Exercises

Check what you learned in “CSS Colors”.

/ 3 solved
Fill in the blank

Set the text colour with an RGB value for pure red.

p { color: ; }
✓ Correct! Not quite. Try again.
Multiple choice

Which property sets the background colour?

✓ Correct! Not quite. Try again.
Code challenge

Give the box a background colour of #2563eb and white text.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services