flexbox将项目对齐到指定的基线?

due*_*tep 6 html css alignment css3 flexbox

我想知道在使用flexbox并尝试使用时是否有一种覆盖/指定基线的方法align-items: baseline.

我有一个flex容器,它包含几个不同的div(即title,img,body,description).

有了这些flex项目,我想以div的基线为中心.flex-body.它应该以"此处中心"文本的下划线为中心.

这就是我目前的尝试.

在此输入图像描述

我希望它看起来像这样,每一行都有以"中心在这里"下划线为中心的弹性项目 - 没有完美排列,因为我只是在那里粘贴边缘以使图像看起来像我的样子通缉:P

在此输入图像描述

如果将项目与基线对齐是我需要的,我如何使用它将我的flex divs置于项目中间的下划线?

codepen

.flex-container {
  display: -webkit-flex;
  display: flex;
  -webkit-align-items: baseline;
  align-items: baseline;
  width: 400px;
  height: 350px;
  background-color: lightgrey;
  flex-wrap: wrap;
}
.flex-item {
  background-color: cornflowerblue;
  width: 100px;
  margin: 10px;
  display: inline-flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
}
.flex-body {
  border-bottom: 1px solid #fff;
}
.flex-img {
  justify-content: center;
  align-items: center;
}
Run Code Online (Sandbox Code Playgroud)
<div class="flex-container">
  <div class="flex-item">
    <div class="flex-title">
      flex title1
    </div>
    <div class="flex-img">
      <img src='http://dummyimage.com/90x40/000/ffffff&text=hi'>
    </div>
    <div class="flex-body">
      center here
    </div>
    <div class="flex-body-more">
      more text
    </div>
  </div>
  <div class="flex-item">
    <div class="flex-title">
      flex title1 longer title

    </div>
    <div class="flex-img">
      <img src='http://dummyimage.com/40x40/000/ffffff&text=hi'>
    </div>
    <div class="flex-body">
      center here
    </div>
    <div class="flex-body-more">
      more text
    </div>
  </div>
  <div class="flex-item">
    <div class="flex-title">
      flex title1
    </div>
    <div class="flex-img">
      <img src='http://dummyimage.com/40x90/000/ffffff&text=hi'>
    </div>
    <div class="flex-body">
      center here
    </div>
    <div class="flex-body-more">
      more text more text more text
    </div>
  </div>
  <div class="flex-item">
    <div class="flex-title">
      flex title1
    </div>
    <div class="flex-img">
      <img src='http://dummyimage.com/90x40/000/ffffff&text=hi'>
    </div>
    <div class="flex-body">
      center here
    </div>
    <div class="flex-body-more">
      more text
    </div>
  </div>
  <div class="flex-item">
    <div class="flex-title">
      flex title1
    </div>
    <div class="flex-img">
      <img src='http://dummyimage.com/40x50/000/ffffff&text=hi'>
    </div>
    <div class="flex-body">
      center here
    </div>
    <div class="flex-body-more">
      more text
    </div>
  </div>

</div>
Run Code Online (Sandbox Code Playgroud)

Ori*_*iol 12

您可以:

  • 将弹性项目的内容包裹在inline-blocks中

    这是因为基线inline-block是一个非常有趣的位置:

    "内联块"的基线是正常流程中其最后一个线框的基线

    <div class="flex-item">
      <div class="inner-wrapper">
        <!-- contents here -->
      </div>
    </div>
    
    Run Code Online (Sandbox Code Playgroud)
    .inner-wrapper {
      display: inline-block;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 在所需基线后浮动内容

    这样,在计算基线时,它们将被忽略,因为浮点数不在流量范围内.这有一些横向影响,由于inline-block包装器建立块格式化上下文,因此可以减轻这种影响.

    如果您有多个,可以清除它们或使用width: 100%它们来防止它们水平堆叠.

    .flex-body-more {
      float: left; /* Take out-of-flow */
      clear: left; /* Do not stack horizontally */
      width: 100%; /* Do not shrink-to-fit */
    }
    
    Run Code Online (Sandbox Code Playgroud)

.flex-container {
  display: -webkit-flex;
  display: flex;
  -webkit-align-items: baseline;
  align-items: baseline;
  width: 400px;
  height: 350px;
  background-color: lightgrey;
  flex-wrap: wrap;
}
.inner-wrapper {
  display: inline-block;
  text-align: center;
  width: 100px;
  margin: 10px;
  background-color: cornflowerblue;
}
.flex-body {
  border-bottom: 1px solid #fff;
}
.flex-body-more {
  float: left;
  clear: left;
  width: 100%;
}
.flex-img {
  justify-content: center;
  align-items: center;
}
Run Code Online (Sandbox Code Playgroud)
<div class="flex-container">
  <div class="flex-item">
    <div class="inner-wrapper">
      <div class="flex-title">flex title1</div>
      <div class="flex-img">
        <img src='http://dummyimage.com/90x40/000/ffffff&text=hi'>
      </div>
      <div class="flex-body">center here</div>
      <div class="flex-body-more">more text</div>
    </div>
  </div>
  <div class="flex-item">
    <div class="inner-wrapper">
      <div class="flex-title">flex title1 longer title</div>
      <div class="flex-img">
        <img src='http://dummyimage.com/40x40/000/ffffff&text=hi'>
      </div>
      <div class="flex-body">center here</div>
      <div class="flex-body-more">more text</div>
    </div>
  </div>
  <div class="flex-item">
    <div class="inner-wrapper">
      <div class="flex-title">flex title1</div>
      <div class="flex-img">
        <img src='http://dummyimage.com/40x90/000/ffffff&text=hi'>
      </div>
      <div class="flex-body">center here</div>
      <div class="flex-body-more">more text more text more text</div>
    </div>
  </div>
  <div class="flex-item">
    <div class="inner-wrapper">
      <div class="flex-title">flex title1</div>
      <div class="flex-img">
        <img src='http://dummyimage.com/90x40/000/ffffff&text=hi'>
      </div>
      <div class="flex-body">center here</div>
      <div class="flex-body-more">more text</div>
    </div>
  </div>
  <div class="flex-item">
    <div class="inner-wrapper">
      <div class="flex-title">flex title1</div>
      <div class="flex-img">
        <img src='http://dummyimage.com/40x50/000/ffffff&text=hi'>
      </div>
      <div class="flex-body">center here</div>
      <div class="flex-body-more">more text</div>
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 精彩纷呈!Flexbox、浮动和“内联块”都集中在一处。实际上使用最深埋的规范语言(实际上是最后一行)来进行“内联块”基线对齐。最后,有人将其投入使用:-) 干得好。 (2认同)