Aral Code

Colors & the Box Model Lesson 5 of 33

CSS Colors — Names, HEX, RGB, HSL and Opacity

Color text and backgrounds with CSS using color names, HEX, RGB and HSL, add transparency, and build a simple color palette for your website.

Color is usually the first thing people notice on a website. In CSS, two properties do most of the coloring:

  • color — the color of the text
  • background-color — the color behind an element
Example Text color and background color
<style>
  .banner {
    color: white;
    background-color: #0f5c4d;
    padding: 16px;
  }
</style>
<div class="banner">Enrollment is now open for SY 2026–2027!</div>

Ways to write a color

CSS accepts colors in several formats. They all produce the same kinds of colors; they're just different ways of writing them.

1. Color names

About 140 English names: red, navy, gold, tomato, teal, crimson, whitesmoke… Easy to remember, but limited.

2. HEX codes

A # followed by six characters (0–9, A–F): two each for red, green and blue. #000000 is black, #ffffff is white, #ff0000 is red. Designers use HEX more than anything else. A short form like #f00 works when the pairs repeat.

3. RGB

rgb(red, green, blue), with each number from 0 to 255. rgb(255, 0, 0) is red. It's the same idea as HEX, written in ordinary numbers.

4. HSL

hsl(hue, saturation, lightness) describes color the way people think about it:

  • Hue — the color itself, as an angle on a color wheel from 0 to 360: 0 is red, 120 is green, 240 is blue.
  • Saturation — how strong the color is, from 0% (gray) to 100% (full color).
  • Lightness — from 0% (black) through 50% (normal) to 100% (white).
Example The same red, four ways
<style>
  div { padding: 10px; margin: 6px 0; color: white; font-family: monospace; font-size: 16px; }
</style>
<div style="background-color: red;">red</div>
<div style="background-color: #ff0000;">#ff0000</div>
<div style="background-color: rgb(255, 0, 0);">rgb(255, 0, 0)</div>
<div style="background-color: hsl(0, 100%, 50%);">hsl(0, 100%, 50%)</div>

Why HSL is great for making shades

With HSL, you can make lighter and darker versions of a color by changing only the last number. This is very handy for hover effects and color palettes:

Example Shades of one hue
<style>
  .s { padding: 10px; margin: 4px 0; font-family: monospace; }
</style>
<div class="s" style="background: hsl(160, 70%, 15%); color: white;">hsl(160, 70%, 15%)</div>
<div class="s" style="background: hsl(160, 70%, 30%); color: white;">hsl(160, 70%, 30%)</div>
<div class="s" style="background: hsl(160, 70%, 45%);">hsl(160, 70%, 45%)</div>
<div class="s" style="background: hsl(160, 70%, 70%);">hsl(160, 70%, 70%)</div>
<div class="s" style="background: hsl(160, 70%, 90%);">hsl(160, 70%, 90%)</div>

Try changing every 160 to 220 (blue) or 30 (orange). The whole set changes color but keeps the same pattern of light and dark.

Transparency

Add a fourth value, the alpha, to make a color see-through. It goes from 0 (invisible) to 1 (solid):

background-color: rgba(0, 0, 0, 0.5);     /* half-transparent black */
background-color: hsla(160, 70%, 30%, 0.2);
background-color: #0f5c4d80;              /* HEX with alpha: the last 2 characters */
Example Text over a photo-like background
<style>
  .hero {
    background: linear-gradient(135deg, #f2b33d, #e2552d);
    padding: 40px 20px;
    text-align: center;
  }
  .hero p {
    background-color: rgba(0, 0, 0, 0.55);
    color: white;
    display: inline-block;
    padding: 12px 20px;
    border-radius: 8px;
    font-size: 20px;
  }
</style>
<div class="hero"><p>Pista sa Barangay San Isidro — May 15!</p></div>

opacity vs. alpha

The opacity property makes a whole element see-through, including its text and everything inside it. An alpha color only affects that one color.

Example opacity vs. rgba
<style>
  .box { background: #0038A8; color: white; padding: 14px; margin: 8px 0; font-size: 18px; }
</style>
<div class="box" style="opacity: 0.4;">opacity: 0.4 — the text fades too</div>
<div class="box" style="background: rgba(0, 56, 168, 0.4); color: black;">rgba background — text stays solid</div>

currentColor

The special value currentColor means "whatever the text color is". It keeps borders and icons matched to the text automatically:

.tag { color: #b4372b; border: 2px solid currentColor; }

Building a color palette

Good-looking sites don't use dozens of colors. A simple, reliable palette:

  • 1 main color (brand) — headings, buttons, links
  • 1 accent color — highlights, sale tags, "new" badges
  • Neutrals — near-black for text, light gray or off-white for backgrounds
Example A simple 4-color palette
<style>
  body { background: #fbfaf7; color: #1d2321; font-family: Arial, sans-serif; }
  h2 { color: #0f5c4d; }
  .badge { background: #f2b33d; color: #1d2321; padding: 2px 8px; border-radius: 6px; font-size: 14px; }
  button { background: #0f5c4d; color: white; border: none; padding: 10px 16px; border-radius: 8px; font-size: 16px; }
</style>
<h2>Bagong Labas: Ube Pandesal <span class="badge">NEW</span></h2>
<p>Soft, fluffy and filled with real ube halaya. Baked fresh every morning at 5 AM.</p>
<button>Order now</button>

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Which property changes the color of text?
2. In hsl(120, 100%, 50%), what does 120 control?
3. What does rgba(0, 0, 0, 0.5) produce?
4. You want a box's background to be see-through but its text to stay solid. What should you use?

By Sir Reyel · Last updated 2026-12-04