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

房东的猫 提交于 2020-01-12 07:34:46

问题


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 only be able to notice this if your screen needs a scrollbar to view the website--otherwise it looks fine.

I have the overlaying div set to:

position: absolute;
height: 100%;
width: 100%;

I also have body set to:

height: 100%;
margin: 0px 0px;
padding: 0px; 0px;

But for some reason on my laptop if I scroll down I see the edge of the DIV and nothing is blocked off underneath it.

The purpose of this is to create a custom lightbox for the website. I have disabled the Javascript that dims the box so you can see the effect.

How do I make it cover the entire page, not just the viewport?

(My laptop is a 1366x768, so presumably you'd see it on anything with a height resolution <= 760).


回答1:


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;



回答2:


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.




回答3:


try to add this css:

html {
height: 100%;
margin 0;
}



回答4:


<!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>


来源:https://stackoverflow.com/questions/4343612/make-div-cover-100-of-viewport-instead-of-100-of-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!