Bootstrap 4 - col垂直对齐未按预期工作[重复]

Posted

技术标签:

【中文标题】Bootstrap 4 - col垂直对齐未按预期工作[重复]【英文标题】:Bootstrap 4 - col vertical alignment not working as expected [duplicate] 【发布时间】:2018-02-15 21:49:03 【问题描述】:

阅读 Bootstrap 文档,您可以访问 alignment examples section.

它告诉你可以使用 flexbox 对齐工具来verticallyhorizo​​ntally将列对齐为 Bootstrap CSS 类 align-items-startalign-items-centeralign-items-end 但是当我'正在尝试,内容未按预期垂直对齐,全高container,请参见以下代码:

html,
body 
  height: 100%;


.fill 
  min-height: 100%;
  height: auto !important;
  height: 100%;
  background-color: lightblue;

.col 
  border: 1px solid red;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">

<div class="container fill">
  <div class="row align-items-start">
    <div class="col text-center">
      align-items-start
    </div>
  </div>
  <div class="row align-items-center">
    <div class="col text-center">
      align-items-center
    </div>
  </div>
  <div class="row align-items-end">
    <div class="col text-center">
      align-items-end
    </div>
  </div>
</div>

<!-- jQuery first, then Tether, then Bootstrap JS. -->
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>

我希望在垂直中间有align-items-center 内容,在container 底部有align-items-end

我做错了什么或我误解了什么?

【问题讨论】:

【参考方案1】:

为了垂直对齐flex-container 中的项目,您需要将height 赋予flex-container

.row
  min-height:100px;

html,
body 
  height: 100%;


.fill 
  min-height: 100%;
  height: auto !important;
  height: 100%;
  background-color: lightblue;

.col 
  border: 1px solid red;


.row
  min-height:100px;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">

<div class="container fill">
  <div class="row align-items-start">
    <div class="col text-center">
      align-items-start
    </div>
  </div>
  <div class="row align-items-center">
    <div class="col text-center">
      align-items-center
    </div>
  </div>
  <div class="row align-items-end">
    <div class="col text-center">
      align-items-end
    </div>
  </div>
</div>

<!-- jQuery first, then Tether, then Bootstrap JS. -->
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>

【讨论】:

以上是关于Bootstrap 4 - col垂直对齐未按预期工作[重复]的主要内容,如果未能解决你的问题,请参考以下文章

bootstrap 3 到 bootstrap 4 cols 不再水平对齐 [重复]

Bootstrap 4垂直对齐列[重复]

如何将 Bootstrap 4 中的 SVG 垂直对齐到行高?

引导网格未按预期工作[重复]

bootstrap 3中的垂直对齐字形图标

如何在 Bootstrap 3 列中垂直对齐 div