<header>和<main>之间的未解释间隙

3

我似乎无法消除标题和主要内容之间存在的空白。您将看到导航上方和背景图像下方存在黑色背景。任何帮助都将是极好的。

<!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);
}

如果你真的想要帮助,你需要展示你的标记。 - robbyphillips
我们在哪里可以看到代码或图像? - MTahir
嘿,不小心删掉了。刚刚编辑了。 - Andrew Eddy
2个回答

3
这是由于位于<div id="top-section-content">中的<h1>顶部边距引起的。在下面的代码片段中可以轻松地复现这种情况。

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>

您可以通过将顶部边距替换为顶部填充来解决此问题,或者您可以向其容器添加1像素的顶部填充。

0
(叹气)又是折叠边距问题。 h1 的边距被它所在的 section 共享,导致该部分在屏幕上的起始位置比预期要低。
解决方案:从 h1 中移除边距。
h1 {margin:0}

请查看fiddle


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接