CSS center display inline block?

后端 未结 9 1825
误落风尘
误落风尘 2020-11-22 08:25

I have a working code here: http://jsfiddle.net/WVm5d/ (you might need to make the result window bigger to see the align center effect)

Question

9条回答
  •  借酒劲吻你
    2020-11-22 09:03

    The accepted solution wouldn't work for me as I need a child element with display: inline-block to be both horizontally and vertically centered within a 100% width parent.

    I used Flexbox's justify-content and align-items properties, which respectively allow you to center elements horizontally and vertically. By setting both to center on the parent, the child element (or even multiple elements!) will be perfectly in the middle.

    This solution does not require fixed width, which would have been unsuitable for me as my button's text will change.

    Here is a CodePen demo and a snippet of the relevant code below:

    .parent {
      display: flex;
      justify-content: center;
      align-items: center;
    }
    
    .child {
      display: inline-block;
    }

提交回复
热议问题