【问题标题】:Issue in fixing the content in the center both horizontally and vertically水平和垂直固定中心内容的问题
【发布时间】:2019-01-23 09:18:18
【问题描述】:

我在将我的内容垂直和水平固定在中心时遇到问题。当我在开发人员工具中检查时,我的容器流体不会覆盖页脚。我查看了其他堆栈,但仍然无法弄清楚。这是我的代码笔。 https://codepen.io/anon/pen/yqdbWd

我想要的是删除侧边栏,因为它是一个内容很少的页面。我已经包含了我的问题的屏幕截图。 而且我的容器流体并没有占据整个页面。 最后,我想知道有没有办法像这样缩短我的hr线的长度。

这是我到目前为止所做的代码。

HTML

<body>
<div class="container-fluid h-100">
        <nav class="navbar navbar-light rounded">
    <a class="navbar-brand" href="#">
      <svg class="d-block" width="36" height="36" viewbox="0 0 612 612" xmlns="http://www.w3.org/2000/svg" focusable="false"><title>Bootstrap</title><path fill="currentColor" d="M510 8a94.3 94.3 0 0 1 94 94v408a94.3 94.3 0 0 1-94 94H102a94.3 94.3 0 0 1-94-94V102a94.3 94.3 0 0 1 94-94h408m0-8H102C45.9 0 0 45.9 0 102v408c0 56.1 45.9 102 102 102h408c56.1 0 102-45.9 102-102V102C612 45.9 566.1 0 510 0z"/><path fill="currentColor" d="M196.77 471.5V154.43h124.15c54.27 0 91 31.64 91 79.1 0 33-24.17 63.72-54.71 69.21v1.76c43.07 5.49 70.75 35.82 70.75 78 0 55.81-40 89-107.45 89zm39.55-180.4h63.28c46.8 0 72.29-18.68 72.29-53 0-31.42-21.53-48.78-60-48.78h-75.57zm78.22 145.46c47.68 0 72.73-19.34 72.73-56s-25.93-55.37-76.46-55.37h-74.49v111.4z"/></svg>
    </a>
    <ul class="nav justify-content-end">
      <li class="nav-item">
        <a class="nav-link active" href="#">Updates</a>
      </li>
    </ul>
  </nav>
  <div class="row align-items-center h-100">
      <div class="col-6 mx-auto">
          <div class="jumbotron text-center">
              <hr class="text-border" />
              <p class="lead">Perth says tuned on it.</p>
              <p class="lead">Stay Alert For Updates</p>
              <hr class="text-border" />
          </div>
      </div>
  </div>

   <footer class="mt-auto">
    <p class="float-left">2018 © Palungo</p>
      <div class="float-right">
        <ul class="list-inline">
          <li class="list-inline-item"><a href="#">Terms</a></li>
          <li class="list-inline-item"><a href="#">Privacy</a></li>
        </ul>
      </div>
  </footer>

</div>

这里是 CSS

   body,html {
      height: 100%;
      background:#333A4D;
   }
   .navbar{
      margin: 10px;
      background: #333A4D;
      padding: 20px;
    }
   .nav-link{
      font-size: 25px;
      line-height: 32px;
      color: #F3B0B6;
   }
  .list-inline-item{ margin-left: 1rem; }
  .jumbotron{ background: none; }
  .text-border{
     display: block;
     height: 1px;
     border: 0;
     border-top: 4px solid #F3B0B6;
    }
   .lead{
     text-transform: uppercase;
     color: #F3B0B6;
     font-size: 25px;
     line-height: 31px;
    }
    @media (min-width: 48em) {
     .navbar-brand{
       float: left;
     }
   .nav{
     float: right;
    }
  }

【问题讨论】:

  • 请缩小到一个问题。有一个滚动条,因为你有一个 100% 高度的行,它外面有一个导航和页脚。该行占据了视口的 100% 高度,因此也需要为导航和页脚留出空间。
  • 但我在容器流体中确实有页脚。那么,当我将它悬停在检查工具中时,为什么容器流体没有覆盖页脚。
  • 因为 jumbotron 行也是 100% 的视口高度,所以它滚动到低于视口的位置

标签: html css bootstrap-4


【解决方案1】:

不要在 jumbotron .row 上使用 height:100% ,而是将其设置为 container-fluid flexbox 列 (d-flex flex-column),然后在 jumbotron .row 上使用 flex-grow-1 以便填充导航之间的剩余高度和页脚。这将消除滚动条。

   <div class="container-fluid h-100 d-flex flex-column">
      <nav class="navbar navbar-light fixed-top">
        ..
      </nav>
      <div class="row align-items-center flex-grow-1">
          <div class="col-6 mx-auto">
              <div class="jumbotron text-center mb-0">
                  <hr class="text-border" />
                  <p class="lead">Perth says tuned on it.</p>
                  <p class="lead">Stay Alert For Updates</p>
                  <hr class="text-border" />
              </div>
          </div>
      </div>
      <footer class="mt-auto">
        ..
      </footer>
    </div>

https://codepen.io/anon/pen/wxLqMK

至于让 jumbotron 在页面上居中,使导航栏 fixed-top 不会向下推 jumbotron 内容并使其偏离中心。还要删除 jumbotron (mb-0) 上的底部边距。这是generic Bootstrap 4 example


相关:Bootstrap 4 Navbar and content fill height flexbox

弹性填充高度导航行页脚jumbotron

【讨论】:

  • 感谢您的帮助!
【解决方案2】:

您将height: 100%; 添加到您的主要部分,这将使其与屏幕一样大,但标题将您的主要部分向下推,这就是出现滚动条的原因。

然后我在您的页脚中添加了position: absolute; 以使其粘在底部,并添加了display: flex; 并使用一些技巧来替换float: left &amp; float: right

还可以缩短我用&lt;div class="col-6 mx-auto"&gt; 替换&lt;div class="col-4 mx-auto"&gt; 的行,如果您想进一步缩短它,请使用&lt;div class="col-3 mx-auto"&gt; 甚至更低的值。

body,
html {
  height: 100%;
  background: #333A4D;
}

.navbar {
  margin: 10px;
  background: #333A4D;
  padding: 20px;
  margin-top: 0px;
}

.nav-link {
  font-size: 25px;
  line-height: 32px;
  color: #F3B0B6;
}

.nav-item {
  margin-left: 1rem;
}

.list-inline-item {
  margin-left: 1rem;
}

.jumbotron {
  background: none;
}

.text-border {
  display: block;
  height: 1px;
  border: 0;
  border-top: 4px solid #F3B0B6;
}

.lead {
  text-transform: uppercase;
  color: #F3B0B6;
  font-size: 25px;
  line-height: 31px;
}

@media (min-width: 48em) {
  .navbar-brand {
    float: left;
  }
  .nav {
    float: right;
  }
}

.footer {
  width: 100%;
  position: absolute;
  bottom: 0;
  left: 0;
  padding: 0 40px;
  display: flex;
  justify-content: space-between;
}

.centered-content {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>

<body>
  <div class="container-fluid h-100">
    <nav class="navbar navbar-light rounded">
      <a class="navbar-brand" href="#">
        <svg class="d-block" width="36" height="36" viewbox="0 0 612 612" xmlns="http://www.w3.org/2000/svg" focusable="false"><title>Bootstrap</title><path fill="currentColor" d="M510 8a94.3 94.3 0 0 1 94 94v408a94.3 94.3 0 0 1-94 94H102a94.3 94.3 0 0 1-94-94V102a94.3 94.3 0 0 1 94-94h408m0-8H102C45.9 0 0 45.9 0 102v408c0 56.1 45.9 102 102 102h408c56.1 0 102-45.9 102-102V102C612 45.9 566.1 0 510 0z"/><path fill="currentColor" d="M196.77 471.5V154.43h124.15c54.27 0 91 31.64 91 79.1 0 33-24.17 63.72-54.71 69.21v1.76c43.07 5.49 70.75 35.82 70.75 78 0 55.81-40 89-107.45 89zm39.55-180.4h63.28c46.8 0 72.29-18.68 72.29-53 0-31.42-21.53-48.78-60-48.78h-75.57zm78.22 145.46c47.68 0 72.73-19.34 72.73-56s-25.93-55.37-76.46-55.37h-74.49v111.4z"/></svg>
      </a>
      <ul class="nav justify-content-end">
        <li class="nav-item">
          <a class="nav-link active" href="#">Updates</a>
        </li>
      </ul>
    </nav>
    <div class="row align-items-center centered-content">
      <div class="col-4 mx-auto">
        <div class="jumbotron text-center">
          <hr class="text-border" />
          <p class="lead">Perth says tuned on it.</p>
          <p class="lead">Stay Alert For Updates</p>
          <hr class="text-border" />
        </div>
      </div>
    </div>

    <footer class="footer mt-auto">
      <p>2018 © Palungo</p>
      <div>
        <ul class="list-inline">
          <li class="list-inline-item"><a href="#">Terms</a></li>
          <li class="list-inline-item"><a href="#">Privacy</a></li>
        </ul>
      </div>
    </footer>

  </div>
</body>

【讨论】:

  • 谢谢,但内容仍然不在页面中心。我想要它正好在中心。你能指导我把它垂直放在中心吗?这是它在我的浏览器中的样子:prntscr.com/kjbycq
  • 非常感谢您的帮助!
猜你喜欢
  • 2013-09-02
  • 2012-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-01
  • 2019-09-26
  • 2012-09-23
相关资源
最近更新 更多