简体   繁体   中英

Background image is not showing up in div

I have tried everything and cannot figure out why I cannot get the background image to show up for this div.

**Here is the code:**

 body { margin: 0px; padding: 0px; } #top { background-image: url(../IMG/PINS.jpg) no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; } 
 <html> <head> <title>TEST BCKGRND IMAGE</title> <link rel="stylesheet" type="text/css" href="CSS/style.css"> </head> <body> <div id="top"> </div> </body> </html> 

The file structure is set up like this: I have a main folder called TEST_SITE, inside of that folder I have a folder for my CSS and a folder for my images called IMG.

I cannot for the life of me figure out why the background image is not showing up.

If anyone can please give me a heads up as to what might be wrong, I would truly appreciate it.

Thanks.

You need to set a height and width value from the #top and use the background-position:center center; background-repeat:no-repeat; background-position:center center; background-repeat:no-repeat; like my answer: for example

#top{
   width:500px;
   height:500px;
   background-image: url(YOUR IMAGE PATH); 
   background-repeat:no-repeat;
   background-position:center center;
   -webkit-background-size: cover;
   -moz-background-size: cover;
   -o-background-size: cover;
   background-size: cover;
}

or you can make it like this:

#top{ 
   -webkit-background-size: cover;
   -moz-background-size: cover;
   -o-background-size: cover;
   background-size: cover;
   background:url(YOUR IMAGE PATH) no-repeat fixed center; 
   width:500px;height:500px;
 }

setting the height of the #top div is important: if you set the width, and then the height to auto, then the background still won't show because there is nothing within the div to put a background to. However, if you force a height by setting one in the css, then the background will show. The code you have given for your path is incorrect because the background-image expects only a path to an image (and nothing else), whereas what you have given is suited to the background .

See my fiddle

It is because your div is empty. If you add content to it then you will see the background. You can also set a height or add padding with CSS.

<div id="top">
 <p>A large amount of text here</p>
</div>  

or

#top{
 height:400px;
 background-image: url(yourimagehere); 
 background-repeat:no-repeat;
 background-position:center center;
 -webkit-background-size: cover;
 -moz-background-size: cover;
 -o-background-size: cover;
 background-size: cover;
}

or

#top{
 padding: 25px 0;
 background-image: url(yourimagehere); 
 background-repeat:no-repeat;
 background-position:center center;
 -webkit-background-size: cover;
 -moz-background-size: cover;
 -o-background-size: cover;
 background-size: cover;
}

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