Search code examples
htmlcssbackground-imagestylesheetdocument-body

Background image not appearing HTML CSS


I am new to html/css and I do not understand why my background image is not showing up. This is part of a simple test CSS sheet and HTML code for a simple site. I am trying to add a background-image and I have done this once and it worked and I do not know why it does not work now.

body{
background: url (rio.jpg); 
background-size: cover; 
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover; 
}

This is part of my css sheet. THe url works, I have used it for another trial site before. And my html is just a regular html document with a body etc..


Solution

  • You can try doing the following:

    background-image: url(http://www.example.com/images/bck.png); // absolute path
    
    background-image: url(rio.jpg); // relative path
    

    If you are stating that the path is just rio.jpg, the image should be in the same directory as the stylesheet/where you are declaring the background.

    If the image is in an image directory, you may need to go up a level as follows:

    background-image: url(../images/rio.jpg);
    

    Depends where the image file is located. You can refer to following for further information: https://developer.mozilla.org/en-US/docs/Web/CSS/background-image

    Hope this helps!