Search code examples
htmlcssimagecss-selectorsexternal

How can one use a selector class that is written in a separate CSS file?


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!


Solution

  • 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.