【问题标题】:white space appearing below divdiv下方出现空白
【发布时间】:2016-12-10 06:10:36
【问题描述】:

EDIT new jsfiddle here 成功复制了问题

我的 div 下方出现了这个空白。

此处显示为 2 张照片,其中一张向下滚动:

我想去掉空格。包含导航栏时,网页应该占据 100% 的视口,因此当视口发生变化时它应该会发生变化,而且确实如此,而且大小合适,但是页面下方有一些随机空白可以向下滚动到。空白看起来是导航栏的确切大小。我如何摆脱那个空白?请尝试the jsfiddle

代码:

<nav class="navbar navbar-dark navbar-fixed-top">
    <div class="container-fluid">
        <button     class="navbar-toggler hidden-md-up pull-xs-right" 
                    type="button" 
                    data-toggle="collapse" 
                    data-target="#nav-content">
                    &#9776;
        </button>
        <a class="navbar-brand" href="#">THE VEGAN REPOSITORY</a>
        <div class="collapse navbar-toggleable-sm" id="nav-content">
            <ul class="nav navbar-nav pull-xs-right">
                <li class="nav-item">
                    <a class="nav-link" href="#">FIND</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">ADD</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">LOGIN</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">SIGN UP FREE</a>
                </li>
            </ul>
        </div>
    </div>
</nav>

<div id="landing-page" class="text-uppercase">
    <div class="container-fluid" style="height: 100%;">
        <div class="row hidden-lg-up" style="height: 20%;">
            <div class="col-xs-3 flex-xs-middle">
                <img width="100" src="images/monster2.png" />
            </div>
            <div class="col-xs-3 offset-xs-6 flex-xs-middle">
                <img class="pull-xs-right" width="100" src="images/monster4.png" />
            </div>
        </div>
        <div class="row" id="middle-row">
            <div class="col-xs-1 col-sm-2 col-md-3 hidden-md-down flex-xs-top 
                flex-sm-middle">
                <img width="80%" src="images/monster2.png" />
            </div>
            <div class="col-md-12 col-lg-6 flex-xs-middle ">
                <div style="text-align: center;">
                    <h5 class="display-6">the vegan repository</h5>
                    <h1 class="display-3">
                        find vegan stuff* near you.
                    </h1>
                                        <a id="try-now-button" class="with-border clickable" href="#search-filter-page">
                        <h5 class="text-center medium-text">try now</h5>
                    </a>
                </div>
            </div>
            <div class="col-xs-1 col-sm-2 col-md-3 hidden-md-down 
                flex-xs-top flex-sm-middle">
                <img class="pull-xs-right" width="80%" src="images/monster4.png" />
            </div>
        </div>
        <div class="row" style="height:5%;">
            <h4 class="display-6 flex-xs-bottom">
                *Stuff like restaurants, meat alternatives, 
                dairy alternatives, and much more!
            </h4>
        </div>
    </div>
</div>

css:

#landing-page {
  background-color: dimgray;
  height: calc(100% - 50px);
  margin-top: 50px;
  overflow-y: auto;
  padding: 10px 40px 10px 40px;
  min-height: 396px; }

h1 {
  font-size: 10vmin;
  color: #FFF; }

h5 {
  color: rgba(255, 255, 255, 0.7); }

h4 {
  font-size: 1rem;
  color: rgba(255, 255, 255, 0.7); }

/* MORE THAN 75 (XL) */
#middle-row {
  height: 95%; }

/* LESS THAN 75 (LG) */
@media (max-width: 74.9em) {
  #middle-row {
    height: 95%; } }
/* LESS THAN 62 (MD) */
@media (max-width: 61.9em) {
  #middle-row {
    height: 75%; } }
/* LESS THAN 48 (SM) */
@media (max-width: 47.9em) {
  #middle-row {
    height: 75%; } }
/* LESS THAN 34 (XS) */
@media (max-width: 33.9em) {
  #middle-row {
    height: 75%; } }

.navbar-toggler {
  color: rgba(255, 255, 255, 0.7); }

.navbar-dark .navbar-nav .nav-link {
  color: rgba(255, 255, 255, 0.7); }

.navbar-dark .navbar-nav .nav-link:focus,
.navbar-dark .navbar-nav .nav-link:hover {
  color: #FFF; }

nav {
  background-color: #fc4747; }


  html, body {
  height: 100%; }

EDIT new jsfiddle here 成功复制了问题

【问题讨论】:

  • 我看不到任何空白。
  • jsfiddle中看不到空格,问题可能出在html/body标签上。
  • @Leothelion 我的问题照片上传了吗?我的互联网连接不稳定
  • no..我们无法看到任何照片,因为它们来自本地而非互联网链接。
  • 你是在使用 MAC 并在 chrome 浏览器中检查吗?

标签: html css twitter-bootstrap-4


【解决方案1】:

在jsfiddle中我们看不到空格,所以问题可能来自html/body标签。 如果无论你调整多少它们总是在 1155 像素,请尝试将 html 和正文高度设置为 100%:

html, body { height: 100%; }

【讨论】:

    【解决方案2】:

    CSS 中,如果您想使用百分比指定height 属性,您必须 指定父容器的height。所以,在你的情况下,你的 #landing-page 元素有它的父 &lt;body&gt; 标签,而 &lt;body&gt; 标签有它的父 &lt;html&gt; 标签。这就是为什么您必须声明:

    html, body { height: 100%; }
    

    在你的 CSS 中。

    这里是另一个问题:

    <div class="row hidden-lg-up" style="height:20%;">
        ...
    </div>
    <div class="row" style="height:95%;">
        ...
    </div>
    <div class="row" style="height:5%;">
        ...
    </div>
    

    尝试对所有高度求和 =) 改变它,这样它们总和会达到 100%,你会得到你想要的


    更新

    我已设法在本地重现您的问题,因此这是解决方案。
    实际上,您的问题不是底部的白线,而底部的主要罪魁祸首是margin-top 元素的margin-top 属性=)。
    看,如果您删除 &lt;nav&gt; 元素,您将在顶部看到相同的白线。您似乎为 #landing-page 设置了 100% 的高度,然后将其移至底部。浏览器在 visible 空间的 100% 处绘制背景,但是,正如您所注意到的,您有一些垂直滚动,并且该滚动下方的所有内容都没有 background-color.

    一般来说,marginbackground-colorbackground-image 相比是棘手的,因为它可能会导致当前(或类似)问题。将margin-top 值移动到padding-top 值以具有相同的间距,而不是从height 属性中删除calc(),如下所示:

    #landing-page {
      background-color: dimgray;
      height: 100%; /* just 100% */
      overflow-y: auto;
      padding: 60px 40px 10px 40px; /* added margin-top to padding-top */
      min-height: 396px; }
    

    【讨论】:

    • 感谢您将高度增加到 100% 是正确的。但是我的 bodyhtml 元素的高度已经是 100%,抱歉。我忘了将它添加到问题中,因为我使用的是 angular 2,并且 htmlbody 的 css 被抽象到我忘记的文件中。我会将该信息添加到问题中。底部的空白仍然存在。我在想我的环境中的某些东西导致了它。我在一个偏远的岛上度假,使用手机作为 wifi 热点,遇到了一些奇怪的问题。
    • 你用什么浏览器?在chrome中,当htmlbody为100%高度时,底部没有空白。
    • 我可以通过网络访问您的网站吗?是在网上吗?
    • @Beniamino_Baggins,你在吗?
    • 我刚重新上线。我设法在问题中发布的新 jsfiddle 中复制了该问题。
    【解决方案3】:

    body 上设置背景颜色而不是div,将解决您的问题。

    【讨论】:

    • 这会使空白区域变灰。但我希望空白完全消失。这样在包含导航栏时不需要滚动,页面高度为 100%。
    • 我明白了,但是你为什么要使用 height: calc(100% - 50px);而不是 100%?
    • 还尝试更改为填充而不是边距并将 box-sizing:border-box 应用于#landing-page
    • 使用高度计算,因为它位于导航栏下方 50px 处,并且需要在视口底部结束,而不是在其下方 50px 处。谢谢,我会尽快尝试。
    • 没有导航栏是固定的,所以它不会增加高度。因此,您需要将高度设置为 100%
    猜你喜欢
    • 1970-01-01
    • 2015-03-07
    • 2018-06-02
    • 2020-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多