Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to scale a background image without losing proportions

How to scale a background image without losing proportions


I want to create a web page Background image that fills the entire background, without distorting the image and changing it's proportions.

You can see a great implementation of this on LaunchRock's pages, and on their homepage, for example.

I saw this question on StackOverflow however if you play around with what that produces you'll see that the background image is 100% height and 100% width which means that the original proportions of the photo are not maintained and the image is stretched.

Notice in the LaunchRock example that if you resize your browser window the image grows proportionally and always fills the entire window (no matter what size window or background image they use).

If the browser window is not wide enough it crops the sides of the background image (keeping the image centered and cropping the left and right sides) and if the window is not high enough it crops the bottom part of the background image.

No matter what size the browser window is, the image height/width ratio is maintained and the entire background is filled.

I'm guess this can't be done with pure CSS, some JavaScript may be needed. Any ideas?


Problem courtesy of: Guy


It is done with CSS only, like this:

body {    
    background: black url(/images/back.jpg) no-repeat top center fixed;
    -webkit-background-size: cover;
    background-size: cover;

Things that make it possible:

  • CSS property background-size set to cover and its vendor prefixed versions
  • using a background-image that fades to black on the sides
  • position set to top center
  • using the fixed property
Solution courtesy of: Moin Zaman


View additional discussion.

This post first appeared on CSS3 Recipes - The Solution To All Your Style Problems, please read the originial post: here

Share the post

How to scale a background image without losing proportions


Subscribe to Css3 Recipes - The Solution To All Your Style Problems

Get updates delivered right to your inbox!

Thank you for your subscription