Bootstrap 4, How do I center-align a button?

后端 未结 8 995
梦谈多话
梦谈多话 2021-01-30 00:22
8条回答
  •  梦毁少年i
    2021-01-30 01:07

    With the use of the bootstrap 4 utilities you could horizontally center an element itself by setting the horizontal margins to 'auto'.

    To set the horizontal margins to auto you can use mx-auto . The m refers to margin and the x will refer to the x-axis (left+right) and auto will refer to the setting. So this will set the left margin and the right margin to the 'auto' setting. Browsers will calculate the margin equally and center the element. The setting will only work on block elements so the display:block needs to be added and with the bootstrap utilities this is done by d-block.

    
    

    You can consider all browsers to fully support auto margin settings according to this answer Browser support for margin: auto so it's safe to use.

    The bootstrap 4 class text-center is also a very good solution, however it needs a parent wrapper element. The benefit of using auto margin is that it can be done directly on the button element itself.

提交回复
热议问题