Make DIV cover 100% of viewport instead of 100% of Body

前端 未结 4 2028
无人及你
无人及你 2021-02-14 10:22

My post is in regards to http://www.thepostboard.net

I need to make the black box that is currently covering 100% of the viewport cover the entire page instead. You\'ll

相关标签:
4条回答
  • 2021-02-14 10:34
    <!DOCTYPE html>
    <html>
        <head>
            <title>image background test</title>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="initial-scale = 1.0, user-scalable = no, maximum-scale=1">
            <style>
                body{
                    margin:0;
                    padding:0;
                    width:100%;
                    height:100%;
                    max-width:100%;
                    overflow-x:hidden;
                }
    
                article, aside, div, dt, figcaption, footer, form, header, hgroup, html, main, nav {
                    display: block;
                    width:auto;
                    height:100%;
                }
    
                .main{
                    position:static;/* Don't Change - Positioning */
                    top:0;
                    left:0;
                    height:100%;
                    max-height:100%;
                }
    
                .background{
                    position:absolute;/* Don't Change - Positioning */
                    top:0;
                    left:0;
                    right:0;
                    bottom:0;
                    background:url('images/beach.jpg');
                    background-position:center center;
                    background-repeat:no-repeat;
                    background-size:cover;
                    margin:0;
                }
    
                @media (min-aspect-ratio: 16/9){
                    .background{
                        position:absolute;/* Don't Change - Positioning */
                        width:100%;
                        height:auto; /* actually taller than viewport */
                        background:url('images/beach.jpg');
                        background-repeat:no-repeat;
                        background-position:center center;
                        background-size:cover;
                        margin:0;
                    }
                }
    
                @media (max-aspect-ratio: 16/9){
                    .background{
                        position:absolute;/* Don't Change - Positioning */
                        width:auto; /* actually wider than viewport */
                        height:100%;
                        background:url('images/beach.jpg');
                        background-repeat:no-repeat;
                        background-position:center center;
                        background-size:cover;
                        margin:0;
                    }
                }
    
                /* If supporting object-fit, overriding 'class="main"' default: */
                @supports (object-fit: cover){
                    .background{
                        background:url('images/beach.jpg');
                        background-position:center center;
                        margin:0;
                        top: 0;
                        left: 0;
                        right: 0;
                        object-fit: cover;
                    }
                }
    
                section h1{
                    width:80%;
                    height:auto;
                    background:#ff0000;
                    margin:20% auto;
                    text-align:center;
                    color:#ffffff;
                    font-size:200%;
                    font-weight:800;
                    line-height:200%;
                }
    
                h2{
                    text-align:center;
                    margin:0 auto;
                    padding:20%;
                    font-size:200%;
                    line-height:100%;
                    font-weight:800;
                }
            </style>
        </head>
        <body>
            <div class="main">
                <div class="background">
                    <section>
                        <h1>Image fits to viewport</h1>
                    </section>
                </div>
            </div>
            <h2>Some Text Here to display that image<br>is fixed toviewport</h2>
        </body>
    </html>
    
    0 讨论(0)
  • 2021-02-14 10:35

    The <div> needs to be

    top: 0;
    left: 0;
    position: fixed;
    width: 100%;
    height: 100%;
    background-color: red;
    

    It will then stay within the viewport's dimensions and "travel" with the scrollbar.

    Edit: it will only cover the entire viewport if <body> is styled to

    margin: 0;
    padding: 0;
    
    0 讨论(0)
  • 2021-02-14 10:38

    This solution has worked best for me:

    #overlay {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
    }
    

    No need to style any other elements except the tag.

    0 讨论(0)
  • 2021-02-14 10:48

    try to add this css:

    html {
    height: 100%;
    margin 0;
    }
    
    0 讨论(0)
提交回复
热议问题