100% width background image with an 'auto' height

后端 未结 8 1188
慢半拍i
慢半拍i 2020-12-04 08:20

I\'m currently working on a mobile landing page for a company. It\'s a really basic layout but below the header there\'s an image of a product which will always be 100% widt

8条回答
  •  离开以前
    2020-12-04 09:05

    You can use the CSS property background-size and set it to cover or contain, depending your preference. Cover will cover the window entirely, while contain will make one side fit the window thus not covering the entire page (unless the aspect ratio of the screen is equal to the image).

    Please note that this is a CSS3 property. In older browsers, this property is ignored. Alternatively, you can use javascript to change the CSS settings depending on the window size, but this isn't preferred.

    body {
        background-image: url(image.jpg); /* image */
        background-position: center;      /* center the image */
        background-size: cover;           /* cover the entire window */
    }
    

提交回复
热议问题