CSS Backgrounds Tutorial with Examples

CSS Backgrounds Tutorial with Examples

CSS Backgrounds – color, image, repeat, position, size, attachment | TipsInLearning
Lesson 5 of 12

CSS Backgrounds

CSS backgrounds control how elements look behind their content. Learn all 7 background properties — color, image, repeat, position, size, attachment, and shorthand — with visual demos and code examples.

11 min read
Beginner
CSS Properties
What You'll Learn
What CSS backgrounds do
background-color property
background-image with url()
background-repeat values
background-position & size
background-attachment: fixed
Background shorthand property
Complete background example

What are CSS Backgrounds?

CSS Backgrounds are used to control the background appearance of HTML elements — such as pages, sections, divs, or any box. You can set colors, images, positions, sizes, and repeating behavior for backgrounds. Used well, backgrounds transform plain pages into visually rich layouts.

Where backgrounds apply: Every HTML element has a background area — you can style the body background for the whole page, or apply different backgrounds to individual div, section, or header elements independently.

7 CSS Background Properties

CSS provides 7 dedicated background properties — each controlling a different aspect of how a background looks:

01
background-color
Sets background colour
02
background-image
Sets background image
03
background-repeat
Controls image tiling
04
background-position
Sets image placement
05
background-size
Controls image size
06
background-attachment
Fixed or scroll
07
background
Shorthand for all

1. background-color

background-color — Set Background Colour

This property sets the background colour of an element. It accepts any valid CSS color — color names, HEX codes, RGB, RGBA, HSL, or HSLA values. It applies to the entire element area including padding, but not the border.

lightblue
body background
yellow
div background
purple
HEX color
rgba
with opacity
CSS — background-color
/* Entire page background */
body {
  background-color: lightblue;
}

/* Using HEX or RGBA */
.card {
  background-color: #7c3aed;
}
.overlay {
  background-color: rgba(0, 0, 0, 0.5);
}

2. background-image

background-image — Add a Background Image

This property adds an image as the background of an element. The image path is specified using the url() function. By default, the image tiles to fill the entire element area. You can also use CSS gradients as background images.

CSS — background-image
/* Image from file */
body {
  background-image: url("image.jpg");
}

/* CSS gradient as background */
header {
  background-image: linear-gradient(135deg, #2563eb, #7c3aed);
}
Tip: Always set a background-color fallback when using background-image. If the image fails to load, the background color will show instead — keeping the page readable.

3. background-repeat

background-repeat — Control Image Tiling

By default, background images tile automatically in both directions. The background-repeat property lets you control this behaviour — repeat in one direction, or stop it entirely.

ValueDescription
repeatImage repeats both horizontally and vertically (default)
repeat-xImage repeats horizontally only
repeat-yImage repeats vertically only
no-repeatImage appears only once — no tiling
spaceImages tiled with equal space between them
roundImages tiled and scaled to fill without clipping
CSS — background-repeat
/* Show image once only */
body {
  background-image: url("hero.jpg");
  background-repeat: no-repeat;
}

/* Repeat horizontally only */
header {
  background-repeat: repeat-x;
}

4. background-position

background-position — Set Image Placement

This property sets the starting position of the background image within the element. Most commonly used with background-repeat: no-repeat to place a single image exactly where you want it.

ValueMeaning
top leftTop left corner (default)
top rightTop right corner
centerCentre of the element
bottom leftBottom left corner
bottom rightBottom right corner
50% 50%Centre using percentages
100px 50pxExact pixel position
CSS — background-position
body {
  background-image: url("image.jpg");
  background-position: center;
  background-repeat: no-repeat;
}

5. background-size

background-size — Control Image Size

This property controls how large the background image is displayed. The cover value is most popular — it scales the image to cover the entire element while maintaining aspect ratio, with possible cropping.

ValueMeaning
coverScales image to cover entire element — may crop edges
containScales image to fit fully inside — may leave gaps
autoImage displayed at its natural size (default)
300px 200pxExact width and height in pixels
100% 100%Stretch to fill element — may distort
CSS — background-size
/* Cover — most common for hero sections */
body {
  background-size: cover;
}

/* Contain — show full image */
div {
  background-size: contain;
}
cover vs contain: Use cover for hero images and full-page backgrounds. Use contain for logos or icons in a background.

6. background-attachment

background-attachment — Fixed or Scrolling

This property controls whether the background image scrolls with the page or stays fixed. The fixed value creates the popular "parallax" effect where the background stays still while content scrolls over it.

ValueMeaning
scrollBackground scrolls with the page (default)
fixedBackground stays fixed — creates parallax effect
localBackground scrolls with the element's own content
CSS — background-attachment
/* Fixed background — parallax effect */
body {
  background-attachment: fixed;
}

7. background (Shorthand Property)

background — Write All Properties in One Line

Instead of writing separate properties, you can combine them all into one background declaration. The shorthand order is: color image repeat position / size attachment.

lightblue
background-color
url("bg.jpg")
background-image
no-repeat
background-repeat
center
background-position
/ cover
background-size
fixed
background-attachment
CSS — background shorthand
/* Shorthand: color image repeat position */
body {
  background: lightblue url("image.jpg") no-repeat center;
}

/* Full shorthand with size and attachment */
body {
  background: #0f172a url("image.jpg") no-repeat center / cover fixed;
}
When to use shorthand: The shorthand is great for concise code, but write individual properties when you need clarity. Any property omitted from shorthand resets to its default value.

Complete CSS Background Example

Here is a full HTML page showing all background properties working together:

HTML + CSS — Complete Background Example
<!DOCTYPE html>
<html lang="en">
<head>
  <style>
    body {
      background-color: lightgray;
      background-image: url("bg.jpg");
      background-repeat: no-repeat;
      background-position: center;
      background-size: cover;
    }
  </style>
</head>
<body>
  <h1>Welcome to My Website</h1>
</body>
</html>
Browser Output

Welcome to My Website

Try It Yourself

Challenge — Style Backgrounds
1Set the body background-color to any color and a div background to a different color
2Add a background-image to an element — try using a CSS gradient
3Try all 4 values of background-repeat and observe the difference
4Use background-size: cover and background-position: center for a hero background
5Rewrite your backgrounds using the background shorthand property
Lesson Summary
1background-color sets background colour (name, HEX, RGB…)
2background-image sets an image or gradient as background
3background-repeat = repeat, repeat-x, repeat-y, no-repeat
4background-position places the image precisely
5background-size: cover fills area · contain fits fully
6background-attachment: fixed creates a parallax effect

Questions About CSS Backgrounds

The questions beginners most often ask when learning backgrounds:

What's the difference between background-color and background-image?
background-color fills the entire element with a solid colour. background-image places an image (file or gradient) on top of that background. You can use both together — the color acts as a fallback if the image fails to load.
Why is my background image repeating across the page?
By default, background-repeat is set to repeat, which tiles the image in both directions. Use background-repeat: no-repeat to show the image once, repeat-x for horizontal only, or repeat-y for vertical only.
How do I centre a background image?
Use background-position: center or background-position: 50% 50%. Also use background-repeat: no-repeat so it only appears once. Pro tip: background-position: center; background-repeat: no-repeat; background-size: cover;
Should I use background-size: cover or contain?
cover scales the image to fill the entire element — may crop edges but looks professional. contain scales the image to fit fully inside without cropping — may leave gaps. Use cover for full-page backgrounds or hero images, contain for logos or icons.
What does background-attachment: fixed do?
This creates a parallax effect — the background image stays fixed in place while the page content scrolls over it. Use background-attachment: fixed on the body or a section. High-end websites use this for visual depth and impact.
Can I use CSS gradients as background images?
Yes! CSS gradients are treated as image values in background-image. For example: background-image: linear-gradient(135deg, #2563eb, #7c3aed); Gradients render instantly in the browser with no file loading time.

Post a Comment