Aligning text to the center of the second column on a page using Bootstrap

A header is designed with two columns: the first column contains the logo, while the second column holds a centered heading. Below that, the main content also features a centered heading. The challenge is to align the header heading to the center of the main parent container similar to the heading in the main content section. The layout utilizes Bootstrap 4.6.

For visual clarification, refer to the following diagram: https://i.sstatic.net/pLmxe.png

html, body {
    height: 100%;
}
.container-fluid {
    height: 100%;
    min-height: 100%;
}
.row-color {
    background-color: #DCE6F2;
}
.logo-width img{
    width: 220px;
    height: 210px;
}
.column-width img {
    width: 120px;
    height: 115px;
}
#page-container {
    height: 100%;
    display: flex;
    flex-direction: column;
}
footer {
    background-color: #024393;   
    color: #fff;             
}
<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

        <!-- Bootstrap CSS -->
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="80e2efeff4f3f4f2e1f0c0b4aeb6aeb0">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">

        <title>Title</title>
        
    </head>
    <body>
        <div id="page-container" class="container-fluid">
            <div class="row row-color align-items-center text-center p-5">
                <div class="col-2 logo-width text-center">                    
                    <img src="img/shelter_logo.svg" alt="Logo Here">                    
                </div>
                <div class="col">
                    <div class="center">
                        <h2>Heading</h2>
                        <p>text</p>
                        <h2>Heading</h2>
                        <p>Text</p>
                    </div>
                </div>
            </div><!-- end row -->
            <div class="row my-5">
                <div class="col text-center">
                    <h2>Heading</h2>
                </div>
            </div>
            <div class="row justify-content-around">
                <div class="col text-center">
                    <img src="#" alt="">
                    <h3>Heading</h3>
                </div>
                <div class="col text-center">
                    <img src="#" alt="">
                    <h3>Heading</h3>
                </div>
                <div class="col text-center">
                    <img src="#" alt="">
                    <h3>Heading</h3>
                </div>
                <div class="col text-center">
                    <img src="#" alt="">
                    <h3>Heading</h3>
                </div>
                <div class="col text-center">
                    <img src="#" alt="">
                    <h3>Heading</h3>
                </div>
                <div class="col text-center">
                    <img src="#" alt="">
                    <h3>Heading</h3>
                </div>
                <div class="col text-center">
                    <img src="#" alt="">
                    <h3>Heading</h3>                    
                </div>
            </div>
            <footer class="d-flex justify-content-center mt-auto">
                <div class="row justify-content-around p-3">
                    <div class="col text-center">                        
                        <h3>Heading</h3>
                    </div>
                    <div class="col text-center">
                        <img src="#" alt="">                        
                    </div>
                    <div class="col text-center">
                        <h3>Heading</h3>
                    </div>
                    <div class="col text-center">
                        <img src="#" alt="">
                    </div>
                    <div class="col text-center">
                        <h3>Heading</h3>
                    </div>
                    <div class="col text-center">
                        <img src="#g" alt="">
                    </div>
                    <div class="col text-center">
                        <h3>Heading</h3>                    
                    </div>
                </div>
            </footer>
        </div>



        <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="05676a6a71767177647545312b332b35">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-Piv4xVNRyMGpqkS2by6br4gNJ7DXjqk09RmUpJ8jgGtD7zP9yug3goQfGII0yAns" crossorigin="anonymous"></script>
    </body>
</html>

Take a look at this codepen for a live demo: https://codepen.io/west4me/pen/vYywWVX

Answer №1

.col will expand to fit the row. In this scenario, since you have a .col-2 in your heading row, the .col is essentially equivalent to a .col-10.

To center it, you must be positioned in the middle of the 12-column Bootstrap grid, which requires 2 columns on each side.

As previously mentioned by Rick, you should substitute your .col with a .col-8

html, body {
    height: 100%;
}
.container-fluid {
    height: 100%;
    min-height: 100%;
}
.row-color {
    background-color: #DCE6F2;
}
.logo-width img{
    width: 220px;
    height: 210px;
}
.column-width img {
    width: 120px;
    height: 115px;
}
#page-container {
    height: 100%;
    display: flex;
    flex-direction: column;
}
footer {
    background-color: #024393;   
    color: #fff;             
}
<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

        <!-- Bootstrap CSS -->
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ff9d90908b8c8b8d9e8fbfcbd1c9d1cf">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">

        <title>Title</title>
        
    </head>
    <body>
        <div id="page-container" class="container-fluid">
            <div class="row row-color align-items-center text-center p-5">
                <div class="col-2 logo-width text-center">                    
                    <img src="img/shelter_logo.svg" alt="Logo Here">                    
                </div>
                <div class="col-8">
                    <div class="center">
                        <h2>Heading</h2>
                        <p>text</p>
                        <h2>Heading</h2>
                        <p>Text</p>
                    </div>
                </div>
            </div><!-- end row -->
            <div class="row my-5">
                <div class="col text-center">
                    <h2>Heading</h2>
                </div>
            </div>
            <div class="row justify-content-around">
                <div class="col text-center">
                    <img src="#" alt="">
                    <h3>Heading</h3>
                </div>
                <div class="col text-center">
                    <img src="#" alt="">
                    <h3>Heading</h3>
                </div>
                <div class="col text-center">
                    <img src="#" alt="">
                    <h3>Heading</h3>
                </div>
                <div class="col text-center">
                    <img src="#" alt="">
                    <h3>Heading</h3>
                </div>
                <div class="col text-center">
                    <img src="#" alt="">
                    <h3>Heading</h3>
                </div>
                <div class="col text-center">
                    <img src="#" alt="">
                    <h3>Heading</h3>                    
                </div>
            </div>
            <footer class="d-flex justify-content-center mt-auto">
                <div class="row justify-content-around p-3">
                    <div class="col text-center">                        
                        <h3>Heading</h3>
                    </div>
                    <div class="col text-center">
                        <img src="#" alt="">                        
                    </div>
                    <div class="col text-center">
                        <h3>Heading</h3>
                    </div>
                    <div class="col text-center">
                        <img src="#" alt="">
                    </div>
                    <div class="col text-center">
                        <h3>Heading</h3>
                    </div>
                    <div class="col text-center">
                        <img src="#g" alt="">
                    </div>
                    <div class="col text-center">
                        <h3>Heading</h3>                    
                    </div>
                </div>
            </footer>
        </div>



        <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="63010c0c17101711021323574d554d53">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-Piv4xVNRyMGpqkS2by6br4gNJ7DXjqk09RmUpJ8jgGtD7zP9yug3goQfGII0yAns" crossorigin="anonymous"></script>
    </body>
</html>

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

Creating a CSS layout that spans a single element across multiple divs

As a newcomer to the world of Html & CSS, I have a seemingly simple question that has been eluding me: How can you make an html element(child div, text, etc.) span across multiple divs using only CSS & Html(with no Javascript/JS)? Currently, I am working o ...

Guide on implementing CSS3 parser with HtmlUnitDriver

As an example, let's consider a scenario where we have a selector to target the active menu item: $("ul#menu li a[href='/']") And a selector to target the remaining menu items (1): $("ul#menu li a:not([href='/'])") However, the ...

Retrieve the selected value from a radio button

I am attempting to display the chosen value in an input field when a radio button is selected. However, the functionality seems to be broken when I include bootstrap.min.js. You can find the code on JSFiddle. $('input:radio').click(function() ...

Grouping and styling table data cells within a row: best practices

I am attempting to "group" a collection of cells/<td> elements within a row/<tr> in a <table> in order to apply styles to them. The code below accomplishes what I desire to some extent, but it does have its limitations: /* Issues wit ...

angular-ui-tab-scroll: Odd spacing between blocks and tabs, each separated individually

Greetings! I would like to express my gratitude for this wonderful library! However, I am encountering an unusual issue when trying to wrap a tabset with tabs that are included separately. This can be done either by adding individual tab elements manually ...

The CSS property for restricting white-space to nowrap is not functioning as

Having a div with multiple child divs floating left can be tricky. To prevent them from breaking, setting them to display:inline-block and white-space:nowrap seems like the solution. However, in some cases this may not work as expected. If your goal is to ...

Trouble with Z-index on FireFox when using absolute positioning

After reviewing some other questions, it seems that the z-index issue in Firefox is often related to one of the items not being set as position:Absolute. <div id="AnimatedBanner" style="right:-5px;"> <object style="positio ...

AMP causing distortion in images

My images are stretching on this page and I cannot figure out the cause. Any help would be greatly appreciated. The issue seemed to arise after I implemented Bootstrap, but I am using a customized version of Bootstrap that only includes grid system and fo ...

Execute a jQuery function every time the class of the parent container div changes

I am seeking to trigger the function below whenever its containing div parent <div class="section">...</div> transitions to an "active" state, for example: <div class="section active">...</div> $(".skills-grid__col").each(function ...

Utilizing exclusively CSS for parent div, achieve dynamic resizing animation

I want to create a transition effect on the parent div when the child div disappears without using display:none My goal is to have a dynamically centered div that animates using CSS transitions only. Here is my current approach. document.querySelector ...

A miniature triangle-shaped bubble crafted with 1px of CSS3 styling

Currently, I am experimenting with CSS3 and creating bubble shapes. However, I have encountered an issue. I am trying to create a triangle with a 1px border, but the result is a thicker triangle than desired. You can view my code on this fiddle : FIDDLE ...

Expanding HTML zones to reach the bottom of the screen, while implementing top and side menus using the latest HTML5 and CSS techniques

Struggling to design a page layout with two navigation sections (top and left) similar to the example below: The light gray area represents where the main content will be displayed on each page. On the left side is the secondary navigation menu that shoul ...

Positioning an Image Within the Center Using Bootstrap

I recently modified some code to create a transparent background for my logo on top of a parallax image. However, after making these changes, I noticed that the logo no longer appears in the center of the screen. Can someone help me identify what could be ...

ReactJs - Reactstrap | Issue with Jumbotron displaying background color

I am trying to create a gray background using Jumbotron in reactstrap. After installation and reference in required places - index.js import 'bootstrap/dist/css/bootstrap.css'; Jumbotron has been implemented in SignIn.js - import Re ...

Retrieve the object when hovering over an element in the grid

In a collection of 16 items belonging to the same class, I am looking for a way to implement a mouseover effect that changes the opacity of only the specific item being hovered over and not all items in the class. Check out this link for an example ...

Aligning text to the left center

I am looking to center left-aligned text in the yellow box (txtbox). The current look is like this: and I want it to appear like this: HTML: <div class="content container small"> <div class="txtbox"> ...

I am experiencing difficulty with implementing a page break within the <tr> and <tbody> elements

Below is the code I am working with: {% for item in child.items %} {%set ns.count = ns.count + 1%} <tbody style="page-break-inside: avoid"> <tr style="page-break-inside: avoid"> ...

Image in an Outlook email surrounded by padding and enclosed in a paragraph

I am facing an issue with my email signature setup in Outlook Live where there is a white space below the image. After trying to add vertical-align:bottom to the CSS, it seems that Outlook Live does not accept it. The code for my HTML in Outlook 20xx is a ...

Shade the tag when the checkbox is marked

Is it possible to change the color of a label without using javascript when the associated checkbox is checked? ...

Expanding a bootstrap accordion not only increases its own height, but also affects the adjacent one

I am facing an issue with my FAQ page where I have used bootstrap accordion tabs. The problem arises when I place them side by side in a 50/50 split, as when one tab is opened, the adjacent tab also expands without displaying its content. For example, thi ...