-1

So I tried displaying an image by making a selector class with a background-image attribute then calling it using the <div> tag. The first time the selector class was between the <head> tags. Everything seems to work fine:

    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
        <style>
            body, html {
                margin: 0;
            }

            .bg {
                /* The image used */
                background-image: url("images.jpeg");

                /* Full height */
                height: 100%;

                /* Center and scale the image nicely */
                background-position: center;
                background-size: cover;
           }
        </style>
    </head>
    <body>
        <div class="bg"></div>
    </body>
    </html>

But when I put the selector class inside a separate CSS file, it doesn’t seem to work. Here is the code for the HTML file:

    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
        <style>
            body, html {
                margin: 0;
            }
        </style>
    </head>
    <body>
        <div class="bg"></div>
    </body>
    </html>

Here’s for the CSS:

.bg {
   /* The image used */
   background-image: url("images.jpeg");

   /* Full height */
   height: 100%;

   /* Center and scale the image nicely */
   background-position: center;
   background-size: cover;
}

Could someone please kindly tell me what I did wrong? I thank you in advance!

2
  • So where did you include the separate css file in your html? Commented Apr 5, 2020 at 17:51
  • May be, you are storing the CSS file inside a subfolder. If that's the case, then, in respect to the CSS file's location, the image file's path has to be specified. If the CSS file is inside 1 subfolder then background-image: url("../images.jpeg"); will do the job. I think you are stuck here, bro. All the best. Commented Apr 5, 2020 at 17:57

2 Answers 2

0

Don't forget to import your separate css file on your html page :

<link rel="stylesheet" href="style.css">

And be careful for the right path.

Sign up to request clarification or add additional context in comments.

2 Comments

Avoid answering questions that will be closed as a problem that couldn't be reproduced
Ok @AnuragSrivastava, thanks for the advice, it is better ?
0

Add a link tag inside of head in your HTML to connect your styles to the HTML.

<head>
   <link rel="stylesheet" href="xxx.css">
</head>

Replace xxx.css with the path to your css file.

Check the Mozilla docs for details.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.