如何删除背景图像和 flex 页脚之间的空白 space?

How to remove blank space between background image and flex footer?

我相信我的背景 img 在它和我的 flexbox 粘性页脚之间创建了一个空白 space。我尝试了很多方法,但到目前为止它们只会产生不同的问题......有人可以帮忙吗?我创建了一个与原始背景 img 大小相同的占位符图像来为您重现问题!谢谢!

JS Fiddle Link

CSS:

.masthead {
  height: 100vh;
  min-height: 100%;
  background:; /* ie 6 cheat */
  background-image: url('http://via.placeholder.com/3024x4032?Placeholder%27');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

/* Sticky Footer Classes */
body
 {
    display: flex;
    flex-direction: column;
}

html, body
 {
height: 100%;
}

#page-content {
    flex: 1 0 auto;
}

#sticky-footer {
  flex-shrink: 0;
}

这可以通过多种方式解决,

方法0:我觉得Footer标签前有一些不需要的文字内容。如果该文本内容是故意的,则转到方法 2,否则执行此操作

删除下面 <body class="d-flex flex-column">

下的代码片段
  <div id="page-content">
    <div class="container text-center">
      <div class="row justify-content-center">
        <div class="col-md-7">
          <h1 class="font-weight-light mt-4 text-white">Sticky Footer using Flexbox</h1>
          <p class="lead text-white-50">Use just two Bootstrap 4 utility classes and three custom CSS rules and you will have a flexbox enabled sticky footer for your website!</p>
        </div>
      </div>
    </div>
<div id="push"></div>
  </div>

方法一:将页脚移向内容结束的地方。

#sticky-footer {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
}

方法二:在样式.css文件中移动CSSbackground-image描述下定义的class .mashead 到 body

  background:; /* ie 6 cheat */
  background-image: url('http://via.placeholder.com/3024x4032?Placeholder');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;

(在 body 中使用,而不是在 .masthead 中使用)

此空白 space 是您的 page-content。

您需要将此添加到您的 header:

.masthead {
    position: absolute;
    width: 100%;
    margin-top: 56px; // your navbar height
}

同时添加此内容以显示您的页脚:

#sticky-footer {
    z-index: 1;
}

如果您想将内容垂直居中,请将此 类 添加到您的 page-content:

<div id="page-content">
<div class="container text-center h-100">
<div class="row justify-content-center align-items-center h-100">

查看代码段:

.masthead {
  height: 100vh;
  min-height: 100%;
  background: ;
  /* ie 6 cheat */
  background-image: url('http://via.placeholder.com/3024x4032?Placeholder');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  position: absolute;
  /* added */
  width: 100%;
  /* added */
}


/* Sticky Footer Classes */

body {
  display: flex;
  flex-direction: column;
}

html,
body {
  height: 100%;
}

#page-content {
  flex: 1 0 auto;
}

#sticky-footer {
  flex-shrink: 0;
  z-index: 1/* added */
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

<nav class="navbar navbar-expand-lg navbar-light bg-light shadow fixed-top">
  <div class="container">
    <a class="navbar-brand" href="#">website</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
     <span class="navbar-toggler-icon"></span>
   </button>
    <div class="collapse navbar-collapse" id="navbarResponsive">
      <ul class="navbar-nav ml-auto">
        <li class="nav-item active">
          <a class="nav-link" href="#home">Home
        <span class="sr-only">(current)</span>
      </a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#about">About</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#blogs">Blogs</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#events">Events</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#music">Music</a>
        </li>
      </ul>
    </div>
  </div>
</nav>

<!--—Full Page Image Header with Vertically Centered Content —-->
<header class="masthead">
  <div class="container h-100">
    <div class="row h-100 align-items-center">
      <div class="col-12 text-center">
        <h1 class="font-weight-light">website</h1>
        <p class="lead">2nd line</p>
      </div>
    </div>
  </div>
</header>

<div id="page-content">
  <div class="container text-center h-100">
    <div class="row justify-content-center align-items-center h-100">
      <div class="col-md-7">
        <h1 class="font-weight-light mt-4 text-white">Sticky Footer using Flexbox</h1>
        <p class="lead text-white-50">Use just two Bootstrap 4 utility classes and three custom CSS rules and you will have a flexbox enabled sticky footer for your website!</p>
      </div>
    </div>
  </div>
  <div id="push"></div>
</div>
<footer id="sticky-footer" class="py-4 bg-dark text-white-50">
  <div class="container text-center">
    <small>Copyright © website</small>
  </div>
</footer>