Fullscreen responsive background image in CSS

2019-01-03 10:47发布

I want to make this image as the background image of my webpage. However , the problem is that the image doesn't cover the whole of the page, as you may see in this preview , and it gets repeated at the rightmost end . Is there a way so that the image covers the whole page (be it any way , stretching , re-sizing , or something new).

My Code:

<!DOCTYPE HTML>
<html>

    <head>
        <title>Angry Birds Star Wars</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
        <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
        <style>
            body {
                background: url('http://www.androidtapp.com/wp-content/uploads/2012/11/Angry-Birds-Star-Wars-Menu.png');
            }
        </style>
    </head>

    <body></body>

</html>

Thanks.

UPDATE:

I have now finally come to believe that there is almost nothing that can get that image to fit exactly to my PC screen. I am now ok , and am moving further , please don't post answers now.

5条回答
家丑人穷心不美
2楼-- · 2019-01-03 11:15

Set this for your background image

background-size: cover
查看更多
欢心
3楼-- · 2019-01-03 11:19

Use the background-size attribute and set the background repeat to no-repeat.

Like this:

body { 
        background: url('http://www.androidtapp.com/wp-content/uploads/2012/11/Angry-Birds-Star-Wars-Menu.png') no-repeat center center fixed;
        -webkit-background-size: cover;
        -moz-background-size: cover;
        -o-background-size: cover;
        background-size: cover;
}

See this jsFiddle

查看更多
啃猪蹄的小仙女
4楼-- · 2019-01-03 11:24

You can always use a absolute position div to 100% width and height with z-index so that it can be a background, then you can insert the image with same css for it. thanks

查看更多
一纸荒年 Trace。
5楼-- · 2019-01-03 11:31
background-size: 100% 100%;
background-position: top left;
background-repeat: no-repeat;

should do the trick. Notice - you can combine position & repeat into the initial background attribute

And apply

html, body { min-height: 100%; min-width: 100%; }
查看更多
仙女界的扛把子
6楼-- · 2019-01-03 11:40

jsBin demo

background: url(image.jpg) fixed 50%;
background-size: cover;                 /* PS: Use separately here cause of Safari bug */

The fixed (background-attachment) is optional.


The above is just a shorthand for:

background-image      : url(image.jpg);
background-attachment : fixed;
background-position   : 50% 50%;     /* or: center center */
background-size       : cover;       /* CSS3 */

You've could in all modern browsers but Safari use:

background: url(image.jpg) fixed 50% / cover;

where the / is used in background shorthand to separate and distinguish position from size (cause position accepts both single and multiple (X,Y) values), but Safari has a bug with this / shorthand so use as described above.

查看更多
登录 后发表回答