The Complete CSS Colors & Backgrounds Cheatsheet: 26 Essential Properties & Techniques

Introduction to CSS Colors & Backgrounds

CSS colors and backgrounds are fundamental styling properties that control the appearance of elements on a webpage. Colors define text and border hues, while background properties control what appears behind content. Mastering these properties allows you to create visually appealing designs, establish visual hierarchy, and enhance user experience through proper contrast and visual cues.

Core CSS Color Concepts

Color Properties

PropertyDescriptionExample
colorSets text colorcolor: #333;
background-colorSets background colorbackground-color: lightblue;
border-colorSets border colorborder-color: red;
outline-colorSets outline coloroutline-color: green;
box-shadowAdds shadow with colorbox-shadow: 0 0 5px rgba(0,0,0,0.3);
text-shadowAdds shadow to texttext-shadow: 1px 1px 2px black;

Color Value Formats

FormatSyntaxExampleNotes
Hex#RRGGBB or #RGB#1a2b3c or #f80Most common format
RGBrgb(r, g, b)rgb(255, 0, 0)Values from 0-255
RGBArgba(r, g, b, a)rgba(255, 0, 0, 0.5)Alpha from 0-1
HSLhsl(h, s%, l%)hsl(120, 100%, 50%)Hue (0-360), saturation and lightness (0-100%)
HSLAhsla(h, s%, l%, a)hsla(120, 100%, 50%, 0.5)HSL with alpha transparency
NamedColor namered, blue, tomato140+ predefined color names
CurrentColorcurrentColorborder: 1px solid currentColor;Inherits the current text color
Color FunctionsVariouscolor-mix(in srgb, red 50%, blue)Modern color manipulation

Background Properties

Basic Background Properties

PropertyDescriptionExample
background-colorSets the background colorbackground-color: #f5f5f5;
background-imageSets one or more background imagesbackground-image: url('image.jpg');
background-repeatControls how background image repeatsbackground-repeat: no-repeat;
background-positionSets the starting positionbackground-position: center;
background-sizeControls background image sizebackground-size: cover;
background-attachmentSets whether image scrolls with contentbackground-attachment: fixed;
background-originSpecifies background positioning areabackground-origin: content-box;
background-clipDefines how background extendsbackground-clip: padding-box;
backgroundShorthand for all propertiesbackground: #f00 url('img.jpg') no-repeat center/cover;

Background-repeat Values

  • repeat: Repeats in both directions (default)
  • repeat-x: Repeats horizontally only
  • repeat-y: Repeats vertically only
  • no-repeat: No repetition
  • space: Images are repeated and spaced to fill area
  • round: Images are repeated and stretched to fill area

Background-position Values

  • Keywords: top, right, bottom, left, center
  • Percentages: 25% 75%
  • Length units: 20px 50px
  • Mixed values: center 2rem
  • Edge offsets: right 10px bottom 20px

Background-size Values

  • auto: Original size (default)
  • cover: Scales to cover container (may crop)
  • contain: Scales to fit inside container (may leave space)
  • Length units: 300px 200px
  • Percentages: 100% 50%

Advanced Background Techniques

CSS Gradients

Linear Gradients

/* Basic linear gradient (top to bottom) */
background-image: linear-gradient(red, yellow);

/* Angled gradient */
background-image: linear-gradient(45deg, red, yellow);

/* Multiple color stops */
background-image: linear-gradient(to right, red, orange, yellow, green, blue, indigo, violet);

/* Color stops with positions */
background-image: linear-gradient(to bottom, red 0%, orange 25%, yellow 50%, green 75%, blue 100%);

/* Repeating linear gradient */
background-image: repeating-linear-gradient(45deg, red, red 10px, blue 10px, blue 20px);

Radial Gradients

/* Basic radial gradient */
background-image: radial-gradient(red, yellow);

/* Shaped radial gradient */
background-image: radial-gradient(circle, red, yellow);

/* Positioned radial gradient */
background-image: radial-gradient(at top left, red, yellow);

/* Size and position */
background-image: radial-gradient(circle closest-side at 20% 30%, red, yellow);

/* Repeating radial gradient */
background-image: repeating-radial-gradient(circle, red, blue 10%, yellow 15%);

Conic Gradients

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

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

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

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

Multiple Backgrounds

/* Multiple background images with different properties */
background: 
  url('top.png') no-repeat top center / 100% auto,
  url('middle.png') no-repeat center / contain,
  url('bottom.png') no-repeat bottom center / 100% auto,
  linear-gradient(to right, rgba(255,0,0,0.5), rgba(0,0,255,0.5));

/* Layers are stacked with first one on top */

CSS Background Patterns & Effects

Pattern Examples

/* Stripes pattern */
background-image: linear-gradient(45deg, #ccc 25%, transparent 25%, transparent 75%, #ccc 75%);
background-size: 40px 40px;

/* Checkerboard pattern */
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;

/* Polka dots */
background-image: radial-gradient(circle, #000 10%, transparent 10%);
background-size: 20px 20px;

Overlay Effects

/* Dark overlay on background image */
background: linear-gradient(rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.7)), url('image.jpg');
background-size: cover;

/* Text-visible overlay gradient */
background: linear-gradient(to right, rgba(0,0,0,0.8), rgba(0,0,0,0.1)), url('image.jpg');
background-size: cover;

Common Challenges & Solutions

Challenge: Background Image Not Showing

Solutions:

  • Verify the image path is correct
  • Check if the element has width and height
  • Try using absolute path instead of relative
  • Ensure the image file exists and is accessible

Challenge: Background Image Distortion

Solutions:

  • Use background-size: cover or contain
  • Set proper aspect ratio on container
  • Use media queries to adjust background properties for different screens

Challenge: Background Colors Not Applying

Solutions:

  • Check for specificity issues (another rule might be overriding it)
  • Verify there’s no background shorthand overriding your background-color
  • Make sure the element has content or dimensions
  • Add !important temporarily for debugging (not for production)

Challenge: Gradient Banding

Solutions:

  • Add noise texture
  • Use more color stops
  • Use slightly transparent gradients
  • Add a subtle dithering effect

Best Practices & Tips

Performance Optimization

  • Use simple colors instead of gradients or images when possible
  • Optimize background images (compress, proper format)
  • Avoid excessive multiple backgrounds
  • Consider using CSS patterns instead of image files
  • Use will-change: background-image for animated backgrounds

Accessibility Considerations

  • Ensure sufficient color contrast (4.5:1 for normal text, 3:1 for large text)
  • Don’t rely solely on color to convey information
  • Avoid patterns that could trigger visual sensitivities
  • Test with color blindness simulators
  • Add alternative text for background images that convey meaning

Modern Techniques

  • Use CSS variables for consistent color schemes:

    :root {
      --primary-color: #3498db;
      --secondary-color: #2ecc71;
    }
    .element {
      background-color: var(--primary-color);
    }
    
  • Apply color themes with custom properties:

    .dark-theme {
      --bg-color: #222;
      --text-color: #eee;
    }
    .light-theme {
      --bg-color: #fff;
      --text-color: #333;
    }
    body {
      background-color: var(--bg-color);
      color: var(--text-color);
    }
    
  • Use backdrop-filter for frosted glass effects:

    .frosted {
      background: rgba(255, 255, 255, 0.2);
      backdrop-filter: blur(10px);
    }
    

Browser Compatibility Notes

  • Modern color formats (color functions, etc.) may not work in older browsers
  • backdrop-filter has limited support (use with fallbacks)
  • Conic gradients aren’t supported in some older browsers
  • Some advanced background properties might need prefixes for older browsers

Resources for Further Learning

This cheatsheet covers the essential properties and techniques for working with CSS colors and backgrounds. Use it as a quick reference when styling your web projects for visually appealing and professional results.

Scroll to Top