CSS Colors & Backgrounds

Work with colors, gradients, and background properties to create beautiful designs.

What are CSS Colors?

CSS colors allow you to specify the color of text, backgrounds, borders, and other elements. CSS supports multiple color formats and provides powerful tools for creating gradients and effects.

Color Applications:

  • Text Color: Control the color of text content
  • Background Color: Set element background colors
  • Border Color: Color element borders
  • Gradients: Create smooth color transitions
  • Opacity: Control transparency levels

Color Formats

Named Colors

CSS provides 148 predefined color names.

/* Basic colors */
.red { color: red; }
.blue { color: blue; }
.green { color: green; }

/* Extended colors */
.crimson { color: crimson; }
.teal { color: teal; }
.orange { color: orange; }

/* Gray scale */
.black { color: black; }
.gray { color: gray; }
.white { color: white; }

Named Colors Demo

Red
Blue
Green
Orange
Purple
Teal
Crimson
Navy

Hexadecimal Colors

Six-digit hex codes representing RGB values.

/* Full hex codes */
.red { color: #ff0000; }
.green { color: #00ff00; }
.blue { color: #0000ff; }

/* Short hex codes (when digits repeat) */
.red { color: #f00; }
.green { color: #0f0; }
.blue { color: #00f; }

/* Common colors */
.black { color: #000000; }
.white { color: #ffffff; }
.gray { color: #808080; }

Hexadecimal Colors Demo

#FF0000
#00FF00
#0000FF
#FFFF00
#FF00FF
#00FFFF

RGB Colors

Red, Green, Blue values from 0-255.

/* RGB values */
.red { color: rgb(255, 0, 0); }
.green { color: rgb(0, 255, 0); }
.blue { color: rgb(0, 0, 255); }

/* RGBA with alpha (transparency) */
.transparent-red {
    color: rgba(255, 0, 0, 0.5); /* 50% transparent */
}

.semi-transparent {
    background-color: rgba(0, 0, 0, 0.7); /* 70% opaque black */
}

RGB Colors Demo

RGB(255,0,0)
RGB(0,255,0)
RGB(0,0,255)
RGBA(255,0,0,0.5)
RGBA(0,255,0,0.5)
RGBA(0,0,255,0.5)

HSL Colors

Hue, Saturation, Lightness values.

/* HSL values */
.red { color: hsl(0, 100%, 50%); }
.green { color: hsl(120, 100%, 50%); }
.blue { color: hsl(240, 100%, 50%); }

/* HSLA with alpha */
.transparent-blue {
    color: hsla(240, 100%, 50%, 0.5);
}

/* Different lightness values */
.light-red { color: hsl(0, 100%, 75%); }
.dark-red { color: hsl(0, 100%, 25%); }

HSL Colors Demo

HSL(0,100%,50%)
HSL(120,100%,50%)
HSL(240,100%,50%)
HSL(0,100%,75%)
HSL(0,100%,25%)
HSLA(240,100%,50%,0.5)

CSS Gradients

Linear Gradients

Create smooth color transitions in a straight line.

/* Basic linear gradient */
.gradient {
    background: linear-gradient(to right, #ff6b6b, #4ecdc4);
}

/* With direction */
.top-to-bottom {
    background: linear-gradient(to bottom, red, blue);
}

.left-to-right {
    background: linear-gradient(to right, red, blue);
}

/* With angle */
.angled {
    background: linear-gradient(45deg, red, blue);
}

/* Multiple color stops */
.multi-color {
    background: linear-gradient(to right, red, yellow, green, blue);
}

/* With transparency */
.transparent-gradient {
    background: linear-gradient(to right, rgba(255,0,0,0.8), rgba(0,0,255,0.8));
}

Linear Gradient Demo

Left to Right
Top to Bottom
45° Angle
Rainbow

Radial Gradients

Create circular or elliptical color transitions.

/* Basic radial gradient */
.radial {
    background: radial-gradient(circle, #ff6b6b, #4ecdc4);
}

/* Elliptical gradient */
.elliptical {
    background: radial-gradient(ellipse, red, blue);
}

/* With position */
.positioned {
    background: radial-gradient(circle at top left, red, blue);
}

/* Multiple color stops */
.multi-radial {
    background: radial-gradient(circle, red, yellow, green, blue);
}

Radial Gradient Demo

Circular
Elliptical
Top Left
Multi-color

Background Properties

Background Color

Set solid background colors for elements.

/* Basic background color */
.element {
    background-color: #f0f0f0;
}

/* With transparency */
.transparent-bg {
    background-color: rgba(0, 0, 0, 0.5);
}

/* Using different color formats */
.hex-bg { background-color: #ff0000; }
.rgb-bg { background-color: rgb(255, 0, 0); }
.hsl-bg { background-color: hsl(0, 100%, 50%); }

Background Image

Use images as backgrounds.

/* Basic background image */
.element {
    background-image: url('image.jpg');
}

/* Background image with color fallback */
.element {
    background-color: #f0f0f0;
    background-image: url('image.jpg');
}

/* Multiple background images */
.layered {
    background-image: 
        url('overlay.png'),
        url('background.jpg');
}

Background Properties

Control how background images are displayed.

/* Background size */
.cover {
    background-size: cover; /* Cover entire element */
}

.contain {
    background-size: contain; /* Fit within element */
}

.specific {
    background-size: 200px 100px; /* Specific dimensions */
}

/* Background position */
.top-left {
    background-position: top left;
}

.center {
    background-position: center;
}

.custom {
    background-position: 20% 50%;
}

/* Background repeat */
.no-repeat {
    background-repeat: no-repeat;
}

.repeat-x {
    background-repeat: repeat-x;
}

.repeat-y {
    background-repeat: repeat-y;
}

Background Shorthand

Combine multiple background properties.

/* Complete background shorthand */
.element {
    background: #f0f0f0 url('image.jpg') no-repeat center/cover;
}

/* Breaking it down:
   - #f0f0f0: background-color
   - url('image.jpg'): background-image
   - no-repeat: background-repeat
   - center: background-position
   - cover: background-size
*/

/* Multiple backgrounds */
.layered {
    background: 
        linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5)),
        url('image.jpg') no-repeat center/cover;
}

Color & Background Best Practices

Color Guidelines

  • Ensure contrast: Maintain WCAG accessibility standards for text readability
  • Use semantic colors: Choose colors that match the content meaning
  • Limit color palette: Use 3-5 main colors for consistency
  • Consider color blindness: Test designs for color accessibility
  • Use CSS variables: Define colors centrally for easy maintenance
  • Test in different lighting: Ensure colors work in various environments

CSS Variables for Colors

/* Define color palette */
:root {
    --primary-color: #007bff;
    --secondary-color: #6c757d;
    --success-color: #28a745;
    --danger-color: #dc3545;
    --warning-color: #ffc107;
    --info-color: #17a2b8;
    --light-color: #f8f9fa;
    --dark-color: #343a40;
}

/* Use variables */
.button {
    background-color: var(--primary-color);
    color: white;
}

.success {
    background-color: var(--success-color);
    color: white;
}
Back to Typography Next: Responsive Design