How to vertically center a card between a search bar and the bottom of the viewport using Bootstrap?

I'm struggling to center a card vertically between the search bar and the bottom of the viewport.

Even after setting the body height to 100%, the content extends beyond the viewport, leaving white space that users can scroll into. I've attempted using align-content: center; on the parent container, but it doesn't seem to make any difference.

html.html


<body>
    <div class="navbar navbar-expand-lg sticky-top">
        <!-- Navigation information -->
    </div>

    <div class="container-fluid">

        <!-- Title above the search bar -->
        <div class="row">
            <div id="search-title" class="col-xl-6 col-md-7 col-sm-8 col-8 mx-auto">
                <h3>Title</h3>
            </div>
        </div>

        <!-- Search bar -->
        <div class="row">
            <div class="col-xl-6 col-md-7 col-sm-8 col-8 mx-auto">
                <!-- Search form -->
                </form>
            </div>
        </div>

        <!-- No results message -->
        <div class="row">
            <div id="no-results" class="col-xl-6 col-md-7 col-sm-8 col-8 mx-auto">

                {% if term != '' and not cameras %}
                <!-- Display no results message -->
                {% endif %}

            </div>
        </div>

        <!-- Card resulting from the search -->
        {% if camera %}
        <div class="container align-center">
            <!-- The goal is to vertically center this card between the search bar and the bottom of the viewport -->
            <div class="col-xl-6 col-md-7 col-sm-8 col-8 mx-auto card myclass">
                <div class="card-body">
                    <!-- Content details -->
                </div>
            </div>
        </div>
        {% endif %}
    </div>
</body>

css

html, body {
    height: 100%;
    width: 100%;
}

.align-center {
    align-content: center;
}

This is the current appearance. You can see that the border at the bottom surpasses the viewport.

https://i.sstatic.net/ghOk8.png

Answer №1

To create a responsive card layout, wrap your card element in a container called "card-container" with the position property set to relative. Apply absolute positioning to your card and set the top property to 50%. Ensure that your container has a specified height.

Additionally, utilize the flexbox feature of CSS to enhance responsiveness and address issues on larger screens. Update the code within the container and adjust the CSS classes accordingly to improve the layout. Make these changes and let me know if you encounter any challenges.

.card-container {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  border: 2px solid red;
}


.card-body {
  flex: 2 0;
  align-self: center;
  border: 2px solid green;
}
.card {
  flex: 1 1;
  align-self: center;
  
}


.good-status {
  color: #45A163;
}

.bad-status {
  color: red;
}
  <div class="card-container">
    <div class="card mx-auto"></div>
    <div class="card-body">
      <h5 class="card-title text-center">Title</h5>
      <ul>
        <li>Item 1</li>
        <li>Item 1</li>
        <li>Item 1</li>
        <li>Item 1</li>
      </ul>
      <a href="#" class="btn btn-primary">Ping device</a>
      <a href="#" class="btn btn-primary">Go to webbooter page</a>
    </div>
       <div class="card mx-auto"></div>
  </div>
</div>

Answer №2

  1. Adjust your page height by using height:100vh to fill the page completely. To account for a fixed navbar, subtract its height from 100vh like this: height:calc(100vh - (56px)).
  2. To center your card on the page vertically and horizontally, utilize position:absolute along with transform:translate(-50%,-50%). This will ensure that the card is centered across all devices.

<!DOCTYPE html>
<html>

... (HTML code continues)

            </div>
          </div>
        </form>
      </div>
      <!--end of col-->
    </div>
    <div class="col-sm-6 centre">
      <div class="card bg-light ">
        <div class="card-body text-center">
          <p class="card-text">Some text inside the card</p>
        </div>
      </div>
    </div>
  </div>
</body>

</html>

Answer №3

If you want to avoid using height:100%, calc(), or any extra CSS, consider utilizing the flexbox classes provided in Bootstrap 4. By setting a minimum height on the body with min-vh-100, applying a flex direction of column, and using flex-fill for the container, you can easily make the content fill the remaining height. This will help achieve the desired vertical centering effect.

<body class="d-flex flex-column min-vh-100">
   <nav>
   </nav>
   <div class="container-fluid d-flex flex-column flex-fill align-items-center justify-content-center">
      <div class="row">...</div>
   </div>
</body>

Explore more on this topic: Bootstrap 4: How to make the row stretch remaining height?

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

Is it possible to implement a custom icon for pagination in Material UI?

Can I use a custom icon for pagination in material ui? Find more information on Material UI Pagination in the official documentation here, and check out the CodeSandbox Demo here I am interested in changing the default left and right chevron icons for na ...

Displaying the usernames of the users on the interface leads to encountering the 'Undefined Index' error

Hey there, I'm currently facing an issue with displaying the user's first name as a greeting message on index.php. I want it to say 'Hello, (user's first name)', but since I'm fairly new to PHP, I'm struggling to make it ...

Attempting to store form data in a database using PHP has proven to be unsuccessful in my efforts

I am facing an issue while trying to add HTML form data into the database using PHP. Unfortunately, I am unable to successfully insert the HTML form data into the database and I'm not sure what mistake I might be making in my code. Below is the code ...

Adjusting the border-right size based on scroll position

Apologies if this question seems trivial, but I am completely new to coding in HTML and JavaScript. I understand that I can make some changes based on scroll position, but right now I'm a little confused. I want to increase the height of a box as the ...

Utilizing Modernizr for detecting support of background-clip:text functionality

Recently, I decided to utilize Modernizr in order to check for support of the css property background-clip: text. After some research, I came across this page:https://github.com/Modernizr/Modernizr/issues/199 I then added the following code to my website ...

Using jQuery and HTML to create numerous links that direct to a solitary iframe

I created a basic overlay setup using css, html, and jQuery. Here's how it looks: <a class="activator" id="activator" style="" href="#">hello</a> <div class="overlay" id="overlay" style="display:none;"></div> <div style="d ...

What is the method for fetching the value from checkbox buttons in Bootstrap 4?

I am currently utilizing Bootstrap's button plugin for my buttons, specifically with 6 checkbox buttons. I am in need of a method to extract the value of each checked button in order to perform some calculations. However, I am struggling to find a sol ...

Is it possible to shift the image within the <canvas> element without having to redraw the entire canvas?

I created a game board and I am trying to implement drag-and-drop functionality for my pieces, which are in .gif format. However, I am struggling with finding a solution that allows me to move the pieces without constantly redrawing the entire board. Cur ...

Arrange JSON response data in alphabetical order

Looking to sharpen my skills by working with public APIs in JavaScript. My goal is to sort the list of items alphabetically by the h4 value (food name) when the query is submitted. I attempted to use .sort() on the response item before using .map(), but en ...

Is there a more efficient method to halt a jQuery animation plugin? Surely, there must be a more effective solution

After feeling under the weather for the past week and feeling incredibly bored, I decided to delve into the world of creating jQuery plugins. In just half an hour, I managed to put together a plugin that mimics the "wiggle" effect when pressing and holding ...

AngularJS: Updating data in controller but not reflecting in the HTML view

Within my AngularJS app, I've come across the following code: example.html <div> <p>{{name}}</p> </div> <div> <button ng-click="someFunction()"></button> </div> exa ...

Using jQuery to dynamically change the CSS color based on the background-color of a checkbox label

When a checkbox is selected, the function below runs to change the text color to white or black based on the darkness of the background color. However, the third checkbox should trigger this change to white but currently does not. function isDark(color) { ...

Adjustable dimensions and proportions for the following image

I'm currently working on a blog page using next.js and I've encountered an issue with displaying images. Each post has a main image of varying dimensions and aspect ratios, making it difficult to maintain consistency when scaling for smaller scre ...

unable to modify the content within a div by clicking on a link

Lately, I've been experimenting with a code snippet I found on this fiddle: http://jsfiddle.net/unbornink/LUKGt/. The goal is to change the content of a div when clicking on links to see if it will work on my website. However, no matter which link I c ...

Cannot access pseudo elements in the Microsoft Edge browser

Is there a workaround for not being able to select or access the properties of :before & :after elements on Microsoft Edge's inspect element? .arrow_box { position: relative; background: #d43959; border: 4px solid #ac1f3c; width ...

My HTML components are not showing alert messages at the top as expected

My application's alert message is supposed to stay on top of all other HTML elements as the page scrolls. However, it seems to be going behind certain components instead of staying on top like it should. This issue is puzzling because all components u ...

What is the best way to contain my content within a bordered box?

As a beginner in HTML and CSS, I have successfully created a basic website with a simple menu at the top that includes links to different pages like "Home," "About," and "Contact." The website will mainly feature information and images, but I believe it wo ...

Is there a way to retrieve JSON data from a different domain and incorporate it into my own website?

I am attempting to utilize JSON data from "" and display the data on my website. My goal is to extract the JSON field name "Stats" and retrieve the sub-field name '\"v\"'. You can refer to the documentation provided by purpleair here: h ...

Utilize the float left and float right properties within a div container with a width of 100% to ensure compatibility

I need to align two divs within a container-div in IE7. The first div should float to the left, and the second to the right. Currently, both divs are floating to the left according to my CSS configuration. Here is the CSS code I'm using: .container ...

Switch back emulation when moving away from page - using angular javascript

I have a unique scenario in my component.ts file on a specific page where I need to incorporate custom CSS for printing purposes: @Component({ selector: "dashboard", templateUrl: "./dashboard.component.html", styleUrls: ["./d ...