html - 当我使用 "height:100vh"时,主体上方有空白

标签 html css viewport

我有一个基本的 HTML 文档设置,我希望 div 具有视口(viewport)的高度。但是,一旦我将 height:100vh 添加到 .main div 中,主体上方就会出现一个间隙,并且会出现一个垂直滚动条

这是我的代码。

HTML:

<div class="main">
    <div class="container">

        <div class="nav">
            <ul>
                <li><a href="index.html" class="active">Work</a></li>
                <li><a href="#">About</a></li>
                <li><a href="#">Contact</a></li>
            </ul>
        </div>

        <div class="main_text">
            <h1>Hey there! I'm <span class="yellow">Gautham SK</span>, a digital designer &amp; web developer.</h1>
        </div>

    </div> <!-- end container -->
</div> <!-- end main -->

CSS:

html, body {
    height: 100%;
    padding: 0;
    margin: 0;
    font-family: 'Montserrat', sans-serif;
    font-weight: 400;
}



.container {
    max-width: 1024px;
    padding: 0 20px;
    margin: 0 auto;
}

/* NAVIGATION */

.nav ul {
    display: flex;
    align-items: center;
    justify-content: flex-end;
/*  margin-top: 75px;*/
    list-style-type: none;
}

.nav ul li {
    margin-left: 40px;
    list-style: none;
    font-size: 16px;
}

.nav ul li a, .nav ul li a:visited {
    color: #000;
    padding: 5px;
    text-decoration: none;
    transition: 0.2s color;
}

.nav ul li a:hover {
    color: #ffde00;
    transition: 0.2s color;
}

.nav ul li a.active {
    color: #000;
    border-bottom: 5px solid #ffde00;
}

/* MAIN SECTION */

.main {
    background: url("../images/bg.jpg") center center no-repeat;    
    background-size: cover;
}

.main, .main .container {
    height: 100vh;
}

.main_text {
    height: 100%;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
}

.main_text h1 {
    font-weight: 400;
    font-size: 45px;
}

span.yellow {
    color: #ffde00;
}

我还有一个链接到此文档的 normalize.css。我尝试删除所有元素的填充和边距,但这不起作用。

更新

耶!找到了解决方案。这是我所做的:

  • 删除了导航及其子项的边距,并将其替换为顶部填充,以获得所需的外观。
  • main_text 的高度更改为 80%。

如果您有更好的解决方案,请告诉我!

最佳答案

作为一个简单的测试,在 body 上设置 padding: 1px。如果差距消失,请阅读:http://www.sitepoint.com/web-foundations/collapsing-margins/

关于html - 当我使用 "height:100vh"时,主体上方有空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34114589/

相关文章:

android 键盘显示后 jQuery 移动页脚或视口(viewport)大小错误

javascript - 检测按钮点击 z-index -1

html - 无法让滚动条显示在溢出文本上

javascript - 检测视口(viewport)方向,如果方向是纵向显示警告消息,建议用户使用说明

javascript - 在 javascript 函数中使用 jquery animate

css - 在 DIV : how to place them? 中创建 "columns"

ios - 媒体查询和视口(viewport)宽度 - 良好的 CSS 修复在 iPad 上不起作用

java - aui validator 无法处理在 liferay 中提交的表单

html - 背景颜色覆盖上一行

javascript - Simple Polymer 1.0数据表