0

What's wrong with this code? It doesn't show me anything on the screen:

 <!DOCTYPE HTML>
 <head>
    <title> CSS(lab2) </title>
    <meta charset="UTF-8">
      <style type = "text/css">
        .bg {
         background-image: url("charlesb.jpg");
         background-color: green;
       }
      </style>
 </head>
 <body>
    <div class= "bg">

    </div>
 </body>
2
  • 2
    set width and height for div.bg (or fill it by text for test) and check the path to image. Commented Mar 9, 2016 at 20:18
  • 2
    Probably because your <div> is empty and therefore has no height and width. Set it explicitly. Commented Mar 9, 2016 at 20:18

2 Answers 2

1

If you know the size of your background image (e.g. 300x200) you can do this:

 <!DOCTYPE HTML>
 <head>
    <title> CSS(lab2) </title>
    <meta charset="UTF-8">
      <style type = "text/css">
        .bg {
         background-image: url("charlesb.jpg");
         background-color: green;
         width: 300px;
         height: 200px;
       }
      </style>
 </head>
 <body>
    <div class= "bg">

    </div>
 </body>

Simply add width and height statements into your css style.

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

Comments

1
<!DOCTYPE HTML>
 <head>
    <title> CSS(lab2) </title>
    <meta charset="UTF-8">
      <style type = "text/css">
        .bg {
         background-image: url("charlesb.jpg");
         background-color: green;
       }
      </style>
 </head>
 <body>
    <div class= "bg">
It needs something inside the div
    </div>
 </body>

You must have content inside the div to have the background color/image show.

1 Comment

This is not necessarily true. Your div needs to have dimensions, which are 0,0 by default when the div is empty, but you could just give the div dimensions without giving the div content.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.