• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

nSiteful Web Builders

Building a Better Web - One Site at a Time.

  • Home
  • About
    • Testimonials
    • Resources
    • Partner With Me
    • Frequently Asked Questions
  • Web Sites
  • Online Marketing
  • WordPress Support
    • Customized WordPress Training
    • 60-for-60 Sessions
  • Web Applications
  • Blog
    • At-A-Glance
    • Blog Articles Grouped by Category
    • Case Studies
    • General
    • Portfolio
    • Reviews
    • Snippets
    • Techniques
    • Projects
  • Contact
    • Purchase Retainer Consulting Hours
    • About Retainer Consulting Hours
    • Book a Meeting with Jeff
    • Tell Me About Your Web Project
    • nSiteful Newsletter Archives
    • nSiteful Rewards

By Jeff - 6/17/2013
Cats: Snippets · Tags: Background Images, CSS

snippets

CSS Multiple Backgrounds

Last updated May 9th, 2024 at 02:59 pm

CSS3 supports multiple backgrounds for an element. There are lots of resources on the Web that tell you how to do it (I list a few below). It’s actually remarkably easy and intuitive. And most modern browsers support multiple CSS backgrounds.

What I want to do here is show you why and when you might want to try this.

Example: Rating Stars

Suppose you’ve got a ratings system and you want to display star icons to signify each rating scale.

The old way, using background-repeat

  • Excellent

You might have guessed (correctly) that my CSS is using a repeating (repeat-x) 48×48 pixel transparent PNG image of a star.

Here’s the HTML:

<ul class="ratings">
<li>Excellent<span class="excellent-2"></span></li>
</ul>

The repeating graphic appears within the empty span element.

And here is the pertinent CSS:

.excellent-2 {
	background: url(images/star_48.png) repeat-x right center;
	padding: 12px 240px 12px 0;
}

The trick here is to provide enough horizontal padding to fit all 5 repeats of the 48px-wide image (48×5=240) and nothing more.

Now this works okay. And I could create four more rules: one each for very good, good, fair, and poor.

But let’s suppose I’m not crazy about the spacing of the stars. They look kind of far apart, even though they’re actually kissing each other (their white spaces, that is).

What if I could get the stars closer together? What if I could even overlap them for a nifty effect?

The good news is I can using CSS3 multiple backgrounds!

The new way, using CSS3 multiple backgrounds

  • Poor
  • Fair
  • Good
  • Very Good
  • Excellent

Here is the HTML for the above:

<ul class="ratings">
<li>Poor<span class="poor"></span></li>
<li>Fair<span class="fair"></span></li>
<li>Good<span class="good"></span></li>
<li>Very Good<span class="very-good"></span></li>
<li>Excellent<span class="excellent"></span></li>
</ul>

And here is the pertinent CSS:

.poor {
	background: 
		url(images/star_48.png) no-repeat left center;
		padding-left: 48px;
}
.fair {
	background: 
		url(images/star_48.png) no-repeat left center,
		url(images/star_48.png) no-repeat 20px center;
		padding-left: 68px;
}

.good {
	background: 
		url(images/star_48.png) no-repeat left center,
		url(images/star_48.png) no-repeat 20px center,
		url(images/star_48.png) no-repeat 40px center;
		padding-left: 88px;
}

.very-good {
	background: 
		url(images/star_48.png) no-repeat left center,
		url(images/star_48.png) no-repeat 20px center,
		url(images/star_48.png) no-repeat 40px center,
		url(images/star_48.png) no-repeat 60px center;
		padding-left: 108px;
}
.excellent {
	background: 
		url(images/star_48.png) no-repeat left center,
		url(images/star_48.png) no-repeat 20px center,
		url(images/star_48.png) no-repeat 40px center,
		url(images/star_48.png) no-repeat 60px center,
		url(images/star_48.png) no-repeat 80px center;
		padding-left: 128px;
}

Note that to get the overlap effect for all the cases except poor, I’m just adding 20 pixels to each successive horizontal position value.

Are you wondering about the padding-left declarations? The empty spans need padding for there to be a background in which the background images can appear. I could have set them to be some large number. But I wanted to be more precise. So here’s the formula to determine the padding width:

(w*n) - ( (w-f) * (n-1) )

where w is the width of the image, n is the number of background images, and f is the horizontal offset value for the background positions.

A Variation

Now look at the next two lines:

  • Excellent 1
  • Excellent 2

Do you see the difference?

Excellent 1 is the same as you saw above, in the original CSS3 Multiple Backgrounds example. Each successive star is under the one to its left.

But Excellent 2 is a bit different. In this case, each successive star is on top of the one to its left.

The difference has to do with the order of the backgrounds in the style rule. The is the one counter-intuitive thing about CSS3 multiple backgrounds. Unlike most things in CSS, where the last thing wins out, the first background in a multiple background declaration wins out — that is, comes out on top.

Here is the style rule for Excellent 2 (whose class is excellent-reverse).

.excellent-reverse {
	background: 
		url(images/star_48.png) no-repeat 80px center,
		url(images/star_48.png) no-repeat 60px center,
		url(images/star_48.png) no-repeat 40px center,
		url(images/star_48.png) no-repeat 20px center,
		url(images/star_48.png) no-repeat left center;
		padding-left: 128px;
}

Fun, yes?

Can you think of nifty applications for CSS3 multiple background images? Please share below.

Resources for HOW to do multiple CSS background

  • W3.org specification
  • CSS3.info: Multiple Backgrounds with CSS3
  • CSS-Tricks: Stacking Order of Multiple Backgrounds

Like what you see? Share with others and join my mailing list. No long-term commitment, unsubscribe any time.

Related Posts

  1. New Membership Site To Help People Follow Through On Their Intentions
  2. Side Effects of Rebuilding Web Sites to be Responsive and Mobile-Friendly
  3. Everything can be gamed
  4. Tent Birth and Breastfeeding Services
  5. How to Reuse Content in WordPress with the amr shortcode any widget Plugin

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

mailchimp signup

Subscribe to get notified when new articles are published. Unsubscribe any time. No spam. I promise. Check out my newsletter archives.

social

Twitter Facebook LinkedIn

Recent Articles

  • Post-Specific CSS for WordPress October 5, 2026
  • You built an online course, but they haven’t come. September 16, 2026
  • Custom MemberPress Pricing Pages December 19, 2025
  • CSS Data Attribute Override December 11, 2025
  • MemberPress Rules Using Custom Taxonomies and Custom Roles December 10, 2025

Filter By Category/Tag

Categories

  • Case Studies (8)
  • For Staff (1)
  • General (73)
  • Portfolio (7)
  • Reviews (14)
  • Snippets (24)
  • Techniques (52)

Popular Tags

Advanced Custom Fields Blogging Child Themes Content Marketing CSS Customer Service Custom Fields Custom Post Types Diagnostics Domain Names Facebook FooGallery Genesis Hosting HTML Images iPhone Libra Live Chat Marketing Media MemberPress MemberPress Courses Membership Sites Mobile-Friendly MySQL Photo Gallery php Pinterest Plugins Post Formats Pricing Project Management SEBA SEO Seth Godin Shortcodes Social Networking Surveys Taxonomies Twitter Video Web design Web forms WordPress

Footer

Background

Web Sites | WordPress Support | Web Applications.

Formally trained in liberal arts and education (I have a B.A. in Government from Harvard and studied Secondary Education at Rutgers Graduate School), I have honed my skills in the communication arts and sciences as a teacher, trainer, instructional designer, writer, photographer, calligrapher, helpdesk manager, database programmer, and multimedia developer.

(I've also been a group counselor, waiter, bartender, bicycle messenger boy, computer salesman, carpenter's helper, financial analyst, and school board president.)

Tech

Systems since 1983.
Web sites since 1994.
PHP since 2001.
WordPress since 2007.

Contact

Book Meeting
770-772-5134
Email Jeff
Send Money
All Ways

Copyright 2026, nSiteful Web Builders, Inc.

Cookies Consent
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
View preferences
  • {title}
  • {title}
  • {title}
https://iframe.mediadelivery.net/embed/392008/42d18bc1-2adc-4741-b733-053d08d09c32
https://vz-000c5976-3ab.b-cdn.net/42d18bc1-2adc-4741-b733-053d08d09c32/play_720p.mp4

Receive occasional emails from Jeff