Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: Stretching image to 100% width

Hi all I am trying to strech the background of my side to fit the width. I only found big workarounds in the internet. Is there not one single command for this?

body {
    background-image: url(money.jpg); 
}

Thanks for the answers. Doonot

like image 578
nimrod Avatar asked Mar 19 '11 14:03

nimrod


People also ask

How do I make an image 100 width in CSS?

width is set on the element in HTML. height (or width ) is set in the CSS — including using percentage values like max-width: 100%; width (or height ) is set to auto in the CSS.

How do I stretch image width in CSS?

You can use the CSS background-size: cover; to stretch and scale an image in the background with CSS only. This scales the image as large as possible in such a way that the background area is completely covered by the background image, while preserving its intrinsic aspect ratio.

How do I fix a stretched image in CSS?

1.1.When you added max-width: 100%; to any image, the width will be adjusted automatically based on the outer element width and you know what height: auto; does as it's self-explanatory. This is the fix we have been using for years to prevent image stretching in our sites.

How do I make a picture stretchable?

Press Ctrl + T (Windows) or ⌘ Cmd + T (Mac). This activates the Transform tool, so you'll be able to freely change the size of the photo. If you want to maintain the ratio, you can press ⇧ Shift as you drag the photo size.


2 Answers

Use background-size like the one used below,

body {
  background-image: url(money.jpg) no-repeat;
  background-size: 100%;
}  

Some useful links :

  • Scale background image style
  • Stretch and Scale a CSS image Background - With CSS only
like image 85
Saurabh Gokhale Avatar answered Sep 18 '22 14:09

Saurabh Gokhale


If you have a CSS3 browser you can use background-size (read more about background-size):

body {
    background-image: url(money.jpg); 
    background-size: 100%;
}

Unfortunately this won't work in IE 6-8. IE 9, Firefox, Chrome, Safari it will work fine.

like image 27
Keltex Avatar answered Sep 17 '22 14:09

Keltex