如何使用窗口大小缩放我的css背景图像?

时间:2017-06-15 21:57:39

标签: html css responsive-design

我正在构建我的第一个实时网站,我注意到背景图像没有随窗口缩小,然后调整大小。网站的其余部分会缩放,但背景图像看起来就像是全屏。我只能假设这意味着它不会完全可见,但在较小的设备中切断。是什么导致这种情况发生,我该如何解决?非常感谢帮助,谢谢

我刚刚发布了我的CSS的一般部分,整个网站共享的非响应部分,但休息是在jsfiddle https://jsfiddle.net/zd00nmce/5/

    html,body{
        min-height: 100vh;
    }
    body{
        margin: 0px;
        background-image: linear-gradient( 0, rgba(0,0,0,.8) 30%, rgba(0,150,255,.8) 100%), url("images/mainCover.jpg");
        background-repeat: no-repeat;
        background-size: cover;
    }
    img{
        max-width: 100%;
    }
    h1,h2,h3{
        font-family: arial;
        text-align: center;
        margin: 20px auto;
    }
    ul{
        list-style: none;
        text-align: center;
        padding: 0;
    }
    a:visited,a{
        color: black;
    }
    section{
        margin: 5%;
        color: white;
    }
    section > article{
        padding: 1em;
        background-color: rgba(0,0,0,.5);
        border-radius: 15px;
        margin: 10px;
    }

    section h2, section h3{
        text-align: center;
    }
    section h2{
        font-size: 1.3em;
        text-shadow: 3px 3px 3px rgba(0,0,0,.5);
    }
    section h3{
        font-size: 1.1em;
    }
    /*nav and header*/
    header{
        background-color: rgba(255,165,0,.8);
        display: flex;
        flex-flow: row wrap;
        justify-content: center;
        border-bottom: 8px solid black;
    }

    header h1{
        text-align: center;
        margin: 0;
        padding: 15px;
        font-size: 1.5em;
        text-shadow: 5px 5px 5px rgba(0,0,0,.3);
    }
    header h1, nav a{
        font-weight: 700;
        font-family: arial;
    }

    header nav{
        position: fixed;
        bottom: 0;
        width: 100%;
        z-index: 1;
       // background-color: rgba(255,165,0,.8);
    }
    .main-nav{
        margin: 0;
        width: 100%;
        color: white;
        font-family: arial;
        font-weight: 600;
    }
    nav ul li{ 
        text-align: center;
        border: 1px solid white;
        border-radius: 15px;
        font-size: 1.2rem;
        background-color:rgba(255,165,0,.8);
        width: 100%;
    }
    .drop-menu-back{
        display: none;
        width: 100%;
    }
    a{
        text-decoration: none;
    }

     nav a:visited, nav a,h1{
       color: white; 
    }

    .main-nav .current-page { 
        background-color: rgba(0,0,0,.5);
    }

        /****drop down menu****/
    .characters:hover {
                position: relative;
                border-radius: 8px 8px 0 0;
                }

         .drop-menu{
                position: absolute;
                visibility: hidden;
                display: block;
                top: 38px;
                white-space: nowrap;
                left: -2px;
                background-color: rgba(255,165,0,.8);
                border: 1px solid rgba(0,0,0,.02);
                box-shadow: 5px 5px 5px 2px rgba(0,0,0,.3);
                opacity: 0;
                transition: opacity 300ms ease-in-out 0s;
                z-index: 1; 
                }
        .characters:hover .drop-menu{
                 visibility: visible;
                 opacity:1;
            }

2 个答案:

答案 0 :(得分:1)

尝试将此添加到css中的正文:

background-position: center center;

这将使您的图像居中,并在调整大小时保持纵横比。

//拉斯

答案 1 :(得分:1)

CSS Tricks对此有一个有用的解决方案。

https://css-tricks.com/perfect-full-page-background-image/

html { 
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;
}

我相信这应该是你正在寻找的东西。