【问题标题】:Vertical align button in the middle of the column- bootstrapcolumn-bootstrap 中间的垂直对齐按钮
【发布时间】:2016-02-06 05:08:39
【问题描述】:

我正在使用引导程序并尝试垂直对齐列中的按钮。

注意:我不能使用 flex,也不能按照标准定义高度。

如果我使用边距,它将无法在平板电脑和移动设备中使用。

下面是我的代码:

.col-xs-2, .col-xs-6 {
   display: inline-block;
}
.col-xs-2 {
    vertical-align: middle;
     
}
<div class="container">
    <div class="row">
    <div class="col-xs-4">
             <h4>Some really large label that will wrap to multiple lines in small screens</h4>

        </div>
    <div class="col-xs-6">
             <h4>Some really large label that will wrap to multiple lines in small screens</h4>

        </div>
        <div class="col-xs-2">
            <button class="btn" style="color: #660066;"> <i class="fa fa-arrow-left" data-ng-click="onClickBack()"></i>

            </button>
        </div><!-- 
     -->
    </div>
</div>

JS Fiddle Link:

所以我的问题是如何使 箭头按钮 对齐到行的中间。
我试过了:display:table-celldisplay: inline-block; 似乎没有任何效果。我错过了什么吗?

【问题讨论】:

  • 恐怕有点难以理解你想要什么。你能上传一张你想要的图片吗?
  • 行中间的箭头按钮按钮。
  • 哪一行与第二个文本元素在同一行,在下一行?另外,如果您指的是中间的“左右”,那就是“水平”,而不是“垂直”。我想也许你是在水平居中之后。
  • 我只寻找垂直对齐方式。
  • @user2936008 你不能把箭头 div 移到 col-xs-6 div 之前,那样它会占据行的中间位置吗?

标签: html css twitter-bootstrap


【解决方案1】:

借用这个问题的不同案例以及这个链接here上提到的源代码。


检查我的工作小提琴是否支持不同媒体:here


HTML:

<div class="container">
  <div class="row">
    <div class="row-height">
      <div class="col-xs-4 col-height">
        <div class="inside">
          <div class="content">
            <h4>Some really large label that will wrap to multiple lines in small screens</h4>
          </div>
        </div>
      </div>
      <div class="col-xs-6 col-height col-middle">
        <div class="inside">
          <div class="content">
            <h4>Some really large label that will wrap to multiple lines in small screens</h4>
          </div>
        </div>
      </div>
      <div class="col-xs-2 col-height col-middle">
        <div class="inside">
          <div class="content">
            <button class="btn" style="color: #660066;"> <i class="fa fa-arrow-left" data-ng-click="onClickBack()"></i>
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

CSS:

  .inside {
  background: #ffffff;
}
.content {
  padding: 12px 3px;
}
.row-height {
  display: table;
  table-layout: fixed;
  height: 100%;
  width: 100%;
}
.col-height {
  display: table-cell;
  float: none;
  height: 100%;
}
.col-top {
  vertical-align: top;
}
.col-middle {
  vertical-align: middle;
}
.col-bottom {
  vertical-align: bottom;
}
@media (min-width: 480px) {
  .row-xs-height {
    display: table;
    table-layout: fixed;
    height: 100%;
    width: 100%;
  }
  .col-xs-height {
    display: table-cell;
    float: none;
    height: 100%;
  }
  .col-xs-top {
    vertical-align: top;
  }
  .col-xs-middle {
    vertical-align: middle;
  }
  .col-xs-bottom {
    vertical-align: bottom;
  }
}
@media (min-width: 768px) {
  .row-sm-height {
    display: table;
    table-layout: fixed;
    height: 100%;
    width: 100%;
  }
  .col-sm-height {
    display: table-cell;
    float: none;
    height: 100%;
  }
  .col-sm-top {
    vertical-align: top;
  }
  .col-sm-middle {
    vertical-align: middle;
  }
  .col-sm-bottom {
    vertical-align: bottom;
  }
}
@media (min-width: 992px) {
  .row-md-height {
    display: table;
    table-layout: fixed;
    height: 100%;
    width: 100%;
  }
  .col-md-height {
    display: table-cell;
    float: none;
    height: 100%;
  }
  .col-md-top {
    vertical-align: top;
  }
  .col-md-middle {
    vertical-align: middle;
  }
  .col-md-bottom {
    vertical-align: bottom;
  }
}
@media (min-width: 1200px) {
  .row-lg-height {
    display: table;
    table-layout: fixed;
    height: 100%;
    width: 100%;
  }
  .col-lg-height {
    display: table-cell;
    float: none;
    height: 100%;
  }
  .col-lg-top {
    vertical-align: top;
  }
  .col-lg-middle {
    vertical-align: middle;
  }
  .col-lg-bottom {
    vertical-align: bottom;
  }
}
body {
  padding-bottom: 40px;
}
h1 {
  margin: 40px 0px 20px 0px;
  color: #95c500;
  font-size: 28px;
  line-height: 34px;
  text-align: center;
}
[class*="col-"] {
  border: none;
  background: #ffffff;
}
html,
body {
  width: 100%;
  height: 100%;
}
html {
  display: table;
}
body {
  display: table-cell;
  vertical-align: top;
}

【讨论】:

  • 它是否符合您的要求,您的问题得到解答了吗?
  • 是的,它有效。请检查我的答案,刚刚想通了。
  • @user2936008:您之前提到您不想使用边距?但是您的答案也有边距属性?
  • 是的。我不想使用顶部和底部边距。这会使它成为静态并且无法在不同的视图中工作。
【解决方案2】:

working JS Fiddle:

CSS 变化:

.col-xs-2, .col-xs-6 {
   display: inline-block;
}
.vcenter {
     float:none;
    display:inline-block;
    vertical-align:middle;
    margin-right:-4px;
}

【讨论】: