Luchadora
Luchadora

Reputation: 63

CSS viewport height:100vh doesn't work correctly with content in the div

I'm new here and registered because I couldn't find my answer in the existing treads.

I'm trying to make a one-page website, and I want the 'landing page' to have a full background. I had the background on the whole page, but when I started placing text in the div, it broke. I used the following css (sass):

.intro {
    color: #fff;
    height: 100vh;
    background: url('http://www.flabber.nl/sites/default/files/archive/files/i-should-buy-a-boat.jpg') no-repeat center center; 
    background-size: cover;

    &--buttons {
        position: absolute;
        bottom: 2em;
    }
}

p.hello {
    margin: 30px;
}

.page1 {
    color: #fff;
    height: 100vh;
    background: beige;
}

.page2 {
    color: #fff;
    height: 100vh;
    background: black;
}

With the following HTML:

<html>
    <head>
        <link href="stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css" />
        <title>My title</title>
    </head>

    <body>
        <div class="container">
            <div class="row">

                <div id="intro" class="intro">

                    <div class="text-center">
                        <p class="hello">
                            Intro text is coming soon!
                        </p>
                            <div class="col small-col-12 intro--buttons">
                                <p>Buttons coming here.
                                </p>
                            </div>
                    </div>

                </div>

                <div id="page1" class="col col-12 page1">
                    <p>Tekst test</p>
                </div>

                <div id="page2" class="col col-12 page2">
                    <p>Tekst test.</p>
                </div>

            </div>
        </div>
    </body>
</html>

You can see the result here: http://codepen.io/Luchadora/full/waYzMZ Or see my pen: http://codepen.io/Luchadora/pen/waYzMZ

As you see, when positioning the intro text (p.hello {margin: 30px;}, the background size changed and is not full screen anymore. (Btw: I used an example background). Also the other pages have white spaces now..

How can I fix this? I've read articles about viewports, but I think the only thing I need for this is height: 100vh;, right? Thanks in advance!

Upvotes: 6

Views: 22704

Answers (3)

Alexander McNulty
Alexander McNulty

Reputation: 890

This was already said above in a side conversation, but it worked for me and I want people to find it easily.

-- overflow-x:hidden; on both html, body in the css.

Upvotes: -1

Luuk van Aggelen
Luuk van Aggelen

Reputation: 65

You have not disabled the margin of the body. Add this to your CSS

body{
  margin:0px;
}

edit: The answer Pete posted seems to be the complete solution, sorry was too slow!

Upvotes: 0

Pete
Pete

Reputation: 58432

Your problem is with margin collapsing:

Parent and first/last child
If there is no border, padding, inline content, or clearance to separate the margin-top of a block with the margin-top of its first child block, or no border, padding, inline content, height, min-height, or max-height to separate the margin-bottom of a block with the margin-bottom of its last child, then those margins collapse. The collapsed margin ends up outside the parent.

To solve your problem, add some padding to .text-center:

.text-center {padding:1px;}

Updated pen

Upvotes: 2

Related Questions