How to vertically center the contents of a flexbox item

前端 未结 2 1269
终归单人心
终归单人心 2021-01-31 16:52

I\'m trying to center the CONTENTS of a flexbox item while using justify-content:stretch; It appears that the old trick of using display:table-cell;vertical-a

2条回答
  •  长情又很酷
    2021-01-31 17:21

    It can be achieved by displaying each flex item as a flex container and then aligning the contents vertically by align-items property, as follows:

    .flex-container {
      display:flex;
      align-items:center;
      height: 200px; /* for demo */
    }
    
    .flex-item {
      align-self:stretch;
      display:flex;
      align-items:center;
      background-color: gold; /* for demo */
    }
    I want to be vertically centered! But I'm not.

    As a side-note, if you omit the align-items:center; declaration of the .flex-container you can safely remove align-self:stretch; from flex items as well.

提交回复
热议问题