Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Div Background Image Fixed Height 100% Width

Tags:

html

css

I'm trying to setup a series of div's with a background image that each have their own fixed height, and stretch to fill up the width, even if there is overflow on the top/bottom that is clipped. I just don't want the white space on the edges.

Currently, I have: http://jsfiddle.net/ndKWN/

CSS

    #main-container {         float:left;         width:100%;         margin:0;         padding:0;         text-align: center;     }      .chapter{         position: relative;         height: 1400px;         z-index: 1;     }      #chapter1{     background: url(http://omset.files.wordpress.com/2010/06/homer-simpson-1-264a0.jpg) 50% 0 no-repeat fixed;         height:1200px;     }      #chapter2{     background: url(http://download.ultradownloads.com.br/wallpaper/94781_Papel-de-Parede-Homer-Simpson--94781_1680x1050.jpg) 50% 0 no-repeat fixed;         height:1200px;     } 
like image 881
TheButlerDidIt Avatar asked Mar 30 '13 00:03

TheButlerDidIt


People also ask

How do I make the background image fit my screen CSS?

Using CSS, you can set the background-size property for the image to fit the screen (viewport). The background-size property has a value of cover . It instructs browsers to automatically scale the width and height of a responsive background image to be the same or bigger than the viewport.

How do I make my background image full screen responsive?

A simple way to create a responsive full-screen background image is to set a background image that covers the entire window – body { width: 100vw; min-height: 100vh; background: url("IMAGE"); background-size: cover; } .

How do you autofit a background image in HTML?

There are four different syntaxes you can use with this property: the keyword syntax ("auto", "cover" and "contain"), the one-value syntax (sets the width of the image (height becomes "auto"), the two-value syntax (first value: width of the image, second value: height), and the multiple background syntax (separated ...


2 Answers

See my answer to a similar question here.

It sounds like you want a background-image to keep it's own aspect ratio while expanding to 100% width and getting cropped off on the top and bottom. If that's the case, do something like this:

.chapter {     position: relative;     height: 1200px;     z-index: 1; }  #chapter1 {     background-image: url(http://omset.files.wordpress.com/2010/06/homer-simpson-1-264a0.jpg);     background-repeat: no-repeat;     background-size: 100% auto;     background-position: center top;     background-attachment: fixed; } 

jsfiddle: http://jsfiddle.net/ndKWN/3/

The problem with this approach is that you have the container elements at a fixed height, so there can be space below if the screen is small enough.

If you want the height to keep the image's aspect ratio, you'll have to do something like what I wrote in an edit to the answer I linked to above. Set the container's height to 0 and set the padding-bottom to the percentage of the width:

.chapter {     position: relative;     height: 0;     padding-bottom: 75%;     z-index: 1; }  #chapter1 {     background-image: url(http://omset.files.wordpress.com/2010/06/homer-simpson-1-264a0.jpg);     background-repeat: no-repeat;     background-size: 100% auto;     background-position: center top;     background-attachment: fixed; } 

jsfiddle: http://jsfiddle.net/ndKWN/4/

You could also put the padding-bottom percentage into each #chapter style if each image has a different aspect ratio. In order to use different aspect ratios, divide the height of the original image by it's own width, and multiply by 100 to get the percentage value.

like image 179
cr0ybot Avatar answered Oct 02 '22 09:10

cr0ybot


http://jsfiddle.net/ndKWN/1/

You can use background-size: cover;

like image 23
Juan Rangel Avatar answered Oct 02 '22 09:10

Juan Rangel