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

Thursday, November 17, 2022

[FIXED] How to vertically align item in Bootstrap 4?

 November 17, 2022     css, twitter-bootstrap, vertical-alignment     No comments   

Issue

I'm trying to vertically align text inside of a column. If I use my-auto in the parent (column), then it vertically aligns the text, but it pulls in everything to the center and I need to maintain the background. The background color is currently on the column.

When I remove my-auto, the full orange background returns, but the text is at the top.

I've tried creating an inner row>column structure to account for it, but it doesn't work. Any thoughts?

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container-fluid">
  <div class="row no-gutters">
    <div class="col-xs-6 col-sm-4 col-md-3 col-lg-4 col-xl-4 my-auto" style="background-color: #f04e23;">
      <h1 class="display-4 text-center">2018 Speakers</h1>
    </div> 
    <div class="col-xs-6 col-sm-4 col-md-3 col-lg-2 col-xl-2">
      <div class="card">
        <img class="img-fluid" src="https://picsum.photos/400/600" alt="" />
      </div>
    </div>
    <div class="col-xs-6 col-sm-4 col-md-3 col-lg-2 col-xl-2">
      <div class="card">
        <img class="img-fluid" src="https://picsum.photos/400/600" alt="" />
      </div>
    </div>
    <div class="col-xs-6 col-sm-4 col-md-3 col-lg-2 col-xl-2">
      <div class="card">
        <img class="img-fluid" src="https://picsum.photos/400/600" alt="" />
      </div>
    </div>
    <div class="col-xs-6 col-sm-4 col-md-3 col-lg-2 col-xl-2">
      <div class="card">
        <img class="img-fluid" src="https://picsum.photos/400/600" alt="" />
      </div>
    </div>
  </div>
</div>


Solution

Instead of using margins, set the column's display to flex by adding the .d-flex class, then use the .align-items-center class to center the text vertically. You could also then add .justify-content-center to center it horizontally:

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container-fluid">
  <div class="row no-gutters">
    <div class="col-xs-6 col-sm-4 col-md-3 col-lg-4 col-xl-4 d-flex align-items-center justify-content-center" style="background-color: #f04e23;">
      <h1 class="display-4 text-center">2018 Speakers</h1>
    </div> 
    <div class="col-xs-6 col-sm-4 col-md-3 col-lg-2 col-xl-2">
      <div class="card">
        <img class="img-fluid" src="https://picsum.photos/400/600" alt="" />
      </div>
    </div>
    <div class="col-xs-6 col-sm-4 col-md-3 col-lg-2 col-xl-2">
      <div class="card">
        <img class="img-fluid" src="https://picsum.photos/400/600" alt="" />
      </div>
    </div>
    <div class="col-xs-6 col-sm-4 col-md-3 col-lg-2 col-xl-2">
      <div class="card">
        <img class="img-fluid" src="https://picsum.photos/400/600" alt="" />
      </div>
    </div>
    <div class="col-xs-6 col-sm-4 col-md-3 col-lg-2 col-xl-2">
      <div class="card">
        <img class="img-fluid" src="https://picsum.photos/400/600" alt="" />
      </div>
    </div>
  </div>
</div>



Answered By - daGUY
Answer Checked By - Marilyn (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