How do you center content in Bootstrap?

<div class="d-flex justify-content-center align-items-center bg-body-tertiary border" style="height: 140px">
  <div class="p-3 bg-primary text-white rounded">Centered both ways</div>
</div>
Live preview

  • Horizontal text: .text-center.
  • A block with a width: .mx-auto.
  • Both directions: .d-flex .justify-content-center .align-items-center on the parent with a height.

See Flex Utilities.


Learn more about the similar topics:
Tutorials
Bootstrap Spacing: Margin, Padding and Gap
Bootstrap Flex Utilities
Bootstrap Utilities
Exercises & Assignments
Center a box horizontally and vertically with flex utilities
Interview Questions & Answers
How do you center content in Bootstrap?
What are the spacing classes (margin and padding) in Bootstrap?
How do you show or hide elements on different screen sizes in Bootstrap?
What is the difference between d-none, invisible and visually-hidden?