How to vertically center a div with Bootstrap framework in your website

I'm currently working with Bootstrap 4 and I am trying to vertically center the table div in the middle of the screen. I attempted using `align-items-center` on its parent element but it did not have the desired effect.

Below is my HTML code:

<div id="gameTable" class="d-flex h-100">
        <div class="container align-items-center text-center h-100">
            <table class="table text-center" id="table">
              <tbody>
                <tr>
                  <td class="align-middle">1</td>
                  <td class="align-middle">2</td>
                  <td class="align-middle">3</td>
                </tr>
                <tr>
                  <td class="align-middle">4</td>
                  <td class="align-middle">5</td>
                  <td class="align-middle">6</td>
                </tr>
                <tr>
                  <td class="align-middle">7</td>
                  <td class="align-middle">8</td>
                  <td class="align-middle">9</td>
                </tr>
              </tbody>
            </table>
            <button class="btn btn-danger" id="quit">Quit Game</button>
        </div>
    </div>

Furthermore, below you will find my CSS:

html, body {
    height: 100%;
}

#table {
    height: 70%;
}

#gameTable {
    height: 100%;
    display: none;
}

Could someone please assist me in identifying any potential issues within my code?

Answer №1

d-flex should always be used with align-items-center in a flexbox container. To simplify your code, consider the following:

#table {
 height:70%;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet">
<div id="gameTable" class="d-flex align-items-center vh-100">
  <div class="container text-center" id="table">
    <table class="table text-center h-100">
      <tbody>
        <tr>
          <td class="align-middle">1</td>
          <td class="align-middle">2</td>
          <td class="align-middle">3</td>
        </tr>
        <tr>
          <td class="align-middle">4</td>
          <td class="align-middle">5</td>
          <td class="align-middle">6</td>
        </tr>
        <tr>
          <td class="align-middle">7</td>
          <td class="align-middle">8</td>
          <td class="align-middle">9</td>
        </tr>
      </tbody>
    </table>
    <button class="btn btn-danger" id="quit">Quit Game</button>
  </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

Select: Exchange icon when clicked

UPDATE MENU ICON jsfiddle.net/a3MKG/83/ Can someone provide guidance on how to change the menu icon image upon clicking it? I would like the icon to switch to a different image (such as an X) once it has been clicked. Please Note: When the menu icon is c ...

Is it possible for Firebug to display CSS comments? This feature would greatly assist in utilizing SASS

Can Firebug display CSS comments? I'm wondering if it can help with debugging when using line numbers from SASS. I haven't been able to find any information on this feature, so any help would be greatly appreciated. ...

Catalog indexed in a JSON document

I'm currently facing an issue with extracting data from a JSON file and populating a list with that information. HTML : <ol id="dataT"> </ol> JavaScript : function GetData(index) { var xhttp = new XMLHttpRequest(); xhttp.onre ...

Embed a FLV video within a jQuery tooltip

My current setup includes a line of code like this: <img title="FLV Video goes here" class="tooltip" src="/images/icon-question.png" alt="questions" width="12" height="12" border="0" /> When hovering over the image, the text in the title attribute ...

Is there a way to create triangles on all four corners of an image using canvas in JavaScript?

In my code, I am currently working on drawing rectangles on the corners of an image using JavaScript. To achieve this, I first extract the image data from a canvas element. // Get image data let imgData = ctx.getImageData(0, 0, canvas.width, canvas.height) ...

Discover the steps for integrating an object into a Ext.grid.Panel using Sencha Ext Js

Currently, I am utilizing Sencha Ext Js 4 and have integrated an Ext.grid.Panel into my project. I am interested in adding another element inside the header, such as a textbox. Is this achievable? {filterable: true, header: 'Unique' /*Here i w ...

Exploring the differences between two CSS style attributes using JQuery

Can someone help me with a quick question about CSS? I need to compare two style attributes, specifically margin-left. If the margin-left is less than 500px, I don't want to make any changes. However, if it's greater than 500px, I want to add ano ...

Utilizing Various Bootstrap Versions in a Single Template with Django 3.0

I have created a template (Base.html) in my django project, using Bootstrap 4 which works perfectly on its own. Additionally, I have designed another template (Child_Base.html) using Bootstrap 3, with the intention of injecting it into Base.html. However ...

Creating divs that adapt to different screen sizes without relying on flexbox

Currently, I am in the process of developing a chat interface where the viewport height is set to 100vh. The layout consists of a header/navbar, a "main content" section, and a footer housing the input field (you can view the code here) The way I have str ...

Is there a way to fill an HTML text box with data retrieved from a session or managed on the server side using C# and HTML?

I have some textbox values on an aspx page that I pass to the next page using sessions. Now, I want to display these values in an HTML textbox. How can I achieve this? The values I have are server-side, while the HTML text box value is client-side (this is ...

Click on the link to return to the previous page on the website

As I work on customizing a Tumblr theme, I find myself facing a few challenges. One such issue is my desire to include a link on post pages that directs users back to the previous page, similar to what is shown in this image: picture1 I have attempted var ...

Using absolute positioning on elements can result in the page zooming out

While this answer may seem obvious, I have been unable to find any similar solutions online. The problem lies with my responsive navbar, which functions perfectly on larger screens. However, on mobile devices, the entire website appears zoomed out like thi ...

Adjust the background image color with CSS styling

I have a collection of icons that I would like to incorporate into my application. These icons primarily consist of glyphicons with a few others mixed in. I want the HTML structure to mimic that of a glyphicon, specifically: <a href="#"> <spa ...

The slideshow feature in Bootstrap carousel seems to be malfunctioning

I can't seem to get my Bootstrap carousel slideshow to work properly. When I run the code in my Angular app.component.html, only the first image loads with no animations or response to control buttons. I've checked all the CDNs and they are worki ...

Using Symfony2 Knp-snappy library for PDF generation, the CSS styling is not being properly imported

Attempting to create a PDF from an html.twig template, but facing some issues... Although the content is correct in the PDF, the layout is missing. It appears that the CSS files are not being imported... Bootstrap from Twitter is being used to handle the ...

Sending an HTML form to an external ASP script

I am currently attempting to create an HTML form that can submit multiple variables to an external ASP file that generates a chat window. The given example I am working with can be viewed through this link, although it involves a two-step process. In the ...

HTML5 sortable lists, ajax posting, and inactive elements are not functioning correctly

I'm currently utilizing a fantastic plugin for jQuery known as HTML5 Sortable. It can be found at . I am attempting to disable certain items using this plugin. My approach involves adding the class="disabled" to the <li> tag and including items ...

Enable scrolling feature for the table

I have integrated a bootstrap table into my React application, but I am facing an issue where the table is not scrollable. Whenever new content is loaded, it increases the height of the table instead of making it scrollable. Below is the code for my table ...

Unable to insert additional lines into diamond shape generated solely with CSS

Is there anyone out there who can assist me in creating this logo using CSS? View the image of the logo here I initially attempted to make a square with width and height, followed by experimenting with pseudo elements. However, I hit a roadblock because ...

Is there a way to move my (bootstrap) elements to the bottom of the columns without using position absolute?

I'm currently working on 3 columns with icons at the bottom, as seen on the bottom of this website: I'm trying to align the icons directly to the bottom of each grey box without relying on position absolute which is what I'm currently using ...