Align flex-box items to baseline of last text line in a block

后端 未结 2 1593
暖寄归人
暖寄归人 2021-02-14 16:18

I am trying to achive the last example on the following image, using flex-box.

From what I see, the align-items: baseline; property works great whe

相关标签:
2条回答
  • 2021-02-14 16:38

    At the time of writing the CSS box model alignment working draft proposes a ‘first’ and ‘last’ value to be added to ‘align-items’. The would allow:

    align-items: last baseline

    Current it only appears to be supported by Firefox so is one for the future.

    https://developer.mozilla.org/en-US/docs/Web/CSS/align-items

    0 讨论(0)
  • 2021-02-14 16:44

    You can wrap the contents of the flex items inside inline-block wrappers:

    .flex {
      display: flex;
      align-items: baseline;
    }
    .inline-block {
      display: inline-block;
    }
    
    .item { border: 1px solid red; }
    .item:first-child { font-size: 200%; }
    .flex::after { content: ''; position: absolute; left: 0; right: 0; border-top: 1px solid blue; }
    <div class="flex">
      <div class="item">
        <div class="inline-block">Lorem<br />Ipsum<br />Dolor</div>
      </div>
      <div class="item">
        <div class="inline-block">Foo bar</div>
      </div>
    </div>

    That will work because, according to CSS 2.1,

    The baseline of an 'inline-block' is the baseline of its last line box in the normal flow, unless it has either no in-flow line boxes or if its 'overflow' property has a computed value other than 'visible', in which case the baseline is the bottom margin edge.

    0 讨论(0)
提交回复
热议问题