PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 29, 2022

[FIXED] How do I get Bootstrap to center content so items stay horizontally compact?

 September 29, 2022     bootstrap-5, css, html, javascript     No comments   

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?

This: enter image description here

Instead of this: enter image description here

<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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing