Note that there are some explanatory texts on larger screens.

plurals
  1. POScale background image so that it always keeps its aspect ratio and stays centered
    primarykey
    data
    text
    <p>I'd like to set a webpage background image to scale with the browser window so that it never loses its original aspect ratio (becomes stretched), and so that the image itself stays basically centered. After the window reaches a small enough size, I want the image to overflow (disappear) on both the left and right sides, not just the right side, as it does by default if the image is absolutely positioned.</p> <p>Here is an example of what I'm doing right now: <a href="http://jsfiddle.net/S59EW/2/" rel="nofollow">http://jsfiddle.net/S59EW/2/</a></p> <pre><code>#background img { position: absolute; min-height:100%; width: 100%; height: auto; top: 0; left: 0; } </code></pre> <p>(The image has to be within a div positioned absolutely because of some javascript I'm using that applies to it.)</p> <p>If you resize the jsfiddle window you'll see that the image keeps its aspect ratio only if you don't make the window too tall. Then the image is stretched vertically.</p> <p>And if you remove "height: auto" you get the same thing except the image stops resizing after a certain point and disappears on the right/bottom sides but not on the top/left sides.</p> <pre><code>#background img { position: absolute; min-height:100%; width: 100%; top: 0; left: 0; } </code></pre> <p>So, I need:</p> <p>The background image to always occupy the entire window without scrollbars. The image to always keep aspect ratio. The image to overflow onto the left and right side after a certain browser size threshold, so that it remains basically centered.</p> <p>Thanks everyone</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload