Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css background image resize width crop height

Tags:

css

I am using this code from css tips and tricks to cover a background image. The problem is that it rescales the image to fit both width and height and changes the aspect ratio. I want the background image to rescale to full screen width and then crop the height only (starting from the top of the image, not the center) to cover the view port. In this way, the image aspect ratio will be maintained.

A secondary problem I have is that it doesn't seem to work unless I use the FQDN for the image instead of just the url below.

html {
  background: url(images/bg.jpg) no-repeat center center fixed; 
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}
like image 356
markhorrocks Avatar asked Jun 27 '12 03:06

markhorrocks


People also ask

Can you resize a background image in CSS?

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

Instead of

background-size: cover;

you will want to use

background-size: 100% auto;

Cover will stretch the image to the smallest size such that both its width and its height can fit inside the content area, hence your issue. See more about that over here.

like image 134
Etheryte Avatar answered Nov 09 '22 00:11

Etheryte