简体   繁体   中英

background-image with css

Why the background image not working with

DEMO

HTML

<div class="container" style="background-color: red;"></div>

CSS

.container {
  width: 100%;
  height: 385px;
  background-image: url(http://fc08.deviantart.net/fs71/i/2013/082/a/c/png_grass_by_moonglowlilly-d5z1o5t.png);
  background-position: center top;
  background-size: 100% auto;
  background-size: cover;
  border-bottom: 1px solid #3f4858;
  background-repeat: repeat-x;
  left: 0px;
  top: 0px;
}

Apart from the already mentioned spelling error


The problem here is with the size of your container and the size of your background image. Your image has a lot of space on top and you will see nothing for the size of you container. Check if you change the background-size property to:

background-size: 100% 100%;

Demo

For the porperty background-size you can also use contain :

the background image should be scaled to be as large as possible while ensuring both its dimensions are less than or equal to the corresponding dimensions of the background positioning area

AnotherDemo

Your width: 100% depends on width of parent element. And its not set. Thats why. So change your CSS to this:

.container{
  width:300px;
  height:385px;
  background-image:url(http://fc08.deviantart.net/fs71/i/2013/082/a/c/png_grass_by_moonglowlilly-d5z1o5t.png);
  background-position: center top;
  background-size: 100% auto;
  background-size:cover;
  border-bottom:1px solid #3f4858;
  background-repeat:repeat-x;
  left:0px;
  top:0px;
}

Also why you have in your HTML style="background-color:red;" ? Do you want to have as background image png_grass_by_moonglowlilly-d5z1o5t.png or red color? Or you want to have in transparency of that PNG image red color?

DEMO

Try This:

 .container{ width:300px; height:385px; background-image:url('http://fc08.deviantart.net/fs71/i/2013/082/a/c/png_grass_by_moonglowlilly-d5z1o5t.png'); background-position: center top; background-size: 100% auto; background-size:cover; border-bottom:1px solid #3f4858; background-repeat:repeat-x; left:0px; top:0px; } 
 <div class="container" style="background-color:red;"> 

The image is of size 1024x819px and the grass is starting from 232px from top so you need to position your background image or change your div.container size so as to fit the image within the given size.

Demo-1 with container size changed:

Demo-1

Demo-2 with image position changed with your code snippet only.:

Demo-2

just notice background-position I have changed to 0px -232px;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM