How to vertically center the contents of a flexbox item

前端 未结 2 1324
终归单人心
终归单人心 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

    I'm not sure if I am interpreting your request correctly, but I think you are trying to use "justify-content:stretch;" when you should be using flex-grow.

    Im my sample I used flex:1 auto; which is just shorthand to set a couple of flex properties.

    HTML:

    I'm top dog!
    I want to be vertically centered! And I am!

    CSS:

    *{
        box-sizing:border-box;
        padding:5px;
    }
    .flex-container {
        border: 1px solid;
        display:flex;
        align-items:center;
        height: 100px;
    }
    .flex-item {
        flex: 1 auto;
        border: 1px solid green;
        height:100%;
        display:flex;
        justify-content:center;
    }
    .flex-center {
        align-items: center;
    }
    

    http://jsfiddle.net/p28tjskq/

    Flex property:
    https://developer.mozilla.org/en-US/docs/Web/CSS/flex

提交回复
热议问题