0

I am trying to do a header on my page in which I want to show an image. To do the header I use a div:

<div id = "header">
</div>

And I want that an image will be on the background of that div so I put on the CSS:

#header{
    background-image: url('img/cars.jpg');
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-size: 100%;

    top: 0px;
    height: 150px;
}

But it is shown like if the image had been cut. I tried replacing background-size: 100%; to background-size: cover; but the efect it is the same.

I also tried adding these lines:

-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;

but the result are the same.

If I try to get it with background-size: contain the image is being reduced from the right.

What can I do to see the full image on my div?

P.S: I have seen this question css - how to stretch and auto-resize background image but the answers provided did not change anything in my case.

EDIT: Here you can see the effect.

Thanks in advance!

Community
  • 1
  • 1
Francisco Romero
  • 12,787
  • 22
  • 92
  • 167

3 Answers3

0

Use this line of code:

background-size: 100% 150px;

So the width is 100% but the height always remains 150px.

Remember that this will stretch your picture if you don't give a fixed width to the header.

Here a fiddle: https://jsfiddle.net/shbLd8v3/

Marcel Wasilewski
  • 2,519
  • 1
  • 17
  • 34
0

Try the below if this is what you want,

body {
  padding: 0px;
  margin: 0px;
}
#header {
  background-image: url('http://www.disney.es/sites/default/files/styles/retina-reduction/public/Cars/GENERIC/Section%20Listings/ICE/msf_cars_ice_lst_characters.jpg');
  height: 100%;
  background-position: center;
  background-size: 100% 100%;
  background-repeat: no-repeat;
  -webkit-transition: 800ms all ease;
  -moz-transition: 800ms all ease;
  -ms-transition: 800ms all ease;
  transition: 800ms all ease;
  background-color: #fff;
  min-height: 150px;
  width: 100%;
  padding-top: 10%;
  margin: 0px;
}
<div id="header">
</div>
jlocker
  • 1,478
  • 1
  • 12
  • 23
0
 background-repeat: repeat-x; 

Try this and you can use background-position: 100%;

Nisha Sharma
  • 245
  • 1
  • 2
  • 13