0

I have an html file, and I set the background url to an image, but the image does not fill the browser's width:

enter image description here

I also tried set the width property to make it wider, but it seems to have no effect.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #bg {

            height:1500px;
            background: url("img/timg7.jpg") center top no-repeat;
            width:1800px;
        }
    </style>

</head>
<body id="bg">

<div style="width:400px; height: 200px; background-color: antiquewhite">
    BLOCK
</div>

</body>
</html>
MJL
  • 161
  • 2
  • 7
qg_java_17137
  • 3,310
  • 10
  • 41
  • 84

6 Answers6

4

You need the background-size attribute for this.

Your CSS should be:

#bg {

    background: url("img/timg7.jpg");
    background-size: cover;
    background-repeat: no-repeat;

}
0

In this scenario, you should use background-size, there is a demo for background-size.

Try to use:

 background-size: 120%;
aircraft
  • 25,146
  • 28
  • 91
  • 166
0

Try to use: background-size: cover;

and please remove those height and width

0

Try this:

#bg {

     background: url("img/timg7.jpg") center top no-repeat;
     background-size: cover;
}
Ehsan
  • 12,655
  • 3
  • 25
  • 44
0
#bg{
  background: url("img/timg7.jpg");
  background-repeat:no-repeat;
  background-size:contain;
  background-position:center;
}
Anil Shrestha
  • 1,180
  • 11
  • 16
0

You don't need an id on your body, as document.body with get it with JavaScript, and you can just use body in the CSS. Of course, that's not your issue. See the code below:

html,body{
 padding:0; margin:0;
}
body{
  background:url(img/timg7.jpg) no-repeat; background-size:cover;
}

By the way, you should use external CSS, so it's cached into Browser Memory.

StackSlave
  • 10,613
  • 2
  • 18
  • 35