【问题标题】:How to remove blank space between background image and flex footer?如何删除背景图像和弹性页脚之间的空白?
【发布时间】:2020-04-17 22:13:33
【问题描述】:

我相信我的背景 img 在它和我的 flexbox 粘性页脚之间创建了一个空白区域。我尝试了很多方法,但到目前为止它们只会产生不同的问题......有人可以帮忙吗?我创建了一个占位符图像,它的大小与我的原始背景 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;
}

【问题讨论】:

    标签: html bootstrap-4 flexbox background-image sticky-footer


    【解决方案1】:

    此空白区域是您的页面内容。

    您需要将此添加到您的标题中:

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

    另外添加这个来显示你的页脚:

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

    如果您想垂直居中您的内容,请将这些类添加到您的页面内容中:

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

    查看sn-p:

    .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>

    【讨论】:

    • 它并没有解决问题,但我感谢您的帮助和建议在顶部设置额外的边距!我正在使用“mt-5”,因为我使用的是引导程序。这是我遇到的另一个问题,后来推迟了。谢谢!
    【解决方案2】:

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

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

    去掉&lt;body class="d-flex flex-column"&gt;下面的代码sn-p这个

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

    方法2:在style.css文件中将.masthead类下定义的CSS background-image描述移动到body中

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

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

    【讨论】:

    • 哦,是的-删除多余的代码(您的方法 0)解决了问题!我没有意识到我有额外的代码——我只是假设它是粘性页脚工作所必需的代码。后见之明是 2020 年——现在对我来说很明显。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 2013-04-26
    • 1970-01-01
    • 2019-10-22
    • 2019-06-05
    • 2020-06-10
    相关资源
    最近更新 更多