Ways to align a bootstrap 4 row both vertically and horizontally in the center amidst other rows

Assume there is a main menu followed by three rows. The first row should be at the top and the third at the bottom, while the second row needs to be centered both vertically and horizontally.
As you scroll down, the main menu should be able to hide while ensuring everything else fits on the page.

I've explored this, I'm familiar with justify-content-center, and I experimented with this. However, I am struggling to bring it all together.
The second row is centered vertically but it increases in height, causing it to lose proper horizontal alignment.
Here's what I have attempted so far:

.vertical-center {
        min-height: 100%; 
        min-height: 100vh; 
        display: flex;
        align-items: center;
      }

      html, body, {
        height: 100%;
      }

      .header {
        float: left;
        width: 100%;
      }

      .full {
        height: 100%;
      }
<!DOCTYPE html>
<html>
   <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title>JS Bin</title>
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
   </head>
   <body>
      <div id="nav">
         <div class="container-fluid navbar-main">
            <nav class="navbar navbar-expand-md no-padding navbar-main">
               <div class="container">
                  <a class="navbar-brand" href="#">Main menu</a>
                  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
                     aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Menü aufklappen">
                  <span class="navbar-toggler-icon"></span>
                  </button>
                  <div class="collapse navbar-collapse"
                     id="navbarSupportedContent">
                     <ul class="navbar-nav mr-auto">
                        <li class="nav-item">
                           <a class="nav-link" href="/" role="button">Start</a>
                        </li>
                     </ul>
                  </div>
               </div>
            </nav>
         </div>
      </div>
      <div class="container">
         <div class="row">
            <div class="col header">
               <span id="score">first row </span>
            </div>
         </div>
         <div class="row justify-content-center vertical-center" style="height:100%">
            <!-- div to center -->
            <div class="col-auto">
               <span id="operand1">text</span>
            </div>
            <div class="col-auto">
               <span id="operator">text</span>
            </div>
            <div class="col-auto">
               <span id="operand2">text</span>
            </div>
            <div class="col-4">
               <span id="operand2">text</span>
            </div>
         </div>
      </div>
   </body>
</html>

Answer №1

It is recommended to utilize the "col-auto" class for all divs within the second row. Take a look at this example

.vertical-center {
  min-height: 100%; 
  min-height: 100vh; 
  display: flex;
  align-items: center;
}

html, body, {
  height: 100%;
}

.header {
  float: left;
  width: 100%;
}

.full {
  height: 100%;
}
<!DOCTYPE html>
<html>
   <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title>JS Bin</title>
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
   </head>
   <body>
      <div id="nav">
         <div class="container-fluid navbar-main">
            <nav class="navbar navbar-expand-md no-padding navbar-main">
               <div class="container">
                  <a class="navbar-brand" href="#">Main menu</a>
                  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
                     aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Menü aufklappen">
                  <span class="navbar-toggler-icon"></span>
                  </button>
                  <div class="collapse navbar-collapse"
                     id="navbarSupportedContent">
                     <ul class="navbar-nav mr-auto">
                        <li class="nav-item">
                           <a class="nav-link" href="/" role="button">Start</a>
                        </li>
                     </ul>
                  </div>
               </div>
            </nav>
         </div>
      </div>
      <div class="container">
         <div class="row">
            <div class="col header">
               <span id="score">first row </span>
            </div>
         </div>
         <div class="row justify-content-center vertical-center">
            <!-- div to center -->
            <div class="col-auto">
               <span id="operand1">text</span>
            </div>
            <div class="col-auto">
               <span id="operator">text</span>
            </div>
            <div class="col-auto">
               <span id="operand2">text</span>
            </div>
            <div class="col-auto">
               <span id="operand2">text</span>
            </div>
         </div>
      </div>
   </body>
</html>

Answer №2

To enclose all the contents within that row, place them in a div element with a specified class named centered. For instance, apply position relative to the main row and add the following CSS class:

.centered{position:absolute; top:50%; left:50%;}

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

Insert a new row with a designated row class

I have a table with 2 columns and an "add row" button. However, I want to add a new row between the rows with the classes "title" and "ongkir". Currently, the new row is being added after the "ongkir" row. Can someone please assist me in achieving this? B ...

The CDN version of Font Awesome is not displaying the necessary icon

Struggling to incorporate Awesome icons into my Laravel/Vue project, the icon refuses to display correctly no matter what I try. As a last resort, I attempted the most basic approach with Awesome: a straightforward HTML page using a CDN. Here is the code: ...

What is the best way to center an image and text vertically using bootstrap?

I'm currently working on a Bootstrap website and have reached a section where I want to display text on the left and an image on the right. While I've managed to achieve this layout, I'm struggling with vertically centering the image. Despit ...

The function $.ajax may not be available, but rest assured that the jquery library is functioning properly

Upon checking the console, I noticed the following: Here is the AJAX code snippet in use: $("#accept").click(function(){ id = $("#idInput").val(); password = $("#passwordInput").val(); alert(id) alert(password) $.aja ...

Is it possible to create a cross-sectional view of a lens using the HTML5 canvas element?

I am interested in creating a visual representation of the cross section of a lens element. Typically, these elements consist of one or two circular surfaces (front and back) with a rim of arbitrary shape. My goal is to simply connect the front and back su ...

Issue with IE 11: Including card image inside anchor tags in Bootstrap 4 causes unwanted white space

I'm having an issue with my Bootstrap 4 card markup where the image is wrapped inside an anchor tag. The problem arises when viewing it in IE 11, as there is a large white space underneath the image, while everything displays fine in Chrome, Edge, and ...

Tips for displaying the contents of a URL within a div element

Is there a way to load a new page into a <div></div> without opening a separate tab? I tried using an iframe, but that method is not recommended and it includes scroll bars. <div id="iframeLogin"><a href="first.php" target="_self"> ...

Encountering a connection issue: SMTP connect() failure when using PHPmailer with reCaptcha on a CentOS server

My company is looking to set up a website on a CentOS server that includes a contact form with captcha. I came across PHPMailer and got the code working on my local machine, but when trying to send emails on CentOS, I encountered an error saying "SMTP conn ...

Looking to create applications for Android and iOS that can integrate with a module designed in Adobe Captivate

I have created an interactive e-learning module using Adobe Captivate, which has been published in both HTML5 and SWF formats. The module includes some interactive elements/actions that can be accessed by clicking on them. It works perfectly fine in a web ...

PHP code to send an HTML template via email:

When attempting to send HTML content in an email using my PHP mail code, I encountered an issue where the HTML template was not being sent along with the message. $to = $email;; $subject = 'New Order Detail&apo ...

Ways to center text vertically within a cell in a Bootstrap 5 table

I am trying to achieve vertical alignment in one of the cells of a table I created. According to the Bootstrap 5 documentation, the vertical-alignment utilities only affect certain elements like inline, inline-block, inline-table, and table cell elements. ...

The sub-menu options are constantly shifting... How can I keep them in place?

Here we go again... I'm having trouble with my sub-menu elements moving when I hover over them. I've searched for a solution but haven't found anything helpful. The previous advice I received about matching padding and adding transparent bo ...

Removing data with the click of a button

I have successfully implemented a feature where clicking the "add to my stay" button displays the name and price data. Subsequently, it automatically changes to a remove button when clicked again for another addon. If I press the remove button of the first ...

When sorting items, the page automatically jumps to the top of the screen

I have been working on creating a sortable portfolio using jQuery. One issue I am facing is that every time I click on a filter for the portfolio items, the page automatically scrolls to the top. Is there a way to prevent this from happening? You can vi ...

What is the best way to control the overflow length and display only a specific amount of content at once?

I am currently designing the homepage for a social media platform. I have around 45 dummy posts and I am looking for a way to limit the overflow in CSS so that only 5 posts are displayed at a time. Once the user reaches the bottom of those 5 posts, another ...

What is the best way to place this dropdown in a fixed position within a parent element with overflow scrolling

I am facing an issue with a dropdown menu inside a parent div. The parent div has a fixed height and is set to overflow: auto. My goal is to have the menu extend beyond the boundaries of the parent when opened, but currently it just increases the height of ...

Unable to retrieve the following element in a JavaScript array

I am a beginner in JavaScript and I am attempting to access the next element of an array using an onclick function but so far I have not been successful. var i, len; function quiz() { var quiz_questions = [ "who is the founder of Fa ...

Utilize a external API to fetch JSON data from the website and showcase it on a webpage in a asynchronous manner

<!DOCTYPE html> <html> <head> <script> src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script> </head> <body> <input type="text" placeholder="Enter Your Zip Code" id="zipCode ...

Building a Compact Dropdown Menu in Bootstrap

I am looking to implement a compact Bootstrap dropdown feature, but I am unsure of how to do it. Take a look at this image for reference. base.html: <a class="text-light" data-bs-toggle="dropdown" aria-expanded="false&qu ...

Exploring various viewport dimensions using Django and Selenium

I am currently experimenting with testing the characteristics of specific UI elements at various viewport sizes and media query breakpoints defined in CSS. Initially, I have a setup function that initializes a headless Chrome browser with what I believe is ...