Align two divs horizontally side by side center to the page using Bootstrap CSS
Align and center two divs
side by side in Bootstrap by placing them inside a .container
and a .row
, and using .col
or .col-auto
for equal or content-width-based sizing, respectively. Use .justify-content-center
for horizontal centering.
This layout applies Bootstrap's grid system for horizontally aligning and centering divs
with responsiveness across varying window sizes.
Grid sizing for responsive design
Bootstrap provides grid classes (col-sm-*
, col-md-*
, etc.) for different screen sizes. For optimal desktop and mobile responsiveness without extra media queries, use col-md-6
and col-6
to produce two half-width columns side by side.
Offset for fine control
Bootstrap’s offset classes (col-md-offset-*
) deliver precise alignment by creating extra space on a column's left side. Here's an example:
This offsets the first div
by two columns, centering the two divs
on the page.
Centre of attention: Advanced flex utilities
Bootstrap 4 leverages flex utilities for advanced alignment. Use d-flex
and justify-content-center
to center items both horizontally and vertically.
Multiple column classes for responsiveness
Using multiple col-*
classes tailors responsiveness for different screen sizes. col-xx
for general responsiveness and col-lg
, col-md
, col-sm
, col-xs
for specific breakpoints.
This responsive design, achieved with Bootstrap container and column classes, provides a clean, maintainable codebase.
When .center-block
isn't your friend
.center-block
in Bootstrap 3 doesn't play well with grid elements. Bootstrap 4 uses flex utilities for alignment, not .center-block
.
Was this article helpful?