O'Reilly

Multiple Backgrounds with CSS

By on  

Anyone that's been in the web development industry for 5+ years knows that there are certain features that we should have had several years ago. One of those features is the HTML5 placeholder; we used JavaScript shims for a decade before placeholder came along. Another one of those simple features is multiple background images with CSS. Instead we'd need to nest another element for every additional background image. Now we a syntax for supporting multiple background images on one element, and here's what it looks like.

The CSS

Multiple backgrounds involved using multiple property assignments with multiple values, separated by a comma:

#multipleBGs {
	background: url(photo1.png),
				url(photo2.png),
				url(photo3.png)
	;
	background-repeat: no-repeat,
					   no-repeat,
					   repeat-y;
						
	background-position: 0 0,
						 30px 70px,
						 right top;
	
	width: 400px; 
	height: 400px;
	border: 1px solid #ccc;
}

Trying to stuff all properties via shorthand within the background property wont work, unfortunately; multiple property declarations must be used. All of the background properties may be used (background-attachment , background-clip , background-image , background-origin , background-position , background-repeat , background-size), as well as CSS gradients.

Another awesome CSS feature that we can finally used. Using multiple CSS backgrounds is an incredible useful tool, preventing the need for nested elements for the sole purpose of formatting.

Track.js Error Reporting

Upcoming Events

Recent Features

  • 6 Things You Didn’t Know About Firefox OS

    Firefox OS is all over the tech news and for good reason:  Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript.  Firefox OS has been rapidly improving,...

  • How I Stopped WordPress Comment Spam

    I love almost every part of being a tech blogger:  learning, preaching, bantering, researching.  The one part about blogging that I absolutely loathe:  dealing with SPAM comments.  For the past two years, my blog has registered 8,000+ SPAM comments per day.  PER DAY.  Bloating my database...

Incredible Demos

Discussion

  1. Cassy

    This is great!
    What are some of the good ways (without using multiple style sheets files) to compensate for IE8 and lower what don’t support multiple background images?

  2. Jane

    This is great!
    What are some of the good ways (without using multiple style sheets files) to compensate for IE8 and lower what don’t support multiple background images?

  3. I am new to HTML and CSS and do not know much. But trying to learn what I can. Thanks for sharing. It is very helpful to me.

  4. Guy Jeffries

    I managed to make the demo page look like a low res picture by resizing and then maximising in Chrome on a Win XP machine, I’ll be damned if I can repeat the bug so it’s not easy to do but I thought you’d like to know.

  5. Great stuff!! Now you only have to find multiple background images that are all good to look at..

  6. Pritesh Desai

    amazing! which browsers support it?
    they should add support for background scaling, something simple like ‘width’ and ‘height’ for img tag.

  7. Hello I have a question, maybe a hazing.

    I’m trying to make a button with an icon that has a gradient background.

    For example if the gradient of the button define .myButton as follows:

    a.myButton{
           background-image: -moz-linear-gradient(top, #aa393b, #97181f);
    	background-image: -ms-linear-gradient(top, #AA393B, #97181F);
    	background-image: -webkit-gradient(linear, left top, left bottom, from(#AA393B), to(#97181F));
    	background-image: -webkit-linear-gradient(top, #AA393B, #97181F);
    	background-image: -o-linear-gradient(top, #AA393B, #97181F);
    	background-image: linear-gradient(top, #AA393B, #97181F);
    	border-bottom: 1px solid #2E0000;
    }
    

    How did you ary to combine gradient with some icon? in the same button which has an HTML structure as follows:

    <a href='#' class='MyButton'> ... </a>
    

    Currently I am what I am doing with 2 images of the gradient background and something like:

    a.myButton{
    	background:URL(../img_icon.png),URL(../img_background.png);
    	background-repeat:no-repeat,repeat-x;
    	background-position:5px 1px,0 0;
    	padding-left:30px;
    	....
    }
    

    thanks beforehand.

  8. How to add link on backround image in div ?

  9. frantender

    David, here you are using all the images of same size i.e same width and height. What if I want to use multiple background images which have different dimensions which are in a sprite image ? Can I write like background-size: 113px 100px, 80px 90px, 120px 109px; for my 3 different backgrounds ? Specifying only background position won’t work for me because they all are of different dimensions.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!

Recently on David Walsh Blog

  • OâReilly Velocity Conference â New York

    My favorite front-end conference has always been O'Reilly's Velocity Conference because the conference series has focused on one of the most undervalued parts of client side coding:  speed.  So often we're so excited that our JavaScript works that we forget that speed, efficiency, and performance are just as important. The next Velocity...

  • Free Download: Font Bundle Featuring 17 Incredible Typefaces

    The only thing we love more than a good font, is a good free font. So we’ve combed the Web for some of our favorite free fonts, and gathered them here in a single download. You’ll find a variety of useful typefaces, from highly geometric designs...

  • OâReilly Velocity Conference â Amsterdam

    My favorite front-end conference has always been O'Reilly's Velocity Conference because the conference series has focused on one of the most undervalued parts of client side coding:  speed.  So often we're so excited that our JavaScript works that we forget that speed, efficiency, and performance are just as important. The next Velocity...

  • CanIUse Command Line

    Every front-end developer should be well acquainted with CanIUse, the website that lets you view browser support for browser features.  When people criticize my blog posts for not detailing browser support for features within the post, I tell them to check CanIUse:  always up to date, unlike...

  • Generating Alternative Stylesheets for Browsers Without @media

    If your CSS code is built with a mobile-first approach, it probably contains all the rules that make up the "desktop" view inside @media statements. That's great, but browsers that don't support media queries (IE 8 and below) will simply ignore them, ending up getting the...