CSS: Background Image

By Xah Lee. Date: . Last updated: .

This page shows you how to use CSS background property and image sprites.

CSS Background Attributes

background-image
url(url). By default, the image tiles from left to right, top to bottom. You can set the background-repeat to override.
background-position
offset-x offset-y. Shift the image horizontally and vertically. The offsets are CSS Units.
background-clip
border-box (default), padding-box, or content-box. The area for the image to fill. [see CSS Box Model Tutorial]
background-origin
border-box (default), padding-box, or content-box. The start position of the image, from top left.
background-repeat
repeat (default), no-repeat, repeat-x, repeat-y.
background-size
width height CSS Units, each can also be auto, or contain (scale to fit, keep aspect ratio), cover (scale to fit, don't keep aspect ratio).
background-attachment
scroll (default), fixed. Whether the background image scrolls with the page, or fixed to window.
background-color
CSS Color Value. Color will show for the transparent area of the image.

For detail, see Colors and backgrounds#background

Example:

<div class="b47126">xyz</div>
div.b47126 {
background-image: url(star_pattern.png);
min-width: 270px;
max-width: 270px;
min-height: 270px;
max-height: 270px;
border: solid thick grey;
}

CSS Sprite Tutorial

What is a CSS sprite?

In many websites, it needs lots of icons. For example, here's icons used by Google Plus website.

Google Plus css sprites
Google Plus icons

Normally, each icon is a image file. For each image file, you need a HTTP request. Too many HTTP request makes your site loading slow. (regardless of the internet bandwidth, because each HTTP request usually means a TCP connection, and starting TCP connection has latency. [see TCP/IP Tutorial for Beginner])

CSS sprites is one way to solve the many http image request problem. Instead of using many images, you combine them into one big image, and use CSS to show part of the image, the icon you want.

(Note: another solution is using CSS: Data URI Scheme, but CSS sprites is in general more efficient, when your sprites are larger (say, bigger than 16×16 pixels, or you have more than 10 icons.).)

(Note: CSS sprite is often used by highly trafficked website that needs tens of icons. It also reduces server load. For small sites, you don't need to use it.)

CSS Sprite Example

Here's a example of a (flame) icon using CSS sprite:

Here's the HTML:

<div class="flameicon"></div>

Here's the CSS:

.flameicon {
width:17px;
height:22px;
background-image: url(i/Google_Plus_css_sprites.png);
background-position: -18px -20px;
}

The most important thing about sprites is positioning. Here's the steps of how to position a CSS sprite.

How to find the “background-position” for a sprite icon?

Suppose this is your image:

Google Plus css sprites

Suppose you want the red flame icon.

Find the coordinates of the upper-left corner and bottom-right corner of the icon you want.

For the flame icon, its upper-left corner is (18,20) and bottom-right corner is (35,42).

The offset for the icon would be background-position: -18px -20px.

The width and height for the element would be: 17px and 22px. (derived by: 35-18=17, 42-20=22.)

Sprites Gallery

Here's sample sprites used by popular sites. Many sites have more than one sprites image.

Twitter css sprites Google Plus sprites 11178 Google Plus sprites 88138
Sprites: Twitter, Google Plus, Google Plus
Facebook css sprites Facebook sprits 63186
Facebook sprites
Google Plus css sprites Google Plus sprits 2
Google Plus sprites
Sina Weibo css sprites
Sina Weibo sprites

Reference

Colors and backgrounds#background

Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

CSS Basics

  1. Basics
  2. Tag Matching Tutorial
  3. Selector Syntax
  4. Units

Color

  1. Color Syntax
  2. Color Names
  3. Opacity
  4. Linear Gradient
  5. Radial Gradient

Font

  1. font-family
  2. Standard Web Fonts
  3. font-size
  4. Meaning of Font Size
  5. Font Weight
  6. Chinese Font Names

Box Model

  1. Border
  2. Outline
  3. Margin vs Padding
  4. Box Sizing

Layout

  1. Layout Tutorial
  2. Display Property
  3. Position: static, relative, fixed, absolute
  4. Pure CSS Table
  5. Tabbed Menu
  6. 3 Columns Page Layout
  7. Multi-Column Layout for Text Flow
  8. Fix Element to Window
  9. Centering
  10. Flowing List
  11. z-index
  12. Text Over Image
  13. Text Flow Around Image
  14. OverFlow, Scrollbar

Transform/Animation

  1. 2D Transform
  2. Transition
  3. Animation

Line Wrap, Justification

  1. Line Wrap
  2. Text Alignment, Justification
  3. Match first-letter and first-line
  4. “:before”, “:after”
  5. Text Decoration: Underline, Overline, Line-Through
  6. Letter Spacing, Word Spacing

Box Decoration

  1. Box Shadow
  2. Text Shadow
  3. Round Corners
  4. Background Image
  5. Image Rollover

Misc

  1. Computed Style
  2. Pseudo Class vs Pseudo Element
  3. Media Query
  4. Variable
  5. calc
  6. Reset
  7. user-select
  8. Data URI Scheme
  9. protocol-relative URL
  10. Default Unit
  11. Declaring Character Set in CSS File
  12. Jargons Explained: Tag, Element, Node, Object, Attribute, Property, Method
  13. Display Table on Small Screen
  14. Fixed Aspect Ratio
  15. Add Icon to Links
  16. Progress Bar Example

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. CSS
  3. JavaScript
  4. JS Obj Ref
  5. DOM
  6. SVG
  7. Blog