skip to content
Website Builder

CSS3 Background Images - 100% width/height of container

28th July 2010

A Box with known width and height

A Box with known width and height

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam diam sem, rhoncus eu auctor sit amet, volutpat at lectus.


A Box with known width and unknown height

A Box with known width and unknown height

Sed tellus nisi, egestas at imperdiet a, consequat vel turpis. Nunc aliquet nulla vel tortor egestas pharetra. Nunc vitae nunc arcu. Donec egestas venenatis egestas. Vestibulum mi tellus, ullamcorper a pretium fermentum, condimentum at tellus. Maecenas tristique urna in lectus feugiat in elementum dolor pretium.

Donec vestibulum, lacus vel ultricies porta, odio lorem suscipit nunc, id vulputate nulla est non velit. Sed at turpis sem. In eu turpis eget nisl interdum pharetra.


A Box with unknown width and unknown height

A Box with unknown width and unknown height

Etiam ut lectus elit. Ut quis tellus vitae orci lobortis viverra vitae id dui.
Nulla ultrices viverra ante, quis imperdiet mi blandit sed.
Vivamus sagittis tincidunt ipsum, a iaculis nibh varius nec.
Proin feugiat lacinia molestie.
Vestibulum ante ipsum primis in faucibus orci luctus et ultrices
posuere cubilia Curae; Morbi pulvinar malesuada turpis eget fermentum.

Vestibulum ante ipsum primis in faucibus orci luctus et ultrices
posuere cubilia Curae; Pellentesque habitant morbi tristique
senectus et netus et malesuada fames ac turpis egestas.

Ut diam erat, vulputate vel eleifend venenatis, auctor sed nisl.
Maecenas nulla magna, vulputate vitae sodales vel, molestie quis leo.
Praesent orci metus, auctor vel hendrerit at, ultrices nec nisl.
Nam non est nunc. Donec id mauris in purus porta hendrerit at at massa.




CSS play - stu nicholls

CSS play Support


CSS play Recommend

SitegrinderPSD to HTMLuk white label shopping carts for resellersFree, practical CSS menus, layouts, and examples

Follow CSS play

Facebook   Twitter



Information

Having recently ploughed through hundreds of pages on the web looking for information on how to style a background image to be 100% the size of the container using CSS3 and found most did not have the correct information, I thought that I would clarify things here and show how this is actually styled.

The above demonstration will work in Firefox, Safari, Chrome and Opera at the moment, all other browsers will see an actual size background image centered in the containers.

All three of the above boxes use the same styling for the background image and is as follows:

.back {
background-image: url(back.jpg);
background-repeat:no-repeat;
background-position:center center;
-o-background-size: 100% 100%, auto;
-moz-background-size: 100% 100%, auto;
-webkit-background-size: 100% 100%, auto;
background-size: 100% 100%, auto;
}

This method can also be applied to the body element and I have a demo of this here http://www.cssplay.co.uk/layouts/background2.html.




Copyright

You may use this method on your personal 'non-profit' web site without seeking my permission. A link back to CSSplay is always appreciated.

Commercial usage is also permitted without seeking approval, but I would ask that a donation is considered to support my work on CSSPlay.

If you are having problems integrating any of my demonstrations into your website then I now offer a service to fault find and correct any errors that you may have introduced. Please email me for more information.




 

CSS play Testimonial

"The citroen.co.uk web site uses
CSS play code for the site drop down menus.
This code works seamlessly in IE 6 & 7, and is an excellent solution to the Creative requirements of the site."

Damon Clark - Brandwidth