【问题标题】:Height: 100%; not working with Flex Box高度:100%;不适用于 Flex Box
【发布时间】:2018-08-16 21:00:46
【问题描述】:

问题:

  • 制作一条分隔两个对象的垂直线,但它不会出现,因为它没有高度,尽管我添加了height: 100%
  • 为什么它没有填满我的顶部到底部的空间 分区?是因为.card-bodyheight: auto吗?


案例:

  • 我已经尝试过添加宽度,禁用 flex-box,但没有任何效果,但如果我在 .card-body 中添加特定高度,它就可以工作。


你呢 知道一个解决方案如何在不增加特定高度的情况下工作?

.card {
  margin-bottom: 30px;
}

.card > .card-header {
  font-weight: 500;
  text-transform: uppercase;
  font-size: 15px;
  margin-bottom: 6px;
}

.card > .card-header.light {
  color: #fff;
}

.card > .card-body {
  background-color: #fff;
  border-radius: 12px;
  padding: 24px;
  -webkit-box-shadow: -2px 4px 34px 1px rgba(0, 0, 0, 0.15);
  -moz-box-shadow: -2px 4px 34px 1px rgba(0, 0, 0, 0.15);
  box-shadow: -2px 4px 34px 1px rgba(0, 0, 0, 0.15);
}

.card > .card-body.server-status {
  display: flex;
  align-items: center;
}

.card > .card-body.server-status > .counter {
  width: 50%;
  font-weight: 500;
  color: #95a0b7;
  font-size: 32px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.card > .card-body.server-status > .counter > span {
  font-size: 15px!important;
  color: #0d2c4a!important;
  text-transform: capitalize;
}
        <div class="card">
          <div class="card-header light">
            Active Services
          </div>
          <div class="card-body server-status">
            <div class="counter">
              7/9
              <span>
                Servers running
              </span>
            </div>
            <div style="border-left:1px solid #0d2c4a;height:100%;"></div>
            <div class="chart">

            </div>
          </div>
        </div>

【问题讨论】:

  • 你的代码 sn-ps 工作不好,无法理解

标签: html css flexbox


【解决方案1】:

你需要让它伸展,因为你的弹性容器是align-items: center

可以去掉100%的高度,我给分隔线加了一个类,归根结底就是这样

.divider {
  align-self: stretch;
}

如果您没有对齐中心,默认情况下它会起作用,因为对齐项目默认为拉伸,但由于您将其更改为居中并且您的分隔线没有内容,因此该行不会显示。将分隔线本身设置为再次拉伸即可解决问题,并且不需要额外的 css

.card {
  margin-bottom: 30px;
}

.card>.card-header {
  font-weight: 500;
  text-transform: uppercase;
  font-size: 15px;
  margin-bottom: 6px;
}

.card>.card-header.light {
  color: #fff;
}

.card>.card-body {
  background-color: #fff;
  border-radius: 12px;
  padding: 24px;
  -webkit-box-shadow: -2px 4px 34px 1px rgba(0, 0, 0, 0.15);
  -moz-box-shadow: -2px 4px 34px 1px rgba(0, 0, 0, 0.15);
  box-shadow: -2px 4px 34px 1px rgba(0, 0, 0, 0.15);
}

.card>.card-body.server-status {
  display: flex;
  align-items: center;
}

.card>.card-body.server-status>.counter {
  width: 50%;
  font-weight: 500;
  color: #95a0b7;
  font-size: 32px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.card>.card-body.server-status>.counter>span {
  font-size: 15px!important;
  color: #0d2c4a!important;
  text-transform: capitalize;
}

.divider {
  align-self: stretch;
}
<div class="card">
  <div class="card-header light">
    Active Services
  </div>
  <div class="card-body server-status">
    <div class="counter">
      7/9
      <span>
         Servers running
      </span>
    </div>
    <div class="divider" style="border-left:1px solid #0d2c4a;"></div>
    <div class="chart"></div>
  </div>
</div>

【讨论】:

    【解决方案2】:

    您也可以将此 css 属性添加到您的 css ...

    .counter{
        border-right: 1px solid black;
    }
    

    【讨论】:

      【解决方案3】:

      不要使用&lt;div&gt;,而是尝试使用&lt;hr&gt; 并使用css 旋转它。大致如下:

      hr { 
          display: block;
          margin-top: 0.5em;
          margin-bottom: 0.5em;
          margin-left: auto;
          margin-right: auto;
          border-style: inset;
          border-width: 1px;
          transform: rotate(90deg);
      } 
      

      请参阅此文档以获取帮助:https://www.w3schools.com/tags/tag_hr.asp

      【讨论】:

      • 我不想用
        因为它代表水平线,对垂直线没有意义。
      猜你喜欢
      • 2019-12-02
      • 2018-01-03
      • 2015-02-14
      • 2015-09-05
      • 2010-09-15
      • 2012-07-09
      • 1970-01-01
      • 1970-01-01
      • 2019-08-08
      相关资源
      最近更新 更多