-1
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
    </head>
    <body>
        <div class="background-image"></div>
    </body>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        .background-image {
            background-image: url('./C:\Users\viraj\Downloads\html\Mountains.jfif');
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
            height: 100vh;
        }    
    </style>
</html>

Whenever I run this code on my browser(Chrome) my background image won't show up. I don't know how to fix it so if anyone knows how to fix it, please tell me.

  • Try changing `\ ` to `/` ... – Déjà vu Aug 06 '22 at 10:53
  • 1
    Does this answer your question? [How to add background image in css?](https://stackoverflow.com/questions/36170310/how-to-add-background-image-in-css) – caramba Aug 06 '22 at 11:12
  • The daily "Why doesn't my image show?" question. – Rob Aug 06 '22 at 12:27
  • Also, the leading **`./`** won't work. For a local file you might want to add `file://` instead of the regular `https://`. While `/` (slashes) are the required characters to use, browsers will likely accept `\\` (back-slashes). – Rene van der Lende Aug 06 '22 at 12:41

4 Answers4

1

There are two issues with this line:

background-image: url('./C:\Users\viraj\Downloads\html\Mountains.jfif');
  1. ./ at the beginning of the url specifies a relative path, so the browser will look for the file in same directory in which your html file resides. You however need to omit ./ so that the browser will use the absolute path.

  2. You need to escape the backslashes in the URL like this: '\\'

So the correct style would look like this:

background-image: url('C:\\Users\\viraj\\Downloads\\html\\Mountains.jfif');
PiFanatic
  • 233
  • 1
  • 4
  • 14
1

use this instead

background-image: url('C:\\Users\viraj\Downloads\html\Mountains.jfif');

it would be better if you save image in same file where your index.html style.css are and then use reference address for background

.background-image {
        background-image: url('\Mountains.jfif');
        background-size: cover;
        background-position: center;
        background-repeat: no-repeat;
        height: 100vh;

}

for more refer to https://www.w3schools.com/cssref/pr_background-image.asp

vksssd
  • 67
  • 7
0

try this background-image: url('C:\Users\viraj\Downloads\html\Mountains.jfif');

Doom
  • 1
  • 1
0

one of pecularity about file path in windows and mac is that in mac each of folder are seprated by a /[forwardslash] whereas on windows they seprated by a \ backslash as you notice when we are writing code we care about that we can just write it as /[forward slash] and making sure that we have the root

solution:


There are two issues with this line:

background-image: url('C:/Users/viraj/Downloads/html/Mountains.jfif');
h sha
  • 186
  • 2
  • 5