What is the best way to center align a card using Bootstrap 4?

<div class="container">
    <div class="row align-items-center h-100">
        <div class="col-6 mx-auto">
            <div class="card bg-white mx-auto" id="maincard">
                <div class="card-body">
                    <h3 class="card-title text-center">Find a Schedule</h3>
                    <h6 class="card-subtitle mb-2 text-muted text-center">Choose the category and fill in the search field.</h6>
                    <hr>
                    <div class="alert alert-danger alert-dismissible fade show" role="alert" style="display: {{none if not show_alert else block}}">
                        {{alert_message}}
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <form method="post" action="">
                        <div class="form-group">
                            <label for="category">Category:</label>
                            <select class="form-control" id="category" name="type" onchange="modifyText()">
                                <option value="classes">Classes</option>
                                <option value="teachers">Teachers</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="user_input">Search:</label>
                            <input type="text" class="form-control" name="user_input" placeholder="E.g. 2DM" autofocus id="user_input" list="classes" autocomplete="off">
                        </div>
                        <br>
                        <div class="container">
                            <div class="row">
                                <div class="col text-center">
                                    <button type="submit" class="btn btn-secondary" style="width: 120px;">Search</button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

I'm having trouble centering a card vertically using Bootstrap 4, it's not aligned as expected:

https://i.sstatic.net/5BiKw.png

The card doesn't appear centered on the page. I've tried searching online for a solution without success. I attempted to follow this guide but it didn't work.

Any help would be appreciated. Thank you.

Answer №1

According to the information in the link you provided, it is stated that "If the parent of the element you're trying to center has no defined height, none of the vertical centering solutions will work!"

To resolve this issue, you simply need to add style='height:100vh' to the container div... as illustrated below:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">

<div class="container" style='height:100vh'>
  <div class="row align-items-center h-100">
    <div class="col-6 mx-auto">
      <div class="card bg-white mx-auto" id="maincard">
        <div class="card-body">
          <h3 class="card-title text-center">Search for a time</h3>
          <h6 class="card-subtitle mb-2 text-muted text-center">Choose the category and fill in the search field.</h6>
          <hr>
          <div class="alert alert-danger alert-dismissible fade show" role="alert" style="display: {{none if not show_alert else block}}">
            {{alert_message}}
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
          </div>
          <form method="post" action="">
            <div class="form-group">
              <label for="category">Category:</label>
              <select class="form-control" id="category" name="type" onchange="modifyText()">
                <option value="classes">Classes</option>
                <option value="teachers">Teachers</option>
              </select>
            </div>
            <div class="form-group">
              <label for="user_input">Search:</label>
              <input type="text" class="form-control" name="user_input" placeholder="E.g. 2DM" autofocus id="user_input" list="classes" autocomplete="off">
            </div>
            <br>
            <div class="container">
              <div class="row">
                <div class="col text-center">
                  <button type="submit" class="btn btn-secondary" style="width: 120px;">Search</button>
                </div>
              </div>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>

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

capturing webpage content with javascript for use in a screenshot

Is there a way to capture a screenshot of a webpage using JavaScript and utilize the canvas tag for this purpose? I attempted to use the html2canvas plugin in the past, but found it lacking in power. I would like to achieve this without relying on extern ...

The alignment of the Unicode character is off and is not centered within the button

Upon implementing a unicode character as a button, I've observed that the alignment of the character is not centered properly (both horizontally and vertically) within the button. It's puzzling why this misalignment occurs even after setting padd ...

Ways to update HTML content generated by Ajax

Having some difficulties modifying the HTML content generated by AJAX. This is the code snippet from the page: @model IEnumerable<WE_SRW_PeerNissen.Models.Reservations> @{ ViewBag.Title = "List"; Layout = "~/Views/Shared/_Layout.cshtml"; } ...

Why does Node.js prompt an error when using document.querySelector? Is there an alternative method to document when utilizing express in my project?

An issue arose when attempting to select the form element using const weatherForm = document.querySelector('form') ^ Unfortunately, a ReferenceError was encountered: document is not defined at Object. (/Users/sheebadola/development/quadacademy/w ...

Hover over a child DIV element to dynamically alter the style of its parent DIV element

Attempting to create a simple CSS hover effect. <div class="menu"> <div class="child"></div> </div> <div class="nav"> <div class="item1"></div> </div> The desired outcome is for the style of < ...

objects continue to escape the confines of the container

While working on this dashboard design, I've encountered an issue where the content items jump out of their designated container. I'm using Bootstrap and have tried various solutions like setting the class to container-fluid, changing tags from & ...

Having trouble sending data from the controller to the view in Laravel 8

I am struggling to display data retrieved from the database in my view through the controller. Despite trying various solutions found on similar questions, none of them seem to be effective. My main goal is to showcase a list of employees on my admin page. ...

Does CSS positioning change depending on the screen size?

I need help fixing the positioning of my logo on the screen. I want it to be 40px from the top and centered horizontally regardless of the screen size. Here is the code that I have: <html> <head> <style type="text/css> body{ back ...

Creating a dropdown menu with an initial value fetched from a text file

I'm currently working on a school project that involves adjusting basic wireless parameters in a Linux router using command-line operations through an HTML interface. My experience has mostly been with PHP, and so far, I've been progressing well. ...

Using a local function within Node.js and referencing it with the <%%> tag in a document

I am currently working with Node.js and attempting to utilize a local function within a document that requires the JSON I send in the res.render. Is there a method to achieve this? Below is an example of how I attempted to implement it: <%local_vari ...

Determine the amount of unused vertical space within a block of text

Having applied CSS to a span element: font-height = 120px; height = 120px; line-height = 120px; The text inside the span does not completely fill the height of 120px. Is there a method to determine the offset of the text from the top and bottom boundar ...

Looking to turn off animation for a WordPress theme element?

Is there a code that I can insert into the style.css file to deactivate the theme animations on my WordPress page? As users scroll down, each element either drops in or slides in. I recently purchased the WP Alchemy theme from , but the animation speed is ...

Vue: update data and trigger function upon completion of animation transformation, utilizing animation transformation completion listener

Check out this straightforward Vue example: https://codesandbox.io/s/sleepy-haze-cstnc2?file=/src/App.vue https://i.stack.imgur.com/zboCb.png Whenever I click on the square, it not only changes color but also expands to 200% of its original size with a 3 ...

What is the method for obtaining receipt numbers in sequential order, such as the format AB0810001?

Is AB the receipt code that should remain constant followed by the current date (08) and 10001 as the receipt number? ...

Conceal the scroll bar while the page preloader is active

Is there a way to hide the scroll bar while the preloader is loading on a webpage? I want to prevent users from scrolling until the preloader disappears. I have tried using CSS and setting the body overflow to hidden, but it's not working as expected. ...

Extracting JSON data in PHP and presenting it in a tabular format

Below is a snippet of my HTML code, <html> <head> <script src="js/jquery.js"></script> </head> <body> <input type="text" name="query" id="queryBox"> <button id="load_basic" value = "button">search</but ...

Having trouble with uploading images?

I'm having issues with using PHP to upload an image to a folder. Can someone please help me troubleshoot this? Thank you! Here is the code for setup.php <?php include("../includes/config.php"); ?> <?php if ($_SESSION["isadmin"]) { ...

Server time dictates the operation of Moment.js

I've been working with Moment.js and it's functioning correctly, but I can't seem to figure out how to make it run on server time instead of local time. I've tried a few things, but haven't had any luck. I'm unsure of how to ...

The alignment of Bootstrap input fields is off center

@import url( 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' ); <div class="row"> <div class="col-xs-12 text-center btn-group" data-toggle="buttons"> <label class="btn btn-primary active"> ...

Employing setInterval() without clearing previously triggered events

Can someone clarify the distinction between these two functions: function displayTime(){ var current = new Date(); var hours = current.getHours(); var minutes = current.getMinutes(); var seconds = current.getSeconds(); ...