Home > Blockchain >  How do I fix this code for html background
How do I fix this code for html background

Time:08-07

<!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 ></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.

CodePudding user response:

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');

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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');
  •  Tags:  
  • html
  • Related