The center alignment for content in my columns is not functioning

I have created a layout using the following HTML and CSS:

<div class="container-fluid header">
    <div class="row text-center">
        <div class="col-md-6 red">
            <h1>First</h1>
        </div>
        <div class="col-md-6 red">
            <h1>Second</h1>
        </div>
    </div>
</div>

CSS

.header {
    background-color: blue;
}

.col-md-6 {
    min-height: 300px;
}

.red {
    border:2px solid red;
}

I have set the minimum height on col-md-6 to create extra space. I have also used text-center on the row to center the text horizontally. However, I am struggling to vertically center the content of the columns.

I have attempted the following:


<div class="container-fluid header">
    <div class="row text-center d-flex align-content-center">
        <div class="col-md-6 red align-self-center">
            <h1>First</h1>
        </div>
        <div class="col-md-6 red align-self-center">
            <h1>Second</h1>
        </div>
    </div>
</div>

Unfortunately, this approach did not work for me.

Answer №1

Using Bootstrap Utility Classes for Centering

To center the contents of a column in Bootstrap, you can utilize the following utility classes: d-flex, align-items-center, and justify-content-center.

Direct CSS Application

These utility classes essentially translate to the following CSS:

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

Understanding Flexbox Alignment

When dealing with a flex container with a row direction (which is the default), align-items controls the vertical alignment, while justify-content manages the horizontal alignment.

For further insight into flex layouts, I highly recommend checking out this helpful CSS-Tricks post on flexbox.

Visual Example

.header {
    background-color: blue;
}

.col-md-6 {
    height: 300px;
}

.red {
    border:2px solid red;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container-fluid header">
    <div class="row">
        <div class="col-md-6 red d-flex align-items-center justify-content-center">
            <h1>First</h1>
        </div>
        <div class="col-md-6 red">
            <h1>Second</h1>
        </div>
    </div>
</div>

Answer №2

Adding the 'd-flex' class to the div with the class "col" is essential.

Although the default behavior for a row is to be a flexbox, this is not the case for columns. The content wasn't aligning properly because the column wasn't a flexbox until the 'd-flex' class was applied to it.

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

How come my initial heading isn't aligned in the middle like the subsequent one?

I am having trouble centering my title Learn About Us correctly, similar to the title What we do. It seems there is an issue with the alignment of the picture, and I'm not sure how to fix it. https://i.sstatic.net/grWdK.png I need help understanding ...

How can I center the navbar logo and use it as a toggle for collapsing the menu?

This is a simple example of a navigation bar <nav class="navbar navbar-expand-lg bg-light"> <div class="container-fluid"> <a class="navbar-brand" href="#">Navbar</a> <button class ...

Ways to retrieve all elements, including those that are hidden, within a CSS grid

My goal is to retrieve all the "Available Items" using Javascript in one go. However, I am facing an issue where not all of them are visible at once due to the need to manually scroll through a CSS grid. <div class="gridWrapper" data-dojo-attach-point= ...

struggling to get the basic .gif loader to function properly

I recently designed a website with multiple images on certain pages, and I wanted to incorporate a loading gif that shows up in a specific div (#carousel) while the images load. I found a super simple guide on a website that I attempted to follow, but unfo ...

Reduce the page zoom to 75% without changing the page layout

I am looking to implement a forced zoom out feature on a webpage, reducing the overall size to 75%. Unfortunately, I am unable to disclose the specific code as it belongs to the company I work for. Despite researching various methods online, including solu ...

Using Conditional Tags for CSS Display

I'm working on implementing Conditional Tags to toggle a CSS class on or off based on the current displayed page. However, I'm running into an issue where the code isn't executing properly. There might be a syntax or logic error causing the ...

Showcasing just the initial two lines of a flexbox using CSS and HTML

My current project requires me to develop a search bar that displays search results in a grid format. The items should be arranged next to each other in two rows. If there are additional results, a button labeled +32 should be present to show all results. ...

Tips for aligning a row with both text and input fields in the center

I'm working on a design that includes text and input fields in rows, and I want everything to be centered. Should I wrap all the content in a div and center it as a whole, or should I center each row individually? Any suggestions? Here is the code sn ...

Guide on inserting text within a Toggle Switch Component using React

Is there a way to insert text inside a Switch component in ReactJS? Specifically, I'm looking to add the text EN and PT within the Switch Component. I opted not to use any libraries for this. Instead, I crafted the component solely using CSS to achie ...

Creating movement in three distinct divisions

I am seeking a way to have three divs flying in upon click. The first DIV is positioned at the top, followed by one on the left and one on the right (both being below the top one). I wish for them to fly in from their respective directions - the top div fr ...

Create a dynamic HTML design with a resizable left panel and a static right panel. The left panel should not wrap text and display ellipsis if necessary

I am looking to display items in a list with a specific format: |Name | Some other val1| |Very very long no wrap line that...| Some other val2| The right side needs to have a fixed width. The left side should fill al ...

What steps do I need to take in order to integrate my unique landing page design into Wordpress

Currently, I am involved in a project that utilizes Node.js on Docker. As part of this project, I have developed a landing page using Bootstrap. In the future stages of the project, our team plans to incorporate Wordpress as a content management system ( ...

Tips for displaying a button when hovering over it on large screens and larger sizes

I am trying to achieve a specific behavior where the button with class .bookmark-btn is only visible on md size screens and smaller at all times. However, on lg size screens and bigger, it should only appear when hovered over. How can I accomplish this? Cu ...

(Monetate) Resolving the issue of delayed transition between the <a> element and CSS arrow in the breadcrumb menu utilizing HTML/CSS/jQuery

Recently, I've been working on implementing a breadcrumb-style checkout progress indicator on my website using a code generator created by an incredible developer named Jonas Ohlsson. This feature utilizes HTML, CSS, and jQuery and is integrated into ...

Choose a sibling element using CSS styling

As I'm developing an AngularJS component, I am on the quest to identify ANY sibling of a particular tag, irrespective of whether they are div, span, or p. I'm hoping for a CSS-native solution that resembles something along the lines of div:siblin ...

After saving a rich field in Microsoft Sharepoint, I noticed that a new "External Class" was automatically added to my CSS

Although I am new to Sharepoint, I have experience in HTML and CSS. I recently created a mini HTML application that changes the pictures of hyperlinks on hover. It works perfectly when run on a normal browser outside of Sharepoint. However, the issue aris ...

Steps for filling a dropdown menu in Bootstrap with data from a SQL database

Can someone please assist me in populating a dropdown list in bootstrap with values from an SQL table? I want to dynamically populate the dropdown list with values from the SQL table. Below is a snippet of my HTML code: <div class="dropdown"&g ...

Looking for help to prevent my bootstrap nav bar from collapsing and showing a hamburger menu on mobile devices. This is my first CSS project and I am seeking guidance

Looking for help keeping my bootstrap nav bar from collapsing on mobile devices (want it to display a hamburger icon). I'm new to CSS so any guidance is appreciated! This is the code for my nav bar using bootstrap classes... <nav class="navb ...

Selenium WebDriver: The challenge of using visibilityOfElementLocated when the element is not actually visible

I've encountered an issue with the Firefox Webdriver where it fails to accurately determine if an element is visible. Here is the code I am using, which incorrectly indicates that the element is visible: wait.ignoring(UnhandledAlertException.class).u ...

Issue with CSS not appearing in Google Chrome's coverage tab

As I delve into analyzing the extent of unused CSS on my webpage, I encounter a challenge. The webpage is crafted in Angular 7, with CSS incorporated through the angular.json build configuration. The css appears to be added to the head of the html file, e ...