Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing background-image always proportionally to scale with a center point

I want to be able to reproduce the behavior of this website's image when resizing.

It seems like the background-image has a center point where the image start to crop when it cannot keep its proportion scale.

Here is an example:

http://themes.evgenyfireform.com/wp-jad/

For now my background has the following css:

#bg {
    position: fixed;
    top: 0;
    left: 0px;
    width:100%;
}

The problem is that it's fixed and I want it to crop when the image can't be scale.

like image 955
Sam Bellerose Avatar asked Aug 19 '13 22:08

Sam Bellerose


People also ask

How do you resize a photo proportionally?

Image resizing with the pointer toolBy clicking and dragging on these handles, the object is resized by moving the edge or corner clicked on to adjust the width and/or height of the selected object. If the Shift key is held down whilst moving the handle, then the proportions of the object will be preserved.

How do I automatically resize a background image?

Use background-size property to cover the entire viewport The CSS background-size property can have the value of cover . The cover value tells the browser to automatically and proportionally scale the background image's width and height so that they are always equal to, or greater than, the viewport's width/height.

How do you scale a proportionally image in CSS?

In that situation we can use CSS max-width or width to fit the image. Use max-width: 100% to limit the size but allow smaller image sizes, use width: 100% to always scale the image to fit the parent container width.

What property allows a background image to scale up or down?

The background-size CSS property lets you resize the background image of an element, overriding the default behavior of tiling the image at its full size by specifying the width and/or height of the image. By doing so, you can scale the image upward or downward as desired.


1 Answers

You are looking for this combination:

background-position:50% 50%;  /* Sets reference point to scale from */
background-size:cover;        /* Sets background image to cover entire element */

Working sample fiddle here.

Note that this is not supported by IE8 and will require JS hackery there if you need IE8 or older to be supported.

like image 174
Niels Keurentjes Avatar answered Oct 05 '22 22:10

Niels Keurentjes