【问题标题】:Vertical centring but also keeping top and bottom sticky when a window gets shrunk [duplicate]垂直居中但在窗口缩小时保持顶部和底部粘性[重复]
【发布时间】:2018-02-26 02:31:30
【问题描述】:

如何才能完成下面的这个css问题?

这是指屏幕有足够的空间来保持元素可见。

但是一旦窗口缩小并且没有足够的空间使它们以原始大小保持可见,顶部和底部元素会粘在原始大小的垂直边上,并且只有中间元素会缩小。

我陷入了这种状态。乍一看几乎接近答案,但实际上并非如此......

http://embed.plnkr.co/AJXYCGEVzMPt1My0xehA/

堆栈sn-p

/* Styles go here */

.wrapper {
  height: 100vh;
}

.top {
  width: 40%;
  height: 50px;
  background-color: green;
}

.middle {
  width: 90%;
  height: 300px;
  background-color: blue;
  margin-top: 30px;
  margin-bottom: 30px;
}

.bottom {
  width: 20%;
  height: 50px;
  background-color: red;
}
<link data-require="bootstrap@4.0.5" data-semver="4.0.5" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" />
    
    <div class="wrapper d-flex flex-column align-items-center justify-content-center">
      <div class="top col-auto">Top Element</div>
      <div class="middle col-auto">Middle Element</div>
      <div class="bottom col-auto">Bottom Element</div>
    </div>

【问题讨论】:

    标签: html twitter-bootstrap css flexbox


    【解决方案1】:

    如果我正确理解您想要做什么,那么 flex 就是一种方式。

    只需设置一个具有最大高度的 flex-container(具有实际的 height 属性,不要使用 max-height 否则它的弹性项目将无法增长,而中间的项目将不会增长完全显示),然后将底部和顶部元素设置为flex:0 0 100px,这样它们就不会增长/缩小,并允许中间元素使用flex: 1 1 auto;进行增长和缩小

    并且将 body 本身也设置为 flex-container,因此 flex-container 也是一个可以通过 justify-content 轻松居中的 flex-item

    实际工作版本: https://embed.plnkr.co/pajfCNCacQX6kuBf6I8z/

    这是一个 SO sn-p... 无法调整大小,因此无法用于测试,只是为了保证代码安全:

    body{
      height:100vh;
      margin:0;
      display:flex;
      flex-direction:column;
      justify-content:center;
    }
    
    .container{
      display:flex;
      flex-direction:column;
      height: 500px;
    }
    
    .top{
      background:CornflowerBlue;
      flex: 0 0 50px;
    }
    
    .center{
      flex:1 1 auto;
      margin:10px 0;
      background:salmon;
    }
    
    .bottom{
      flex: 0 0 50px;
      background:MediumSeaGreen ;
    }
    <body>
        <div class="container"> 
          <div class="top">
          </div>
          <div class="center">
          </div>
          <div class="bottom">
          </div>
        </div>
      </body>

    【讨论】:

    • 感谢您的回复!我觉得@Bhuwan 的解决方案要简单得多,但是当我将overflow: scroll 添加到中间元素时,当所有子元素都没有高度时,这也有效。无论如何,感谢您的帮助:)
    • 由于您没有使用 Bootstrap,您的答案错过了它如何影响使用的内置类的布局。 OP的解决方案不起作用的主要原因是wrapper上的justify-content-center,我在回答中对此进行了解释。此外,您的flex: 1 1 auto 将始终使center 填充container 的高度,正如您在发布的图片中看到的那样,它不应该。
    • @LGSon wut??在图像中,它看起来需要填充容器的高度,并且容器应该长到某个点,之后它应该保持居中。
    • @RyoIkarashi 两种解决方案都以完全相同的方式做同样的事情(尽管语法发生了细微的变化),唯一的区别是我有一个额外的容器,该容器具有“最大”高度并在身体上保持弯曲容器居中,因为您的第一张图片在顶部和底部项目的上方和下方都有灰色空间。
    【解决方案2】:

    您需要在顶部和中间 div 中使用 flex-shrink:0,以便在调整视口大小时它们不会缩小

    Fiddle Link ▸

    堆栈片段

    body {
      margin: 0;
    }
    
    .parent {
      display: flex;
      flex-direction: column;
      height: 100vh;
      justify-content: center;
    }
    
    .top,
    .bottom {
      margin: 0 30px;
      flex-shrink: 0;
      background: red;
    }
    
    .middle {
      background: blue;
      overflow: auto;
    }
    <div class="parent">
      <div class="item top">Lorem ipsum dolor sit amet, consectetur adipiscing elit</div>
      <div class="item middle">. Quisque porta massa vitae lacus cursus, mattis consectetur ex tristique. Nunc in cursus sem. . Quisque porta massa vitae lacus cursus, mattis consectetur ex tristique. Nunc in cursus sem. . Quisque porta massa vitae lacus cursus, mattis consectetur
        ex tristique. Nunc in cursus sem. . Quisque porta massa vitae lacus cursus, mattis consectetur ex tristique. Nunc in cursus sem.</div>
      <div class="item bottom">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque porta massa vitae lacus cursus, mattis consectetur ex tristique. Nunc in cursus sem.</div>
    </div>

    【讨论】:

    • 感谢您的快速回复!尽管您的代码运行良好,但它仅在每个子元素都有其高度时才有效。当每个孩子都没有明确指定高度时,上面的代码不起作用。这里的例子。 fiddle.jshell.net/bhuwanb9/g79gjL2j/1你知道有什么解决办法吗?
    • 我通过编辑您的代码找到了一个解决方案。我在中间元素中添加了overflow: scroll,现在它就像一个魅力!非常感谢您的帮助 :) fiddle.jshell.net/g79gjL2j/10
    • @RyoIkarashi 对不起,我刚刚看到你的两个评论...我添加了视觉的高度..很明显div 的高度取决于里面的内容....@987654329 @ 将帮助...编辑我的答案....
    • @RyoIkarashi 和 Bhuwan:这里的问题是 OP 在顶部/底部元素上已经有 flex-shrink: 0,通过它的 col-auto,虽然因为你没有使用 Bootstrap,所以你的答案错过了那部分. OP的解决方案不起作用的主要原因是wrapper上的justify-content-center,我在回答中对此进行了解释。
    • @LGSon 实际上在开始 OP 时并没有发布他的 plucker 代码链接......所以不知道 OP 使用 bootstrap4 ......我刚刚看到 OP 也使用了@987654334 @标签。:)...
    猜你喜欢
    • 2014-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多