#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
CSS3 Fundamentals

CSS3 Colours

CSS colours control the colour of text, backgrounds, borders, shadows and outlines. CSS offers several ways to write a colour: named keywords, hexadecimal codes, rgb(), hsl() and newer spaces such as oklch(). Each one describes the same thing differently, and choosing well makes your palette easier to adjust and your text easier to read.

Ways to write a colour

.examples {
  color: tomato;                    /* named keyword */
  color: #1e88e5;                   /* hex: red, green, blue */
  color: #1e88e580;                 /* hex with alpha (8 digits) */
  color: rgb(30 136 229);           /* modern space-separated syntax */
  color: rgb(30 136 229 / 50%);     /* with transparency */
  color: hsl(210 79% 51%);          /* hue, saturation, lightness */
  color: hsl(210 79% 51% / 0.5);
  color: oklch(62% 0.17 250);       /* perceptual lightness, chroma, hue */
  color: currentColor;              /* reuse the element's text colour */
  background: transparent;
}

Named colours are quick for experiments. Hex is compact and is what most design tools export. rgb() mirrors how screens mix light. The older comma syntax, rgba(30, 136, 229, 0.5), still works everywhere.

Why HSL is easier to reason about

HSL describes a colour the way people talk about it. Hue is an angle on the colour wheel (0 is red, 120 green, 240 blue), saturation is how vivid it is, and lightness runs from black at 0% to white at 100%. To make a darker hover shade, you lower the lightness and leave the rest alone. In hex, the same change means recalculating three pairs of digits.

oklch() goes a step further. Its lightness value matches how bright colours actually look to the eye, so two colours with the same lightness number look equally bright even if their hues differ. That makes it useful for building consistent palettes.

Storing a palette in custom properties

:root {
  --brand-h: 210;
  --brand: hsl(var(--brand-h) 79% 51%);
  --brand-dark: hsl(var(--brand-h) 79% 40%);
  --text: #1f2933;
  --surface: #ffffff;
}

.button {
  background: var(--brand);
  color: #fff;
  border: 2px solid var(--brand);
}

.button:hover {
  background: var(--brand-dark);
}

.button-outline {
  background: transparent;
  color: var(--brand);
  border: 2px solid currentColor;
}

Changing --brand-h re-tints every button at once. The outline button shows another handy keyword: currentColor makes the border follow the text colour, so you set the colour in one place.

Opacity versus transparent colours

A frequent mistake is using opacity: 0.5 to fade a background. opacity fades the whole element, including its text and children, which often makes the text hard to read. If you only want a see-through background, use a colour with alpha, such as background: rgb(0 0 0 / 40%), and keep the text fully opaque.

Contrast is part of choosing colours

Light grey text on white might look elegant on your monitor and be unreadable on a phone in sunlight. The WCAG accessibility guidelines ask for a contrast ratio of at least 4.5 to 1 for normal body text and 3 to 1 for large text. Browser developer tools show the ratio when you inspect a colour, so check it as you pick shades. Also avoid using colour alone to signal meaning; pair a red border on an invalid field with an error message.

What to take away

  • Use hex or rgb() when copying from a design file, and hsl() or oklch() when you need to adjust shades.
  • Keep your palette in custom properties on :root.
  • Use alpha in the colour, not opacity, to fade only a background.
  • Check text contrast before you commit to a colour.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course