Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't my Background Image Show?

Tags:

I am having trouble getting a background image to show in a div and can't for the life of me see why...

This is the structure that I have:

Folder
      \style.css
      \index.html
      \Images
             \bookone.jpg

I want the bookone.jpg file to be the background of a div. So the CSS path would be Folder/style.css and the image path is Folder/Images/bookone.jpg. I have the below code in my html and css file but I get nothing when previewing it.

/* CSS */

.book {
  height: 300px;
  width: 300px;
  margin: 0px;
  padding: auto;
  border: 1px solid #000;
}

#bookone {
  background: url(..\Images\bookone.jpg) ;
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
}
<!-- HTML -->

<div id="bookone" class="book"></div>
like image 995
Mark R Avatar asked Nov 05 '16 03:11

Mark R


People also ask

Why is my background image not displaying?

Make sure the image path is set correctly in the background-image url. Once you have made sure that your CSS file is linked correctly, also check that the image itself is set correctly. Again, you will want to open your code inspector in the browser to check.

Why is my image not showing up in HTML?

There are several possible reasons why your images are not showing up on your pages as expected: The image file is not located in the same location that is specified in your IMG tag. The image does not have the same file name as specified in your IMG tag. The image file is corrupt or damaged.

Why is my background image URL not working?

Make Sure Your CSS File Is Properly Embedded in Your HTML File. The CSS file must be linked to the HTML file for the background images to be displayed or loaded correctly on the website. Furthermore, you must include the link tag in the HTML file to fix a problem such as a background-image URL not working.

How do I enable my browser background image?

To set the background image of a webpage, use the CSS style. Under the CSS <style> tag, add the property background-image. The property sets a graphic such as jpg, png, svg, gif, etc. HTML5 do not support the <body> background attribute, so CSS is used to change set background image.


1 Answers

  1. You should use slash (/) and not backslash (\).
  2. You should make sure the image is in the correct path (relatively to the current css/html file). If the images exists in the same location of your css file (or your html file) you shouldn't use the .., since it actually tells your browser to search for that image in the upper-folder.

This is the final css code you should probably use:

.book {
  height: 300px;
  width: 300px;
  margin: 0px;
  padding: auto;
  border: 1px solid #000;
}

#bookone {
  background: url(Images/bookone.jpg) ;
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
}
like image 77
Dekel Avatar answered Sep 22 '22 17:09

Dekel