Andrew Eddy
Andrew Eddy

Reputation: 31

Unexplained Gap Between <header> and <main>

I can not seem to get rid of this space that exists between my header and main content. You will see a black background above the background image and below the navigation. Any help would be great.

<!doctype html>
<html>
    <head>
    <meta charset="utf-8">
    <title>Awesome Landing Page</title>
    <link rel="stylesheet" type="text/css" href="CSS/style.css">
    </head>
<body>
    <header>
        <div id="header-container">
            <div class="col-3">
                <img class="logo" src="Images/logo-dark.png" height="18" width="143">
            </div>
            <div class="col-3">
            <nav>
                <ul class="menu">
                    <li>Home</li>
                    <li>Features</li>
                    <li>About</li>
                    <li>Signup</li>
                </ul>    
            </nav>
            </div>
            <div class="col-3">
                <a href="#" class="getStartedBTN">Get Started</a>
            </div>
        </div><!-- Header-Container Ends Here -->
    </header>
    <main>
    <section>
        <div id="top-section-main">
            <div id="top-section-content">
                <h1>Awesome looks so good</h1>
                <p>Awesome is the landing page you wish you had when you started.</p>
            </div>
        </div>    
    </section>
    <section></section>
    <section></section>
    <section></section>
    <section></section>
    <section></section>
    </main>
    <footer></footer>
</body>
</html>

CSS

html {
    font-family: "Lato", "Helvetica Neue", "Helvetica", "Arial", sans-serif;
}
body {
    background-color: black; 
    margin: 0;
}
header {
    height: 80px;
    background-color: #ffffff;
}
#header-container {
    width: 75%;
    margin: auto;
}
.col-3 {
    width: 33%;
    float: left;
}
.logo {
    padding-top: 30px;   
}
.menu {
    margin-top: 32px;  
}
.menu li {
    list-style-type: none;
    display: inline;
    font-size: .85em;
    color: #8e8e8e;
    padding-right: 30px;
}
.getStartedBTN {
    background-color: #6dc77a;
    border-radius: 28px;
    -moz-border-radius:28px;
    -webkit-border-radius:28px;
    text-decoration: none;
    color: #ffffff;
    padding: 10px 26px;
    margin-top: 20px;
    display: inline-block;
    font-size: 17px;
    float: right;
}
#top-section-main {
    height: 740px;
    background-image: url(../Images/friends.jpg);
}

Upvotes: 3

Views: 5964

Answers (2)

Mr Lister
Mr Lister

Reputation: 46589

(sigh) Collapsing margins again. The margin of the h1 is shared by the section it sits in, causing the section to start lower on the screen than expected.

Solution: remove the margin from the h1.

h1 {margin:0}

See fiddle

Upvotes: 0

TreeTree
TreeTree

Reputation: 3230

This is caused by the top margin of your <h1> located in <div id="top-section-content">. This is easily reproduced in the below snippet.

body {
    background:gray;
}

header {
    height:50px;
    background:orange;
}

section {
    background:red;
}

#fixed-1 section {
    padding-top:1px;
}

#fixed-2 h1 {
    margin-top:0;
    padding-top:16px;
}
<div>
    <header></header>
    
    <section>
        <h1>hello i'm broken</h1>
    </section>
</div>

<div id = "fixed-1">
    <header></header>
    
    <section>
        <h1>hello i'm fixed by a padding on the container!</h1>
    </section>
</div>

<div id = "fixed-2">
    <header></header>
    
    <section>
        <h1>hello i'm fixed by replacing the margin with a padding!</h1>
    </section>
</div>

You can solve this by replacing the top margin with a top padding instead or you can add a top padding of 1 pixel to its container.

Upvotes: 3

Related Questions