Poor contrast:
Light gray text on white background
color: #999; background: #fafafa;

Color Accessibility Guidelines

Performance Considerations

Complex backgrounds can impact performance, so consider these best practices:

Real-World Performance Example

Instead of using a repeating background image:

/* Using image (requires HTTP request, larger file size) */
.pattern {
background-image: url('pattern.jpg');
background-repeat: repeat;
}

/* Using CSS (no HTTP request, efficiently rendered) */
.pattern {
background-image: 
radial-gradient(circle at 1px 1px, #333 1px, transparent 0);
background-size: 10px 10px;
}

Practice Exercises

Exercise 1: Color Palette Creation

Create a harmonious color palette for a website with:

Define your palette using CSS variables and provide sample applications.

Exercise 2: Gradient Button

Create a button with:

Exercise 3: Background Pattern

Create a repeating background pattern using CSS gradients (no images). Examples:

Exercise 4: Layered Background

Create a layered background for a hero section that includes:

Resources and Tools

Color Selection Tools

Gradient Tools

Accessibility Tools

Further Reading

Summary

In this lecture, we explored:

These concepts provide the foundation for creating visually appealing, accessible, and performant web designs that go beyond basic styling.

In the next lecture, we'll explore the Box Model in Depth, which will help you understand how elements are sized and spaced in CSS.

Colors, Backgrounds, and Gradients

Colors, Backgrounds, and Gradients

Module 5: CSS Fundamentals - Tuesday Lecture 2

Introduction to Color in Web Design

Color is one of the most powerful tools in visual communication. In web design, color helps establish brand identity, creates visual hierarchy, improves usability, sets emotional tone, and enhances aesthetics.

CSS provides robust capabilities for applying color not just to text (as we saw in the previous lecture), but also to backgrounds, borders, and other elements through various properties and gradient techniques.

"Color does not add a pleasant quality to design—it reinforces it." — Pierre Bonnard, Painter

Just as a film director uses color grading to establish mood and guide attention, web designers use color strategically to create cohesive, engaging, and intuitive user experiences.

CSS Color Formats

CSS supports multiple formats for specifying colors, each with its own advantages:

Named Colors

color: black;
color: white;
color: red;
color: forestgreen;
color: goldenrod;
color: rebeccapurple;

CSS has 148 named colors, ranging from basic colors to more specific shades. They're easy to remember but limited in range.

Fun fact: "rebeccapurple" was added in memory of Rebecca Meyer, the daughter of web standards pioneer Eric Meyer.

Hexadecimal (#RGB)

color: #000000;  /* Black */
color: #FFFFFF;  /* White */
color: #FF0000;  /* Red */
color: #00FF00;  /* Green */
color: #0000FF;  /* Blue */
color: #FF5733;  /* Custom orange */

/* Shorthand for pairs of identical values */
color: #000;     /* Same as #000000 */
color: #FFF;     /* Same as #FFFFFF */
color: #F00;     /* Same as #FF0000 */

Hex values represent RGB values in base-16 notation. Each pair of characters represents a color channel (red, green, blue) with values from 00 to FF (0-255 in decimal).

RGB and RGBA

color: rgb(0, 0, 0);          /* Black */
color: rgb(255, 255, 255);    /* White */
color: rgb(255, 0, 0);        /* Red */
color: rgb(0, 128, 0);        /* Green */

/* With alpha (transparency) */
color: rgba(255, 0, 0, 0.5);  /* Semi-transparent red */
color: rgba(0, 0, 0, 0.25);   /* 25% opaque black */

/* Modern notation with space separation */
color: rgb(0 0 0);            /* Black */
color: rgb(255 0 0 / 50%);    /* Semi-transparent red */

RGB values specify red, green, and blue components directly (0-255). RGBA adds a fourth value for alpha transparency (0-1), which controls opacity.

HSL and HSLA

color: hsl(0, 0%, 0%);         /* Black */
color: hsl(0, 0%, 100%);       /* White */
color: hsl(0, 100%, 50%);      /* Red */
color: hsl(120, 100%, 50%);    /* Green */
color: hsl(240, 100%, 50%);    /* Blue */

/* With alpha (transparency) */
color: hsla(0, 100%, 50%, 0.5); /* Semi-transparent red */

/* Modern notation */
color: hsl(0 100% 50% / 50%);   /* Semi-transparent red */

HSL (Hue, Saturation, Lightness) is often more intuitive for design work:

  • Hue: Color wheel position (0-360 degrees)
  • Saturation: Color intensity (0% = gray, 100% = full color)
  • Lightness: Brightness (0% = black, 100% = white, 50% = normal)

Modern Color Formats

/* HWB (Hue, Whiteness, Blackness) */
color: hwb(0 0% 0%);          /* Red */
color: hwb(120 0% 0%);        /* Green */

/* Lab color space */
color: lab(50% 50 0);         /* Reddish */

/* LCH (Lightness, Chroma, Hue) */
color: lch(50% 100 0);        /* Vivid red */

/* Color function */
color: color(display-p3 1 0 0);  /* Vibrant red in wider gamut */

Modern browsers support more sophisticated color models for better color precision and wider gamuts.

HSL Color Wheel Visualization

HSL Color System Hue (0-360°) 0° (Red) 60° (Yellow) 120° (Green) 180° (Cyan) 240° (Blue) 300° (Magenta) Saturation (0-100%) 0% (Gray) 100% (Full color) Lightness (0-100%) 0% (Black) 50% (Full color) 100% (White)

Choosing Color Formats

  • Named colors: Quick prototyping, simple projects
  • Hexadecimal: Industry standard, widely used in design tools
  • RGB/RGBA: When transparency is needed
  • HSL/HSLA: When working with color relationships or generating color schemes
  • Modern formats: For cutting-edge applications with specific color requirements

Applying Colors to Elements

Color Properties

CSS provides specific properties to control colors for different parts of elements:

/* Text color */
color: #333;

/* Background color */
background-color: #f5f5f5;

/* Border color */
border-color: #ddd;

/* Outline color */
outline-color: #0066cc;

/* Box shadow color */
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);

/* Text decoration color */
text-decoration-color: red;
graph TD A[Element Appearance] --> B[Text Color] A --> C[Background Color] A --> D[Border Color] A --> E[Special Effects] B --> B1[color] B --> B2[text-decoration-color] C --> C1[background-color] D --> D1[border-color] D --> D2[outline-color] E --> E1[box-shadow] E --> E2[text-shadow]
Element with multiple color properties

This demonstrates how different color properties work together on an element.

Color Theory Fundamentals

Understanding color theory helps create harmonious and effective color schemes.

Color Harmony

Common harmonious color combinations based on the color wheel:

  • Monochromatic: Different shades of a single hue
  • Analogous: Colors adjacent on the color wheel
  • Complementary: Colors opposite on the color wheel
  • Triadic: Three colors equally spaced on the color wheel
  • Split-complementary: A color and two adjacent to its complement

Color Psychology

Colors evoke emotional and psychological responses:

  • Red: Energy, passion, danger, attention
  • Blue: Trust, calm, security, professionalism
  • Green: Growth, health, nature, prosperity
  • Yellow: Optimism, clarity, warmth, caution
  • Purple: Creativity, luxury, wisdom, mystery
  • Orange: Enthusiasm, creativity, determination
  • Black: Elegance, power, formality, sophistication
  • White: Purity, cleanliness, simplicity, spaciousness

Example Color Schemes

Monochromatic

CSS: hsl(210, --%, --%) with varying saturation and lightness

Complementary

CSS: hsl(210, --%, --%) and hsl(30, --%, --%) (180° apart)

Analogous

CSS: Adjacent hues (hsl(280, 60%, 50%) through hsl(40, 60%, 50%))

Real-World Examples

  • Financial services often use blue (trust) with green accents (money, growth)
  • Food services commonly use red and yellow (stimulate appetite and attention)
  • Health and wellness typically use green and blue (health, calm, cleanliness)
  • Luxury brands favor black, gold, and purple (exclusivity, elegance)
  • Tech companies often use blue (reliability, innovation) with clean white space

Backgrounds in CSS

CSS provides several properties to control element backgrounds, with background-color being the most basic.

background-color

/* Solid background colors */
background-color: white;
background-color: #f0f0f0;
background-color: rgb(240, 240, 240);
background-color: hsl(0, 0%, 94%);

/* Transparent backgrounds */
background-color: transparent;
background-color: rgba(240, 240, 240, 0.5);
background-color: hsla(0, 0%, 94%, 0.5);

Solid background: background-color: #f0f0f0;

Semi-transparent background: background-color: rgba(240, 240, 240, 0.5);

background-image

The background-image property sets one or more background images for an element.

/* URL to an image */
background-image: url('image.jpg');

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

/* Using gradients as images */
background-image: linear-gradient(to bottom, #f9f9f9, #e9e9e9);

/* Data URIs - Inline small images */
background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10"><circle cx="5" cy="5" r="5" fill="blue"/></svg>');

When both background-color and background-image are specified, the image is drawn on top of the color. The color will show through transparent parts of the image.

background-repeat

The background-repeat property defines how background images are repeated.

/* Repeat in both directions (default) */
background-repeat: repeat;

/* Repeat only horizontally */
background-repeat: repeat-x;

/* Repeat only vertically */
background-repeat: repeat-y;

/* No repetition */
background-repeat: no-repeat;

/* Space evenly with no clipping */
background-repeat: space;

/* Repeat with scaling to fit */
background-repeat: round;

/* For multiple backgrounds */
background-repeat: no-repeat, repeat;
repeat
repeat-x
repeat-y
no-repeat

background-position

The background-position property sets the initial position of a background image.

/* Keyword values */
background-position: top;
background-position: bottom;
background-position: left;
background-position: right;
background-position: center;

/* Keyword combinations */
background-position: top left;
background-position: bottom right;

/* Percentage values */
background-position: 25% 75%;

/* Length values */
background-position: 10px 20px;

/* Mixed values */
background-position: right 10px bottom 20px;
center
top right
25% 75%
right 10px bottom 10px

background-size

The background-size property specifies the size of the background images.

/* Keyword values */
background-size: cover;     /* Scale to cover entire element */
background-size: contain;   /* Scale to fit within element */

/* One-value syntax (sets width, height=auto) */
background-size: 50%;
background-size: 200px;

/* Two-value syntax (width, height) */
background-size: 50% auto;
background-size: 100px 150px;

/* Multiple backgrounds */
background-size: cover, contain;
auto (natural size)
cover
contain
75px 50px

Common Use Cases

  • cover: Hero images, full-screen backgrounds, cards with thumbnail images
  • contain: Logos, icons where the entire image must be visible
  • Fixed dimensions: UI elements, decorative patterns
  • Percentage values: Responsive designs where image size scales with container

background-attachment

The background-attachment property determines whether a background image scrolls with the content or remains fixed.

/* Scroll with the content (default) */
background-attachment: scroll;

/* Fixed relative to the viewport */
background-attachment: fixed;

/* Fixed relative to the element's content */
background-attachment: local;

Parallax Effect with Fixed Background

/* CSS for a parallax section */
.parallax-section {
height: 500px;
background-image: url('mountain.jpg');
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}

/* Content container */
.parallax-content {
padding: 50px;
background-color: rgba(255, 255, 255, 0.8);
}

background Shorthand

The background shorthand property allows setting all background properties at once.

/* Syntax: background: [color] [image] [repeat] [attachment] [position] / [size] */

/* Simple examples */
background: red;
background: url('image.jpg');

/* Comprehensive examples */
background: #f5f5f5 url('pattern.png') repeat-x fixed center top;
background: linear-gradient(to bottom, #fff, #f0f0f0) no-repeat center / cover;

/* Multiple backgrounds (newest syntax) */
background: 
url('overlay.png') no-repeat center / 100%,
url('background.jpg') no-repeat center / cover #333;

Important: When using the background shorthand, omitted values reset to their initial values. If you want to preserve existing values for some properties, use individual properties instead of the shorthand.

With multiple backgrounds, they're stacked with the first one on top.

Multiple Background Example

CSS Gradients

Gradients are smooth transitions between two or more colors. CSS supports several types of gradients that can be used as background images.

Linear Gradients

Linear gradients transition colors along a straight line.

/* Basic linear gradient (top to bottom) */
background-image: linear-gradient(#e66465, #9198e5);

/* Specifying direction */
background-image: linear-gradient(to right, #e66465, #9198e5);
background-image: linear-gradient(to bottom right, #e66465, #9198e5);
background-image: linear-gradient(45deg, #e66465, #9198e5);

/* Multiple color stops */
background-image: linear-gradient(#e66465, #9198e5, #66cc99);

/* Color stops with positions */
background-image: linear-gradient(to right, red 0%, orange 20%, yellow 40%, green 60%, blue 80%, purple 100%);

/* Hard color stops for stripes */
background-image: linear-gradient(to right, red 33.3%, yellow 33.3%, yellow 66.6%, green 66.6%);

/* Transparent gradients */
background-image: linear-gradient(to bottom, rgba(255,255,255,0), rgba(255,255,255,1));
Basic linear gradient
to right
45deg
Rainbow
Hard stops (stripes)
Transparency

Radial Gradients

Radial gradients transition from a central point outward in a circular or elliptical pattern.

/* Basic radial gradient (default is ellipse) */
background-image: radial-gradient(#e66465, #9198e5);

/* Specifying shape */
background-image: radial-gradient(circle, #e66465, #9198e5);
background-image: radial-gradient(ellipse, #e66465, #9198e5);

/* Specifying size */
background-image: radial-gradient(circle closest-side, #e66465, #9198e5);
background-image: radial-gradient(circle farthest-corner, #e66465, #9198e5);

/* Specifying position */
background-image: radial-gradient(circle at top right, #e66465, #9198e5);
background-image: radial-gradient(circle at 25% 75%, #e66465, #9198e5);

/* Multiple color stops */
background-image: radial-gradient(circle, white, blue 20%, green 40%, yellow 60%, red);

/* Complex example */
background-image: radial-gradient(circle farthest-corner at 10% 20%, rgba(0,0,255,0.5), rgba(0,255,0,0.5) 60%, rgba(255,0,0,0.5));
Basic radial gradient
Circle
closest-side
at top right
Multiple stops

Conic Gradients

Conic gradients transition colors around a circle (rather than radiating from the center).

/* Basic conic gradient */
background-image: conic-gradient(red, yellow, green, blue, purple);

/* Starting angle */
background-image: conic-gradient(from 45deg, red, yellow, green, blue);

/* Centered at specific position */
background-image: conic-gradient(at 60% 40%, red, yellow, green, blue);

/* With specific color stops */
background-image: conic-gradient(red 0deg, red 90deg, yellow 90deg, yellow 180deg, green 180deg, green 270deg, blue 270deg);

/* Pie chart effect */
background-image: conic-gradient(red 0deg, red 90deg, yellow 90deg, yellow 180deg, green 180deg, green 270deg, blue 270deg);

/* Color wheel */
background-image: conic-gradient(
hsl(0, 100%, 50%),
hsl(60, 100%, 50%),
hsl(120, 100%, 50%),
hsl(180, 100%, 50%),
hsl(240, 100%, 50%),
hsl(300, 100%, 50%),
hsl(360, 100%, 50%)
);
Basic conic gradient
from 45deg
at 25% 75%
Pie chart
Color wheel

Repeating Gradients

CSS also supports repeating versions of all gradient types, creating periodic patterns.

/* Repeating linear gradient (stripes) */
background-image: repeating-linear-gradient(
45deg,
#606dbc,
#606dbc 10px,
#465298 10px,
#465298 20px
);

/* Repeating radial gradient (concentric circles) */
background-image: repeating-radial-gradient(
circle,
purple,
purple 10px,
#4b026f 10px,
#4b026f 20px
);

/* Repeating conic gradient (wedge pattern) */
background-image: repeating-conic-gradient(
red 0deg 10deg,
yellow 10deg 20deg,
green 20deg 30deg
);
Repeating linear
Repeating radial
Repeating conic

Creative Uses of Gradients

  • UI elements: Buttons, progress bars, headers with subtle gradients
  • Backgrounds: Page backgrounds, card backgrounds, section dividers
  • Data visualization: Charts, indicators, heat maps
  • Decorative patterns: Using repeating gradients for stripes, checks, or other patterns
  • Text effects: Combined with background-clip for gradient text

Advanced Background Techniques

Multiple Backgrounds

CSS allows stacking multiple backgrounds on a single element by providing comma-separated values.

/* Multiple background images */
background-image: 
url('top-layer.png'),
url('middle-layer.png'),
url('bottom-layer.png');

/* Multiple backgrounds with different properties */
background: 
url('icon.svg') no-repeat top right / 50px,
linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5)) no-repeat center / cover,
url('background.jpg') no-repeat center / cover;
Text overlay on multiple backgrounds

Common Multiple Background Patterns

  • Image overlay: Using a gradient on top of an image to improve text readability
  • Decorative elements: Adding icons or patterns over a base background
  • Complex UI: Building layered interfaces with background elements
  • Textures: Combining base color with subtle texture patterns

background-blend-mode

The background-blend-mode property specifies how an element's background images should blend with each other and with the background color.

/* Common blend modes */
background-blend-mode: normal;
background-blend-mode: multiply;
background-blend-mode: screen;
background-blend-mode: overlay;
background-blend-mode: darken;
background-blend-mode: lighten;
background-blend-mode: color-dodge;
background-blend-mode: color-burn;
background-blend-mode: difference;
background-blend-mode: exclusion;
background-blend-mode: hue;
background-blend-mode: saturation;
background-blend-mode: color;
background-blend-mode: luminosity;
multiply
screen
overlay

Duotone Effect with Blend Modes

/* Creating a duotone effect */
.duotone {
background: 
linear-gradient(#09f, #09f), /* Blue layer */
linear-gradient(#f90, #f90), /* Orange layer */
url('image.jpg') no-repeat center;
background-size: cover, cover, cover;
background-blend-mode: screen, multiply, normal;
}

background-clip and background-origin

These properties control where the background is drawn relative to the element's box model.

/* background-clip: specifies the painting area */
background-clip: border-box;    /* Default: extends to outer edge of border */
background-clip: padding-box;   /* Clips to padding edge (inside border) */
background-clip: content-box;   /* Clips to content area only */
background-clip: text;          /* Clips to text characters (for text effects) */

/* background-origin: specifies where the background-position is calculated from */
background-origin: border-box;  /* Positions relative to border edge */
background-origin: padding-box; /* Default: positions relative to padding edge */
background-origin: content-box; /* Positions relative to content edge */
border-box
padding-box
content-box

Gradient Text with background-clip: text

/* CSS for gradient text */
.gradient-text {
background-image: linear-gradient(to right, #f06, #9f6);
background-clip: text;
-webkit-background-clip: text;  /* For Safari support */
color: transparent;
font-size: 3em;
font-weight: bold;
}
Gradient Text

Practical Applications and Techniques

Creating Custom UI Elements

Combining colors and backgrounds allows for creating custom UI elements without images:

Glossy Button

.glossy-button {
display: inline-block;
padding: 10px 20px;
background: linear-gradient(to bottom, 
      rgba(255,255,255,0.8) 0%,
      rgba(255,255,255,0.2) 50%,
      rgba(0,0,0,0.05) 51%,
      rgba(0,0,0,0.1) 100%),
      #0088cc;
color: white;
border-radius: 5px;
border: 1px solid #0066aa;
box-shadow: 0 1px 3px rgba(0,0,0,0.2);
text-shadow: 0 -1px 1px rgba(0,0,0,0.3);
font-weight: bold;
cursor: pointer;
transition: background 0.3s;
}

.glossy-button:hover {
background: linear-gradient(to bottom, 
      rgba(255,255,255,0.8) 0%,
      rgba(255,255,255,0.2) 50%,
      rgba(0,0,0,0.05) 51%,
      rgba(0,0,0,0.1) 100%),
      #00aaee;
}
Glossy Button

Checkerboard Pattern

.checkerboard {
background-image: 
linear-gradient(45deg, #ccc 25%, transparent 25%), 
linear-gradient(-45deg, #ccc 25%, transparent 25%),
linear-gradient(45deg, transparent 75%, #ccc 75%),
linear-gradient(-45deg, transparent 75%, #ccc 75%);
background-size: 20px 20px;
background-position: 0 0, 0 10px, 10px 0, 10px 10px;
background-color: white;
}

Accessible Color Combinations

When selecting colors, it's important to ensure sufficient contrast for accessibility:

Good contrast:
White text on black background
color: #fff; background: #000;
Good contrast:
Dark gray text on light gray background
color: #333; background: #eee;

Color Accessibility Guidelines

  • Ensure a contrast ratio of at least 4.5:1 for normal text and 3:1 for large text
  • Don't rely solely on color to convey information (use patterns, icons, or text)
  • Test your color combinations with accessibility tools
  • Consider color blindness when choosing color palettes
  • Use the Web Content Accessibility Guidelines (WCAG) as a reference

Performance Considerations

Complex backgrounds can impact performance, so consider these best practices:

  • Use CSS gradients instead of image gradients when possible
  • Minimize the use of multiple backgrounds on elements that appear frequently
  • Be cautious with large fixed backgrounds, as they can cause scrolling jank
  • Consider using CSS sprites for multiple background images
  • Use SVG for vector graphics and patterns, as they scale well and are compact
  • Be mindful of alpha transparency, as it requires more processing

Real-World Performance Example

Instead of using a repeating background image:

/* Using image (requires HTTP request, larger file size) */
.pattern {
  background-image: url('pattern.jpg');
  background-repeat: repeat;
}

/* Using CSS (no HTTP request, efficiently rendered) */
.pattern {
  background-image: 
    radial-gradient(circle at 1px 1px, #333 1px, transparent 0);
  background-size: 10px 10px;
}

Practice Exercises

Exercise 1: Color Palette Creation

Create a harmonious color palette for a website with:

  • A primary color
  • A secondary color
  • An accent color
  • Background colors (light and dark variants)
  • Text colors that provide sufficient contrast

Define your palette using CSS variables and provide sample applications.

Exercise 2: Gradient Button

Create a button with:

  • A linear gradient background
  • A subtle hover effect
  • Appropriate text contrast
  • A shadow for depth
  • Rounded corners and padding

Exercise 3: Background Pattern

Create a repeating background pattern using CSS gradients (no images). Examples:

  • Stripes
  • Checkerboard
  • Polka dots
  • Grid lines
  • Zigzag pattern

Exercise 4: Layered Background

Create a layered background for a hero section that includes:

  • A base gradient
  • A translucent pattern overlay
  • A positioning for text content that ensures readability
  • Responsive behavior (background works on different screen sizes)

Resources and Tools

Color Selection Tools

Gradient Tools

Accessibility Tools

Further Reading

Summary

In this lecture, we explored:

  • Various CSS color formats (named colors, hex, RGB/RGBA, HSL/HSLA, and modern formats)
  • Color properties for text, backgrounds, borders, and effects
  • Color theory fundamentals for creating harmonious designs
  • Background properties including background-color, background-image, background-repeat, background-position, background-size, and background-attachment
  • Different types of gradients: linear, radial, conic, and repeating
  • Advanced techniques like multiple backgrounds, blend modes, and background-clip
  • Practical applications, performance considerations, and accessibility guidelines

These concepts provide the foundation for creating visually appealing, accessible, and performant web designs that go beyond basic styling.

In the next lecture, we'll explore the Box Model in Depth, which will help you understand how elements are sized and spaced in CSS.