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

后端 未结 8 991
梦谈多话
梦谈多话 2021-01-30 00:22
相关标签:
8条回答
  • 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.

    <button type="submit" class="btn btn-primary mx-auto d-block">Submit</button>
    

    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.

    0 讨论(0)
  • 2021-01-30 01:21

    you can also just wrap with an H class or P class with a text-center attribute

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