Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap 4 vertical align text won't center on card

Tags:

Trying to vertically align the text center in the following card:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">

<div class="row text-center h-100">
  <div class="col-md-3 text-center my-auto">
    <div class="card card-block justify-content-center" style="height: 120px">
      <div class="card-body">
        This is some text within a card body.
      </div>
    </div>
  </div>
</div>

I am fully aware of the many similar questions here, however I tried various solutions and none of them seems to work for my cards.

I tried "my-auto" on parent, card, and card-body. I tried "d-flex align-items-center h-100" and "justify-content-center". Also tried to play around with the display property. What am I missing?

like image 317
tealowpillow Avatar asked Oct 20 '17 20:10

tealowpillow


People also ask

How do I center my bootstrap card vertically?

One way to vertically center is to use my-auto . This will center the element within it's flexbox container (The Bootstrap 4 . row is display:flex ). For example, h-100 makes the row full height, and my-auto will vertically center the col-sm-12 column.

How do you center vertically bootstrap 4 CSS?

Since Bootstrap 4 . row is now display:flex you can simply use align-self-center on any column to vertically center it... or, use align-items-center on the entire .

How to vertically align items in Bootstrap 4?

To vertically align items, Bootstrap 4 provides different techniques, such as: Vertical Align utilities for changing the vertical alignment of inline, inline-block, inline-table, and table cell elements combined with Display utilities

How do I Center a row vertically in Bootstrap 4?

One way to vertically center is to use my-auto. This will center the element within it’s flexbox container (The Bootstrap 4 .row is display:flex ). For example, h-100 makes the row full height, and my-auto will vertically center the col-sm-12 column. my-auto represents margins on the vertical y-axis, and is equivalent to:

How do you center align text in HTML?

To make the text aligned center, use the .text-center class. In the following example, this class is applied in a paragraph and h1 to h6 headings to make text aligned centered: For aligning text right, use the .text-right class in the element.

How to center content in Bootstrap?

Much love to bootstrap! You can use Flexbox here. Just add .d-flex and .justify-content-center and it'll be centered. Sorry, something went wrong.


1 Answers

Here is a solution using bootstrap 4 documentation for flexbox. Read more here

What you need to know,

d-flex -> makes the element a flexbox.

align-items-center - vertically aligns the content.

justify-content-center - horizontally aligns the content.

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">

<div class="row text-center h-100">
  <div class="col-md-3 text-center my-auto">
    <div class="card card-block d-flex" style="height: 120px">
      <div class="card-body align-items-center d-flex justify-content-center">
        This is some text within a card body.
      </div>
    </div>
  </div>
</div>
like image 151
Naren Murali Avatar answered Sep 27 '22 19:09

Naren Murali