Background Properties - Complete Control

What You'll Learn

  • Set background colors with background-color including transparency
  • Control image tiling with background-repeat (repeat, no-repeat, space, round)
  • Position backgrounds with background-position using keywords and precise values
  • Scale backgrounds with background-size (cover, contain, custom dimensions)
  • Control scroll behavior with background-attachment (scroll, fixed, local)
  • Set background starting point with background-origin
  • Control visibility area with background-clip including text clipping
  • Layer multiple backgrounds for complex visual effects
  • Use the background shorthand to combine properties efficiently

Introduction to Background Properties

CSS provides comprehensive control over backgrounds through dedicated properties. Each property targets a specific aspect: color, images, positioning, sizing, scrolling behavior, and layering. Master these properties to create sophisticated background designs.

While gradients (covered in the previous lesson) create color transitions, these properties control how any background—color, gradient, or image—is displayed, positioned, and behaves.

background-color - Solid & Transparent

The background-color property sets a solid color behind an element. Use named colors, hex values, RGB, or RGBA for transparency.

/* Named colors */ background-color: white; background-color: cornflowerblue; /* Hex values */ background-color: #4CAF50; background-color: #667eea; /* RGB and RGBA */ background-color: rgb(102, 126, 234); background-color: rgba(102, 126, 234, 0.3); /* 30% opacity */ /* Modern color functions */ background-color: oklch(0.7 0.2 250); /* OKLCH color space */ /* Transparent */ background-color: transparent;

background-repeat - Tiling Control

Control how background images tile to fill the element. The default is repeat (tiles in both directions).

/* Standard values */ background-repeat: repeat; /* Tiles horizontally and vertically (default) */ background-repeat: no-repeat; /* Shows once, no tiling */ background-repeat: repeat-x; /* Tiles horizontally only */ background-repeat: repeat-y; /* Tiles vertically only */ /* Advanced values */ background-repeat: space; /* Distributes evenly with spacing */ background-repeat: round; /* Scales to fit exactly, no clipping */ /* Two-value syntax (horizontal vertical) */ background-repeat: repeat no-repeat; /* Horizontal repeat, no vertical */ background-repeat: space round; /* Space horizontal, round vertical */

background-position - Placement Control

Position backgrounds within their container using keywords, percentages, or absolute values. Control horizontal and vertical placement independently.

/* Keyword positioning */ background-position: center; /* Center both axes */ background-position: top right; /* Top-right corner */ background-position: bottom left; /* Bottom-left corner */ /* Percentage positioning */ background-position: 50% 50%; /* Center (same as 'center') */ background-position: 0% 0%; /* Top-left */ background-position: 100% 100%; /* Bottom-right */ /* Absolute positioning */ background-position: 20px 30px; /* 20px from left, 30px from top */ background-position: 2rem 3rem; /* Using rem units */ /* Edge offset syntax (modern) */ background-position: right 20px bottom 30px; /* 20px from right, 30px from bottom */ background-position: left 10% top 15%; /* 10% from left, 15% from top */ /* calc() for complex positioning */ background-position: calc(100% - 20px) calc(100% - 20px);

background-size - Scaling Control

Control how background images are scaled. The two most important values are cover (fills entire area) and contain (fits inside completely).

/* Keyword values */ background-size: cover; /* Fills entire area, may crop edges */ background-size: contain; /* Fits completely inside, may show space */ background-size: auto; /* Natural image size (default) */ /* Single value (width, height auto) */ background-size: 50%; /* Width 50%, height maintains aspect ratio */ background-size: 200px; /* Width 200px, height maintains aspect ratio */ /* Two values (width height) */ background-size: 100% 200px; /* Width 100%, height 200px */ background-size: 200px 300px; /* Fixed dimensions */ background-size: 50% auto; /* Width 50%, height auto */ background-size: auto 100%; /* Width auto, height 100% */ /* Practical examples */ .hero { background-size: cover; background-position: center; } .logo { background-size: contain; background-repeat: no-repeat; background-position: center; } .pattern { background-size: 60px 60px; background-repeat: repeat; }

background-attachment - Scroll Behavior

Control whether backgrounds scroll with the page, the element, or stay fixed in the viewport.

/* scroll (default) - Scrolls with the element */ background-attachment: scroll; /* fixed - Stays fixed in viewport (parallax effect) */ background-attachment: fixed; /* local - Scrolls with element's content */ background-attachment: local; /* Parallax effect example */ .parallax { background-image: url('hero.jpg'); background-size: cover; background-position: center; background-attachment: fixed; min-height: 500px; }

background-origin - Starting Point

Determine where the background positioning area starts relative to the box model (border, padding, content).

/* padding-box (default) - Starts at padding edge */ background-origin: padding-box; /* border-box - Starts under border */ background-origin: border-box; /* content-box - Starts at content edge */ background-origin: content-box; /* Example: Background under border */ .bordered-bg { border: 15px dashed rgba(0,0,0,0.2); padding: 30px; background: linear-gradient(135deg, #667eea, #764ba2); background-origin: border-box; /* Background shows under border */ }

background-clip - Visibility Area

Control where the background is painted (visible). Most importantly, background-clip: text enables gradient text effects.

/* border-box (default) - Visible under border */ background-clip: border-box; /* padding-box - Clipped to padding area */ background-clip: padding-box; /* content-box - Clipped to content area */ background-clip: content-box; /* text - Clips to text shape (gradient text) */ background-clip: text; -webkit-background-clip: text; /* Vendor prefix needed */ /* Gradient text example */ .gradient-text { background: linear-gradient(135deg, #667eea, #764ba2); -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text; color: transparent; /* Fallback */ }

Multiple Backgrounds - Layering

Stack multiple backgrounds by separating them with commas. The first background listed appears on top; subsequent backgrounds render behind it.

/* Basic layering: pattern over gradient */ .card { background-image: url('dots.svg'), linear-gradient(135deg, #667eea, #764ba2); background-position: 0 0, center; background-size: 60px 60px, cover; background-repeat: repeat, no-repeat; } /* Dark overlay on image for text contrast */ .hero { background-image: linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5)), url('hero.jpg'); background-size: cover, cover; background-position: center, center; } /* Complex multi-layer effect */ .fancy { background-image: radial-gradient(circle at 20% 30%, rgba(255,255,255,0.2), transparent 50%), radial-gradient(circle at 80% 70%, rgba(255,255,255,0.15), transparent 50%), repeating-linear-gradient( 45deg, transparent, transparent 20px, rgba(255,255,255,0.05) 20px, rgba(255,255,255,0.05) 40px ), linear-gradient(135deg, #f093fb, #f5576c); } /* Each layer can have independent properties */ .multi { background-image: url('a.png'), url('b.png'), url('c.png'); background-position: top left, top right, bottom center; background-repeat: no-repeat, no-repeat, repeat-x; background-size: 100px, 200px, auto; }

Background Shorthand - All-in-One

The background shorthand combines multiple background properties into a single declaration. Order matters!

/* Full syntax */ background: [color] [image] [repeat] [attachment] [position] / [size] [origin] [clip]; /* Simple examples */ background: #667eea; background: url('pattern.svg'); background: linear-gradient(135deg, #667eea, #764ba2); /* With size (note the / separator) */ background: url('pattern.svg') repeat center / 50px 50px; background: url('hero.jpg') no-repeat center / cover; /* Complex example */ background: #fa709a url('dots.svg') repeat top left / 60px 60px padding-box content-box; /* Multiple backgrounds in shorthand */ background: url('overlay.png') no-repeat center / cover, linear-gradient(135deg, #667eea, #764ba2); /* Common patterns */ /* Hero image */ .hero { background: url('hero.jpg') no-repeat center / cover; } /* Pattern with color fallback */ .pattern { background: #667eea url('pattern.svg') repeat center / 60px 60px; } /* Gradient overlay on image */ .overlay { background: linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5)), url('image.jpg') no-repeat center / cover; }

Key Takeaways

  • background-color: Sets solid colors; use RGBA for transparency
  • background-repeat: Control tiling (repeat, no-repeat, space, round)
  • background-position: Use keywords, percentages, or edge offsets for precise placement
  • background-size: cover fills area (may crop), contain fits inside (may show space)
  • background-attachment: fixed creates parallax but has mobile performance issues
  • background-origin: Controls where positioning starts (border-box, padding-box, content-box)
  • background-clip: Controls visibility area; text enables gradient text
  • Multiple backgrounds: Layer with commas; first listed appears on top
  • Shorthand: Use / to separate position from size
  • Always set background-position: center with cover for balanced cropping
  • Use RGBA/transparency in upper layers when layering multiple backgrounds
  • CSS backgrounds are GPU-accelerated and perform better than images
  • For gradient text, use both -webkit-background-clip and background-clip