vertical-align with Bootstrap 3

Cover Image for vertical-align with Bootstrap 3
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

Aligning Divs Vertically with Bootstrap 3: Your Complete Guide! 🚀

If you've ever used Bootstrap 3, you might have run into the problem of aligning two div vertically. It can be a real headache, especially when you want your design to be responsive. But don't worry, we've got you covered! In this blog post, we'll walk you through the common issues you might encounter and provide easy solutions to align your divs vertically with Bootstrap 3. Let's dive in! 💪

The Problem 😕

So you have two divs that you want to align vertically. For example, you might have a large div and a small div, and you want them to be vertically aligned within a row. Here's the code snippet that demonstrates the problem:

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>

<div class="row">
  <div class="col-xs-5">
    <div style="height:5em;border:1px solid #000">Big</div>
  </div>
  <div class="col-xs-5">
    <div style="height:3em;border:1px solid #F00">Small</div>
  </div>
</div>

Understanding the Root Cause 🕵️‍♀️

The grid system in Bootstrap uses float: left instead of display: inline-block. As a result, the vertical-align property doesn't work as expected. This is where the problem lies. 😢

The Solution 💡

To align your divs vertically in Bootstrap 3, we can leverage the power of Flexbox. Flexbox is a modern CSS layout model that allows you to easily align and distribute elements within a container. Here's how you can apply Flexbox to align your divs vertically:

  1. Wrap your divs in a container with the class d-flex and align-items-center. This will make the container a flex container and align its items vertically in the center.

<div class="row d-flex align-items-center">
  <!-- Your divs here -->
</div>
  1. Remove the col-xs-5 classes from your individual divs and replace them with a single class col-xs-6. This will make both divs occupy half of the row, resulting in a vertically aligned layout.

<div class="row d-flex align-items-center">
  <div class="col-xs-6">
    <!-- Your large div here -->
  </div>
  <div class="col-xs-6">
    <!-- Your small div here -->
  </div>
</div>
  1. Style your individual divs as needed, such as adding borders or setting heights. Flexbox will take care of the vertical alignment.

<div class="row d-flex align-items-center">
  <div class="col-xs-6">
    <div style="height:5em;border:1px solid #000">Big</div>
  </div>
  <div class="col-xs-6">
    <div style="height:3em;border:1px solid #F00">Small</div>
  </div>
</div>

Voila! ✨

That's it! With just a few tweaks using Flexbox, you can easily align your divs vertically in Bootstrap 3. No more headaches or hacks using margin-top! 😎

Take It Further 🚀

Now that you know how to align divs vertically in Bootstrap 3, why not experiment with different layouts and alignments? Flexbox offers a whole range of possibilities for creating beautiful and responsive designs. Take your Bootstrap skills to the next level!

Conclusion 📝

Aligning divs vertically in Bootstrap 3 doesn't have to be a struggle anymore. By utilizing the power of Flexbox, you can easily achieve the desired layout without compromising responsiveness. Give it a try and let us know how it goes! Happy coding! 💻

PS: Do you have any other Bootstrap-related questions or challenges? Share them in the comments below and let's tackle them together! 👇😊


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello