【问题标题】:Bootstrap 4.0 - responsive header with image + navbar + full-height bodyBootstrap 4.0 - 带有图像 + 导航栏 + 全高主体的响应式标题
【发布时间】:2018-09-02 20:58:33
【问题描述】:

我正在使用 bootstrap 4 构建一个响应式页面,该页面有一个标题/标题部分,其中包含客户徽标的图像、他们的名称和导航栏。所有这些元素都是响应式的,即图像根据屏幕尺寸缩小,导航栏在小屏幕上最小化。

现在我想让身体也以一种响应的方式。即:用内容填充屏幕的剩余高度,必要时使用滚动条显示所有内容。
但由于某种原因,我无法弄清楚如何将页面的其余部分跨越整个高度。它要么在某些较大的屏幕上太大,但它非常适合移动设备,或者它非常适合大屏幕,但只有移动设备上应有的高度的一半。

页面大致是这样构建的:

<!DOCTYPE html>
<html lang="de" style="height:100%">

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  <title>Client Name</title>
</head>

<body class="main" style="height:100%">
  <div class="container">
    <div class="text-center">
    <!-- Client image goes here -->
    <h1 style="display:inline;margin-left:0.6em;color:#8b7d7b">Client Name</h1>
   </div>
  </div>
  <nav class="navbar navbar-expand-lg navbar-light" style="background-color:#e0eee0">
    <button class="navbar-toggler collapsed" type="button" data-toggle="collapse" data-target="#mainnavbar" aria-controls="mainnavbar" aria-expanded="false" aria-label="Toggle Navigation">
  <span class="navbar-toggler-icon"></span>
</button>
<div id="mainnavbar" class="navbar-collapse collapse justify-content-md-center">
  <ul class="navbar-nav">
    <li class="nav-item nav-link active">Page A</li>
  </ul>
</div>
</nav>
<div class="container" style="height:70vh">
<div class="row" style="height:100%">
  <!-- Main body of page -->
  <div class="col-sm-1 col-md-9" style="height:100%">
    <p class="col-scrol">
      <!-- Main part of the page. Content should have full height and scroll vertically if necessary -->
    </p>
  </div>
  <!-- Comment section (scrolling) -->
  <div class="d-none d-md-block col-md-3 comment-section">
        <!-- Comment section at the left hand side of the page -->      
  </div>
 </div>
 </div>
</body>

</html>

我创建了一个实际显示整个页面的 plunkr:
https://plnkr.co/edit/GQHoephQyx3hoejgkT4k?p=preview

在 bootstrap 4.0 中,根据需要显示页面的正确方法是什么,即使用图像根据屏幕尺寸缩小/增长时剩余的全部高度?

编辑澄清:
内容本身应该有一个滚动条,而不是整个页面。 IE。带有图像和菜单栏的标题应始终保持可见,而随后出现的内容应具有滚动条(如有必要)。

【问题讨论】:

  • 请回答:如果要使用Bootstrap 4,为什么代码引用Bootstrap 3?
  • 你是对的。对不起。在此处和 Plunkr 示例中修复了该问题。我下载了文件以在本地包含它们,因此我不得不将包含的内容替换为在线包含的内容,并且我使用了错误的 url。问题的实际问题仍然适用。

标签: html css twitter-bootstrap bootstrap-4


【解决方案1】:

不要设置定义的高度,而是使用 flexbox 使内容区域填充其父容器(在本例中为主体)的高度。

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

.flex-fill {
    flex: 1 1 auto;
}

.flex-shrink {
    flex-shrink: 0;
}

https://codeply.com/go/fKNRvG0Hak

注意:flex-fill 实用程序类将包含在下一个 Bootstrap 4.2 版本中:https://github.com/twbs/bootstrap/commit/2137d61eacbd962ea41e16a492da8b1d1597d3d9


类似问题:
Bootstrap 4 make nested row fill parent that has been made to fill viewport height using flex-grow
Bootstrap 4 Navbar and content fill height flexbox
Bootstrap 4 page wide header bar above navbar

【讨论】:

  • 对不起,我的问题似乎没有正确陈述。我将编辑问题。滚动条应该只用于内容,而不是标题或菜单。 IE。标题和菜单应位于页面顶部并始终可见。
  • 仍然可以按照说明使用 flexbox 完成。看看我发布的其他类似问题,以及这个答案:stackoverflow.com/questions/48703632/…
  • 太好了,明白了。非常感谢。
猜你喜欢
  • 2017-09-12
  • 2017-12-14
  • 2013-09-24
  • 2016-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多