Aligning text with headings using Bootstrap CSS

I am facing a challenge with aligning two elements in the middle. I have a column setup where col-md-4 contains an h1 element and col-md-8 contains a paragraph element. Despite my efforts to use line-height for alignment, sometimes on smaller screens the text wraps onto two lines causing excessive spacing. My goal is to align both elements in the center regardless of whether there are 1, 2, or 3 lines in the paragraph.

<div class="container-fluid">
        <div class="container">
            <div class="row">
                <div class="col-md-4">
                    <h1>Heading One</h1>
                </div>
                <div class="col-md-8">
                    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla non risus nibh. Suspendisse vehi.</p>
                </div>
            </div>
        </div>
    </div>

Answer №2

To achieve centering in those columns, consider assigning a class to them and applying flexbox properties to that class. Make sure to specify a min-width for proper visualization of the centering.

You can use media queries to disable this functionality when needed.

This code snippet is optimized for fullscreen viewing.

.col-flex {
  display: flex;
  align-items: center;
  min-height: 200px !important;
}

.col-flex h1,
.col-flex p {
  margin: 0;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid">
        <div class="container">
            <div class="row">
                <div class="col-md-4 col-flex">
                    <h1>Heading One</h1>
                </div>
                <div class="col-md-8 col-flex">
                    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla non risus nibh. Suspendisse vehi.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla non risus nibh. Suspendisse vehi. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla non risus nibh. Suspendisse vehi. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla non risus nibh. Suspendisse vehi.</p>
                </div>
            </div>
        </div>
    </div>

Answer №3

There are two ways to center them:
1. Flexbox
2. display: table/table-cell

If you choose table-cell, with bootstrap you may need to adjust the float property as well ;)

Check out this page for examples of four different vertical alignment methods.

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

The element 'HTMLVideoElement' does not support the property 'captureStream'

Currently, I am working on a project that involves using WebRTC in React with typescript. I came across the MDN HTMLMediaElement.captureStream() documentation which I followed. const vid: HTMLVideoElement | null = document.querySelector("video") ...

Width of the table caption

A challenge has been presented to me - I must improve the accessibility of an existing table layout containing tabular data. My first step was to add a caption tag to each table, but I quickly discovered that the width of these captions varies across diff ...

Stopping the <nav> element from displaying as "unlabeled section" on HTML5 webpages

As I work on incorporating proper sectioning in HTML5 with sectioning elements and headlines to meet my customer's design goals (while adhering to certain restrictions), I have encountered a challenge. The basic layout requested is as follows: <b ...

What is causing iframes to fail on some websites?

Recently, while I was experimenting with a jQuery tutorial, I encountered an issue where iframes were not working as expected. Surprisingly, only the iframes from w3schools seemed to work fine, leaving me confused about what could be causing the problem. & ...

Encountering an issue when adding SASS Compass Breakpoint to my development project

I'm having trouble integrating breakpoints into my web project. I have SASS, Compass, and breakpoint all installed and I've followed the installation instructions, but it seems to be causing errors. Can someone help me troubleshoot the issue? He ...

Create animated changes in height for a mdDialog as elements are shown or hidden

Utilizing Angular Material, I have implemented tabs within an md-dialog. The dialog smoothly adjusts its height based on the content of the active tab during navigation. However, when utilizing an ng-if directive to toggle visibility of content, there is n ...

Looking for assistance with regards to submitting an event

I have been working on developing a form and validating it using jQuery before submitting it to the server. Everything seems to be functioning correctly except for the function on the submit event. The form is being submitted with errors and upon checking ...

Introducing the World of Wordpress Blogging

How can I create an introduction on my WordPress site similar to the one found at ? I am specifically interested in incorporating the expanding horizon line effect. It seems like it may just be a GIF that plays and then fades into the homepage. Are there ...

Why is my CSS not showing up in Live Server when I preview it from Visual Studio?

Every time I use the "Go Live" option with Live Server (Visual Studio extension), I only get a preview of my plain HTML file. However, when I manually open the HTML file from the folder containing both HTML and CSS files, the page loads correctly with the ...

How can you use Flexbox to vertically center the content of a list item?

Here is a simple list example that I have created: ul{background:wheat;height:200px;text-align:center;} li{height:200px;display:inline-block;margin-right:10px;background:green;color:white;} <ul> <li>Item 1</li> ...

What is preventing the Navbar from aligning to the right side in Bootstrap 5?

I'm trying to position only the "Account" navbar all the way on the right side using ms-auto, but it doesn't seem to be working correctly. Here's my code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="u ...

Each row in a table is filled with an array of data, with each column containing pdf

My goal is to split each associated element in every row of my PDF. Currently, the values are displayed below each column, as shown in the image below. packStruct = [] packEnergy = [] packDescriptionEnergy = [] packPrice = [] {text: 'Integration&apos ...

In what scenarios does Element.getClientRects() provide a collection of multiple objects as a return value?

Every time I use Element.getClientRects(), it always gives me a collection containing just one DOMRect object. Under what circumstances does Element.getClientRects() return a collection with multiple DOMRect objects? function handleClick() { console. ...

Exploring CSS Images, Graphics, and Typography in the Laravel 4 Framework

I am encountering some difficulties with setting up my assets in the Laravel 4 Framework. To provide some context, I have an assets folder within my public directory, containing subfolders for css, images, and fonts. Here are the paths: /laravel-master/pu ...

What is the best way to reduce the width of the preformatted tag?

Is there a way to adjust the width of the pre tag in order to avoid affecting the q-card when resizing the browser window? I am trying to create a q-card that can display code similar to what you see in ChatGPT. However, the q-card's size is not respo ...

Enhance your Angular material datepicker with additional content such as a close button

I'm currently working on customizing my Angular Material datepicker by adding a button. The goal is to have this button placed in the top right corner and enable it to close the datepicker when clicked. In addition, I also want to include extra conte ...

Using Angular expressions, you can dynamically add strings to HTML based on conditions

I currently have the following piece of code: <div>{{animalType}}</div> This outputs dog. Is there a way to conditionally add an 's' if the value of animalType is anything other than dog? I attempted the following, but it did not ...

What could be causing my custom Google font in CSS to not function properly?

I've been trying to change the font-family from Google Fonts in my Bootstrap 5 project, but it's not working as expected. Here's a snippet of my code: HTML <div class="row"> <div class="col-lg-6 col-md-12" ...

When an HTML email is sent from the Outlook 2007 Exchange server, it may appear as plain text when viewed on

After extensive searching, I've come across two old threads with unanswered questions on this topic. In our office, we have a Windows XP PC designated for "production" that sends out emails in HTML format. These emails contain multiple excel Cells for ...

Struggling with applying mixins

Is it possible to select only the top border using this mixin? Currently, I only need a top border but would like to have the option to use others in the future. Using separate mixins for each side of the border seems inefficient. .bordered(@top-width: 1 ...