Search code examples
htmlcssbootstrap-4vertical-alignment

Vertically align a div in the middle of a Bootstrap 4.1 container-fluid > row > col


I'm trying to apply Bootstrap 4.1's align-middle to a col inside of a row. But it seems the content inside it stays aligned to the top. Here is my code:

<body class="d-flex">
  <div class="container-fluid">
    <a class="my-container" href="#">
      <div class="row">
        <div class="col flex-grow-1">
          Line 1<br>Line 2<br>Line 3
        </div>
        <div class="col-auto text-right align-middle">
          <i class="fas fa-chevron-right"></i>
        </div>
      </div>
    </a>
  </div>
</body>

Here is a snippet showing the result:

<link href="https://use.fontawesome.com/releases/v5.4.2/css/all.css" rel="stylesheet"/>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<body class="d-flex">
  <div class="container-fluid">
    <a class="my-container" href="#">
      <div class="row">
        <div class="col flex-grow-1">
          Line 1<br>Line 2<br>Line 3
        </div>
        <div class="col-auto text-right align-middle">
          <i class="fas fa-chevron-right"></i>
        </div>
      </div>
    </a>
  </div>
</body>

How do I align the icon correctly?


Solution

  • You can use the Bootstrap 4's my-auto class on the column. Here is the code:

    html:

    <div class="container-fluid">
      <a class="my-container" href="#">
      <div class="row">
        <div class="col flex-grow-1 my-auto">
          <p>line 1</p>
          <p>line 1</p>
          <p>line 1</p>
        </div>
      <div class="col-auto text-right">
      <i class="fas fa-chevron-right"></i>
    </div>
    

    css:

    p {
      margin: 16px;
    }
    

    The paragraph has a margin-bottom of 1rem, so you can either remove the margin or just give it an overall margin like I did. This worked fine for me.