Centering a Div Vertically with Bootstrap Grid System

I'm currently grappling with a template, endeavoring to arrange the elements according to these stipulations:

  • The header needs to adhere to the top, centered horizontally, with adequate spacing between the H3 and the grouping of <nav> links.
  • The <main> should be aligned both horizontally and vertically, with text alignment set to the right.

I've been tackling this for 3 hours without success. Below is the template I'm working with:

  <div>
    <header className='fixed-top d-flex justify-content-around'>
      <div className="">
        <h3 className="float-md-start mb-0">Ethan Leyden</h3>
        <nav className="nav nav-masthead justify-content-center float-md-end">
          <a className="nav-link fw-bold py-1 px-0 active" aria-current="page" href="#">Home</a>
          <a className="nav-link fw-bold py-1 px-0" href="#">Features</a>
          <a className="nav-link fw-bold py-1 px-0" href="#">Contact</a>
        </nav>
      </div>
    </header>

    <div className="mx-auto my-auto">
      <main className="">
        <h1>Welcome to the site</h1>
        <p className="lead">It's still a work in progress here. Any tips on centering content vertically using Bootstrap? I'm stumped.</p>
        <p className="lead">
          <a href="#" className="btn btn-lg btn-light fw-bold border-white bg-white">Learn more</a>
        </p>
      </main>
    </div>
  </div>

I've experimented with different combinations of mx-auto, my-auto, align-items-center, d-flex, but haven't found the right solution yet. It may seem like a simple question, but I can't seem to locate a satisfactory answer tailored to my specific scenario (or one that actually works).

Answer №1

To achieve both horizontal and vertical centering, utilize d-flex for the first point and flex-grow-1.

<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap demo</title>
    <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ea8885859e999e988b9aaadfc4d9c4d9">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>

<body>
    <div class="flex flex-column vh-100">
    <header class='fixed-top d-flex justify-content-around'>
        <div class="d-flex align-items-center gap-5 justify-content-center">
            <h3 class="float-md-start mb-0">Ethan Leyden</h3>
            <nav class="nav nav-masthead justify-content-center float-md-end">
                <a class="nav-link fw-bold py-1 px-0 active" aria-current="page" href="#">Home</a>
                <a class="nav-link fw-bold py-1 px-0" href="#">Features</a>
                <a class="nav-link fw-bold py-1 px-0" href="#">Contact</a>
            </nav>
        </div>
    </header>
<div class="mx-auto flex-grow-1 d-flex justify-content-center align-items-center h-100 mt-5">
    <main class="">
        <h1>Welcome to the site</h1>
        <p class="lead">It's definitely still under construction. Do you know how to vertically center
            content with Bootstrap? I sure don't</p>
        <p class="lead">
            <a href="#" class="btn btn-lg btn-light fw-bold border-white bg-white">Learn more</a>
        </p>
    </main>
</div>
    <div class="mx-auto my-auto">
     
    </div></div>

    <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="23414c4c57505751425363160d100d10">[email protected]</a>/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
        crossorigin="anonymous"></script>
</body>

</html>

Answer №2

<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap demo</title>
    <link href="https://cdn.jsdelivr.net/npm/example/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>

<body>
    <div class="flex flex-column vh-100">
    <header class='fixed-top d-flex justify-content-around'>
        <div class="d-flex align-items-center gap-5 justify-content-center">
            <h3 class="float-md-start mb-0">Ethan Leyden</h3>
            <nav class="nav nav-masthead justify-content-center float-md-end">
                <a class="nav-link fw-bold py-1 px-0 active" aria-current="page" href="#">Home</a>
                <a class="nav-link fw-bold py-1 px-0" href="#">Features</a>
                <a class="nav-link fw-bold py-1 px-0" href="#">Contact</a>
            </nav>
        </div>
    </header>
<div class="mx-auto flex-grow-1 d-flex justify-content-center align-items-center h-100 mt-5">
    <main class="">
        <h1>Welcome to the site</h1>
        <p class="lead">It's definitely still under construction. Do you know how to vertically center
            content with Bootstrap? I sure don't</p>
        <p class="lead">
            <a href="#" class="btn btn-lg btn-light fw-bold border-white bg-white">Learn more</a>
        </p>
    </main>
</div>
    <div class="mx-auto my-auto">
     
    </div></div>

    <script src="https://cdn.jsdelivr.net/npm/demo/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
        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

What is the best way to obtain a custom font for individuals without access to it?

Similar Question: How can I incorporate a non-standard font into my website? I have received a .ttf file from a client with the request that it should be used as the font for their website, and also for users to download this font if they do not alrea ...

Unable to determine the appropriate signature for calling the date function

<td> {{ fundInfo.startDate | date: "yyyy-MM-dd" }} </td> The issue is located in the primeng-test-b.component.html file under src\app\primeng-test-b directory. ...

Troubleshooting EasyTabs: Localhost Issue with Ajax Tab Configurations

I've implemented EasyTabs for organizing my tabs on a webpage. I decided to use ajax-tabs functionality in order to fetch content from other pages when users click on the navigation menu buttons. However, I've encountered an issue where the conte ...

The mouse pointer adjusts according to the event

I am working on an ajax request and I have implemented a feature where the cursor changes appearance. document.body.style.cursor = "wait"; This immediately shows the cursor as a spinning circle when the request starts. At the end of the request, I ch ...

What are some top-notch 960.gs extensions for Firefox?

Currently, I am utilizing the 960.gs grid system for my website design work. Unfortunately, I have not been able to come across a high-quality 960.gs Firefox plugin. Can anyone recommend any good Firefox plugins that support 960.gs for web layout design? ...

When the parent element is already set to justify its content with flexbox, you can ensure equal heights for CSS classes by utilizing

Within my design, I have several rows with 3 columns each. Every column contains a main content section labeled SavedSearchBox-content (with various permutations that affect its height) and a footer labeled SavedSearchBox-footer. To structure this layout, ...

padding is increasing the overall size of the div element

I have been working with the following code: <div class="main"> <div class="babyOne"> </div> <div class="babyTwo"> </div> </div> .main{ width:100%; position:relative; } .babyOne,.badyTwo{ width:50%; float:left; } Ever ...

What is the process for retrieving data from multiple tables within a database using Node.js, MySQL, and Express.js?

Can someone help me pinpoint the issue? I am trying to retrieve data from different MySQL tables using separate queries, specifically in gestionEntreprise.ejs: <tbody> <tr> <% data1.forEach(entry => { %> &l ...

Looking for a reliable CSS aggregator for .Net that can merge and minimize style sheets effectively?

Is there an open source or free project that offers a CSS manager? I want to optimize performance and am hoping to find a pre-built solution rather than starting from scratch. Some key features I'm looking for include: Merging multiple .css files in ...

Error Message Border Exceeds Width Limit and Spans Over Two Lines in Regular Expression

While working on creating a lengthy RegEx error message using server controls, I had the idea to add some style by giving the message a background color and border. However, when I tried to add a CSS class to the server control with a border and color, th ...

Creating a string of DIV elements with PHP and mySQL - is it possible?

I am in the process of creating a well-formatted product list using data from an SQL database. The goal is to have my website display a store layout with small boxes containing brief information about each product that, when clicked, will open up detailed ...

The animation isn't loading

I discovered a handy script that displays a waiting message when my form is submitted. I implemented jquery-ui to showcase this message in a dialog box, and it was successful. However, upon customizing the text and adding an animated gif background using C ...

Is this code in line with commonly accepted norms and standards in Javascript and HTML?

Check out this Javascript Quiz script I created: /* Jane Doe. 2022. */ var Questions = [ { Question: "What is 5+2?", Values: ["7", "9", "10", "6"], Answer: 1 }, { Question: "What is the square root of 16?", Values: ["7", "5", "4", "1"], Answer: ...

Struggling to find a way to showcase API data on a HTML site

Is there a way to dynamically show the live prices of crypto currencies on my website? I wrote a script that successfully displays the current price, but I'm struggling with implementing auto-refresh using setInterval. Here's the code I have so f ...

The functionality of two-way data binding seems to be failing when it comes to interacting with Knock

I am currently working on a piece of code that consists of two main functions. When 'Add more' is clicked, a new value is added to the observable array and a new text box is displayed on the UI. Upon clicking Save, the values in the text boxes ...

How to use Jquery to dynamically alter cell backgrounds in a table based on their values?

I successfully imported the table from MySQL to HTML, as shown below: https://i.sstatic.net/kzJtF.png My script is designed to highlight the two lowest and two highest values in each column: $(document).ready(function(){ var $table = $("#tbTodos"); ...

Can you provide suggestions for implementing constant retry attempts for a JSoup request?

I'm currently facing an issue using JSoup to retrieve data and save it to a file. Despite including the agent header, I sometimes encounter a 404 error during requests. Is there a way for me to continuously retry the request until it is successful? A ...

Currently, I am compiling a list of tasks that need to be completed, but I am encountering a dilemma that is proving difficult to resolve

Recently delved into the world of Javascript and encountered a roadblock that I can't seem to overcome. Here's the snippet of code causing me trouble: add = document.getElementById("add"); add.addEventListener("click", () => { console.log("Ple ...

Tips on eliminating the blue color from a hyperlink within a button

Currently, I am in the process of designing an HTML email template. I have included a green button in the template and now need to eliminate the blue color from the hyperlink within the button. I have exhaustively tried different solutions such as using ! ...

Looking to set the "min" attribute for an input type="date" in HTML5 with a specific date format preselected?

My upcoming date has a specific format as detailed below. Date_received = Tue Apr 05 2016 00:00:00 GMT+0530 (India Standard Time) To set the 'min' attribute for the HTML5 input type="date" datepicker, I need to initialize it using the following ...