How To Center a div Horizontally in Bootstrap 4

  • read
  • Add to Flipboard Magazine.

Screen Shot 2019 08 14 at 5.20.13 AM

It is very common in web design layouts to horizontally center a column in a row or container. But how do you do that using Bootstrap classes? Below are some different ways you can achieve this effect.

Option 1 – Bootstrap Offset

You can use Bootstrap offset classes to horizontally shift columns left or right. This is helpful when you want one column to have a max width but center it in a row.

Option 2 – Margin Auto

This example sets the maximum width on medium breakpoints and up to be 8 columns wide. Then I added a utility class of .mx-auto that sets the margin left and right to auto centering it horizontally inside the .row container.

Option 3 – Bootstrap Width Utility

In this example, I am using Bootstrap’s width utility class of .w-50 to make the div 50% wide all the time. Then I added a utility class of .mx-auto that sets the margin left and right to auto centering it horizontally inside the .row container.

Conclusion

As in most cases in web development, there is more than one way to achieve the same result. Choosing the right approach for your project depends on what you think is easiest to remember and maintain. Personally, I like Option 1 because it allows you to horizontally center the column on large screens and then go full width on mobile.

View Codepen Demo

bootstrap 4 books

The Bootstrap 4 Quick Start

Learn More

Advertise Here

Written by: Jacob Lett - Jacob is an experienced HubSpot website designer and developer who helps Industrial companies increase revenue by improving their website for awareness, lead generation, and branding.
Category: Common Questions
Tags:

Want to Get Email Updates?

Receive a monthly digest of new tutorials and resources.

Advertise Here