CSS Colors

Color in our designs is not just decoration - it represents emotion, identity, and user experience. In CSS, color is essential to successful, engaging and accessible designs. Colors can accent lots of elements in your web pages - backgrounds, borders, text, links, buttons, and more!

Why CSS Colors Matter

Colors enrich aesthetic organization, evoke brand identity, and facilitate user engagements. When colors have been selected successfully, usability is enhanced, and users will remember your site.

CSS Properties That Accept Colors

There are several CSS properties where you can apply colors. Some common ones include:

Ways to Define Colors in CSS

CSS offers multiple ways to define colors, providing flexibility based on your requirements:

Color Names

There are over 140 predefined color names in CSS.


    h1 {
       color: red ;
       }
    div {
       background-color: lightblue ;
       }
    

Examples: red , green , blue , coarl , gold , etc.

Hexadecimal Notation

Hex codes are widely used and give precise control over colors.


    p {
    color: #ff0000 ; /* red */
       }
    div {
    background-color: #f1f1f1 ;
       }
    

Format: #RRGGBB where each pair ranges from 00 to FF.

RGB (Red, Green, Blue)

Defines color using three values (0 to 255).


    div {
       background-color: rgb(255, 99, 71) ;  /* tomato */
       }
    

Advantage: Easy to visualize and manipulate through JavaScript or CSS functions.

RGBA (Red, Green, Blue, Alpha)

Same as RGB, but adds alpha for transparency (0 = completely transparent, 1 = opaque).


    div {
        background-color: rgba(0, 0, 255, 0.5) ;  /* semi-transparent blue */
       }
    

HSL (Hue, Saturation, Lightness)

HSL defines color more like a human would: Hue (0 – 360), Saturation (0 – 100%), and Lightness (0 – 100%).


    h1 {
       color: hsl(120, 100%, 25%) ;  /* dark green */
        }
    

HSLA (Hue, Saturation, Lightness, Alpha)

HSLA adds the alpha channel for transparency.


    p {
         color: hsla(0, 100%, 50%, 0.3) ;  /* transparent red */
       }
    

Transparent and Inherited Colors

CSS also supports keywords like:

Setting Background and Text Colors

 
    body {
       background-color: #ffffff; 
       color: #333333; 
    }
  

This is most highly used color pair for reading purposes: dark copy on a light background.

Using Variables for Colors

CSS variables allow consistent and manageable color theming:


    :root {
       --primary-color: #3498db;
       --text-color: #2c3e50;
      }
  
   h1 {
      color: var(--primary-color);
      }

   p {
      color: var(--text-color);
      }
    

This makes it easier to update your theme later.

Creating a Color Palette

Every good design starts with a harmonious color palette. A balanced palette includes:

Accessibility and Color Contrast

Always ensure that your choices for colors keep in mind that you want everyone to be able to read them. When constructing text at a minimum level of contrast and that color will be the only way a user can figure out meaning, is a poor experience.

Conclusion: Color the Web with Purpose

CSS colors allow us to convey beautiful, bright, accessible experiences for our users. We have color in multiple formats: HEX, RGB, HSL and named colors to choose from; we have a great amount of control when building our visual designs.

Carefully consider your palette, respect contrast, and allow us to flourish through color in the web/ on the web!

Try it Yourself