Achieving Vertical Alignment of DIV and Text Using Bootstrap 4

After scouring through various questions, articles, and Bootstrap 4 documentation, I have yet to find the solution I am seeking.

DEMO: https://jsfiddle.net/zxLLqsm0/

My goal is to create boxes with uniform height for all columns while vertically aligning the text inside them. Although I have successfully aligned the text vertically, I am still struggling with achieving consistent box heights.

Below is my HTML structure:

<div class="container">
  <div class="row align-items-center">
    <div class="col-4">
      <div class="box">
        <h6>Title 1</h6>
        <p>A small description</p>
      </div>
    </div>
    <div class="col-4">
      <div class="box">
        <h6>Title 2</h6>
        <p>A bigger description goes here</p>
      </div>
    </div>
    <div class="col-4">
      <div class="box">
        <h6>Title 3</h6>
        <p>A large description is placed here to make whatever changes we need.</p>
      </div>
    </div>
  </div>
</div>

Below is my CSS:

.container {
  margin-top: 15px;
}
.box {
  background-color: grey;
  padding: 15px;
}

Answer №1

Essentially, the inquiry has already received a resolution.

To achieve center alignment for the box, utilize flexbox and justify-content-center, along with h-100 for height:100%...

<div class="container">
  <div class="row">
    <div class="col-4">
      <div class="box h-100 d-flex justify-content-center flex-column">
        <h6>Title 1</h6>
        <p>A brief summary</p>
      </div>
    </div>
    <div class="col-4">
      <div class="box h-100 d-flex justify-content-center flex-column">
        <h6>Title 2</h6>
        <p>A detailed description here</p>
      </div>
    </div>
    <div class="col-4">
      <div class="box h-100 d-flex justify-content-center flex-column">
        <h6>Title 3</h6>
        <p>An extensive description is included to accommodate any necessary modifications.</p>
      </div>
    </div>
  </div>
</div>


Alternatively, if you prefer making alterations to the .box directly instead of relying on Bootstrap classes...

https://jsfiddle.net/g38e9Lfm/

.box {
  background-color: grey;
  padding: 15px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  height: 100%;
}

Answer №2

You simply need to utilize standard bootstrap classes without the need for additional CSS writing

<div class="container">
  <div class="row">
    <div class="col-4">
      <div class="box d-table h-100 w-100">
        <div class="d-table-cell align-middle">
          <h6>Title 1</h6>
          <p>A brief description</p>
        </div>
      </div>
    </div>
    <div class="col-4">
      <div class="box d-table h-100 w-100">
        <div class="d-table-cell align-middle">
          <h6>Title 2</h6>
          <p>A more detailed description is included here</p>
        </div>
      </div>
    </div>
    <div class="col-4">
      <div class="box d-table h-100 w-100">
        <div class="d-table-cell align-middle">
          <h6>Title 3</h6>
          <p>An extensive description is provided to allow for any necessary modifications.</p>
        </div>
      </div>
    </div>
  </div>
</div>

Check out the demo here

Answer №3

Check out this method, it might provide some assistance.

.container{
  background-color: blue;
  padding: 20px;
  height: 250px; 
  display: flex;
  justify-content: center;
  align-items: center;
}

Enclose your text content within a new div with class = "content", and then apply the following styles

.content{
  display: flex;
  justify-content: center;
  align-items: center;
}

Answer №4

Swap out the class align-item-center for row-eq-height more details can be found here:

To better understand, refer to: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

If you want to vertically center align, you can use the following code:

    .box {
      background-color: grey;
      padding: 15px;
      height: 100%;
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      align-content: center;
      text-align: center;
      justify-content: center;
    }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilize Jquery to insert new text into a textbox and then organize the contents of the textbox using Jquery

Currently, I am utilizing the ASP.NET asp:checkboxlist control which consists of 36 listitems, displaying 36 checkboxes. The HTML representation resembles a table format. My goal is to dynamically add checked items in this div in a sorted manner. Additiona ...

How can CSS be used to create a responsive form script?

We have enlisted the services of an SEO provider through upcity.com, which necessitates us to embed a form on our website. However, we are facing challenges with the responsiveness of the form on mobile devices, which undermines our efforts. Therefore, I a ...

Troubleshooting: jQuery script encountering issues with loading Bodymovin JSON files

The animations for the slider and shuffle lottie are designed to go from 0 to 100 and then back to 0 when toggled, similar to the box animation. However, it appears that the slider animation disappears in the final frame while the shuffle animation appear ...

Recreated using CSS and JavaScript: iOS "Background Open" Animation

I am currently seeking a method to replicate the iOS Safari animation that occurs when opening a link in the background (the item jumps up and quickly animates to where it can be accessed) - This same animation is also seen on OSX Safari's "save to re ...

Implement a floating div below an accordion element on the page

As depicted in the image, there is a "Quick Tips" div located below the accordion. Let's assume the outermost div is named id="TipsDiv". The goal is to ensure that this div remains below the accordion as the user scrolls down, never going above positi ...

Adjust the vertical scaling of a container in CSS Grid to maintain aspect ratio similar to an <img>

I am attempting to recreate a demonstration where the house image scales as the viewport height decreases. However, instead of using an <img>, I would like to use a container that also includes absolutely positioned content (unable to provide all the ...

Spartacus 3.3 - Unleashing the Full Potential of the Default Sparta Storefront Theme

Looking for advice on how to customize the default Spartacus Storefront theme (Sparta) by only overriding specific CSS vars. Any suggestions? I tried following the instructions provided at: https://github.com/SAP/spartacus/blob/develop/projects/storefront ...

Look for specific content within a div tag and display that div at the top of the page

<div class="search-container"> <input type="text" class="input-medium search-query" placeholder="Search"> <button type="button" class="btn">Search</button> </div> <div class="panel panel-default"> ...

Capture information from an HTML5 form and securely store it in a MySQL database

I have a simple web form that includes a "date" type input field. I want to retrieve the date entered by the user and store it in my MySQL database, which has a DATE type column. I am looking for some PHP/JS magic to facilitate the communication between t ...

What is the best way to design a shape (a quadrilateral with rounded edges) using CSS?

Struggling with creating a widget in React Js that has an image as the background? The image is designed to be dynamic, changing color and size based on the device. I've attempted using inline SVG and SVG within an img tag but can't seem to contr ...

The AppJS warning: A potentially dangerous JavaScript attempt to access a frame with a URL

I am currently working on a desktop application where I am trying to filter specific divs inside an iframe using AppJS. However, I am facing an issue with hiding some of the div elements. Here is the code snippet: <!DOCTYPE html> <html> <s ...

Tips to avoid the page from scrolling to the top when the menu is opened

Whenever a user taps the menu button on the page, a full-page menu opens. However, there is an issue - the main content page automatically scrolls to the top. Can you provide suggestions on how to prevent this from happening? I have already looked into a s ...

Creating a React button animation with CSS vibes

When I click a button, I want a subtle sparkle to appear and disappear quickly, within half a second. I've thought about using a gif, but it requires a lot of manual artistic work to achieve the desired effect. Is there a way to utilize the Animatio ...

The font size in the responsive navbar displays properly on a computer, but is not rendering correctly on

There is an interesting issue that I have come across. I recently made all text on my Bootstrap4 website responsive by modifying the CSS. This adjustment has been successful, and here is the code snippet I used: @media (max-width: 576px) { html { font ...

Personalize the Vue.js datepicker to work seamlessly with Bootstrap 3

Currently the Vuejs datepicker for Bootstrap 3 has the following appearance: https://i.sstatic.net/dIKr8.png The label currently displays as 2018 April. I am looking to change it to display as April 2018. I have searched for an API reference within the ...

Align a series of items in the center while also ensuring they are left-justified

Issue Description : I am facing a problem with centering inline-block elements. When the elements exceed the width of a single line, Action Required : I want them to be justified to the left. I have tried using flex boxes and other methods, but I can ...

Ways to access large h5p files

My understanding is that h5p files cannot be accessed on a local device in offline mode (although there is an option like h5p-standalone, it requires setting up a local webserver). To view h5p content, a webserver that supports h5p is necessary, such as th ...

I need to adjust my navbar in Bootstrap 5 so that one <li> item floats to the left while the remaining items float to the right

Currently embarking on my inaugural html/css venture while utilizing bootstrap. I do have some previous html experience from using Blogger in the past. Issue I'm encountering is that when expanding my navbar, I aim to have the first li item (a button ...

Add some animation to elements when the page loads

I am curious about triggering a css animation when the page loads. I am currently experimenting with css animations for the first time. Here is what I have created so far: https://jsfiddle.net/7prg793g. However, it only works upon hover at the moment. * ...

React Card with Overflowing TableCell Texts

I am facing an issue with a table inside a table card where the TableCell is overflowing horizontally when the word is too long. How can I break it to the next line? Please refer to the "code" section for details. For more information, please visit my cod ...