Issue
I have content that I want centered on the page. Inside a div I want items to stay close together (top image), but when the page is resized wider Bootstrap adds space (bottom image) - trying to put them on new column boundaries?
How do I get Bootstrap 5 to keep them compacted to center of page?
<div class="container text-center center-container">
<div class="row">
<div class="col">
<div>
image
</div>
</div>
<div class="col">
Some texty bits
</div>
<div class="col">
<div>
image
</div>
</div>
</div>
</div>
Solution
You can use flexbox
with Bootstrap's flex
utility classes.
You can also use the col-{breakpoint}-auto
class in grid to achieve variable width columns.
Solution and demos
https://codesandbox.io/s/bootstrap-flex-example-k7iyn3
With flex:
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
crossorigin="anonymous"
/>
<div class="d-flex justify-content-center">
<div class="d-flex justify-content-center gap-4">
<div>
<img src='https://via.placeholder.com/150'>
</div>
<div class="p-4 bg-info">
Some texty bits
</div>
<div>
<img src='https://via.placeholder.com/150'>
</div>
</div>
</div>
With grid:
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
crossorigin="anonymous"
/>
<div class="container">
<div class="row gx-0 justify-content-sm-center gap-4">
<div class="col col-sm-auto">
<img src="https://via.placeholder.com/150" />
</div>
<div class="col-sm-auto p-4 bg-info">
Some texty bits
</div>
<div class="col col-sm-auto">
<img src="https://via.placeholder.com/150" />
</div>
</div>
</div>
Answered By - abgregs Answer Checked By - Terry (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.