css - how to stretch and auto-resize background im

2020-02-11 03:09发布

I'm trying to get my background image to stretch across the entire page, but so far i have this:

enter image description here

This is the picture i want stretched across the browser window:

enter image description here

My external CSS contains the code below:

hr {color:sienna;}
p {margin-left:20px;}
body {background-image:url("mybackground.jpg")} 

Can anyone tell me how I can edit the CSS file to fix my problem?

标签: html css
6条回答
仙女界的扛把子
2楼-- · 2020-02-11 03:43

You can use:

background-size: cover;

However please bear in mind which browsers this supports. For example, this won't work in IE8 and below.

查看更多
啃猪蹄的小仙女
3楼-- · 2020-02-11 03:44

You need to make this an <img> tag, with a low z-index, with position: fixed and then for the <img> tag, use height: 100%; width: auto or the opposite.

This method is cross-compatible for all browsers.

查看更多
女痞
4楼-- · 2020-02-11 03:45

Have you tried using

background-size: 10px 10px

Change 10 to the width and height of the body

You can also do

background-size: 100%
查看更多
Juvenile、少年°
5楼-- · 2020-02-11 03:46

Another option would also include:

body{
   background-image:url("mybackground.jpg")
   background-size:contain;
   background-repeat:no-repeat;
}
查看更多
家丑人穷心不美
6楼-- · 2020-02-11 03:56

Background size will do the trick:

    body { 
     background: url(images/bg.jpg) no-repeat center center fixed; 
     -webkit-background-size: cover;
     -moz-background-size: cover;
     -o-background-size: cover;
     background-size: cover;
   }

Check this out for more info: http://css-tricks.com/perfect-full-page-background-image/

查看更多
smile是对你的礼貌
7楼-- · 2020-02-11 03:58

Use background-size: cover. Note its browser support.

MDN documentation.

查看更多
登录 后发表回答