Organizing tables with Bootstrap 4: stacking one beneath the other and aligning centrally

I am having trouble with aligning my tables horizontally and displaying them one under the other. Despite using Bootstrap 4 classes, I'm encountering issues such as uneven table display or missing tables. Therefore, I am seeking assistance and providing the code where both tables exist in the same row.

<div class="mt-3 d-flex justify-content-center">
        <div class="text-center p-2">
        <table class="table table-dark table-striped w-auto">  
        <thead>
            <tr class="bg-primary">
            <th>Stop</th>
            <th>(A)</th>     
            <th>(A)</th>   
            <th>(A)</th>   
            <th>(A)</th>   
            </tr>
        </thead>       
        <tbody>
            <tr>
            <th scope="row">Lidzbark Warmiński</th>
            <td>08:00</td>
            <td>12:00</td>
            <td>15:00</td>
            <td>19:00</td>
            </tr>
            <tr>
            <th scope="row">Kraszewo</th>
            <td>08:20</td>
            <td>12:20</td>
            <td>15:20</td>
            <td>19:20</td>
            </tr>
            <tr>
            <th scope="row">Dobre Miasto</th>
            <td>08:30</td>
            <td>12:30</td>
            <td>15:30</td>
            <td>19:30</td>
            </tr>
            <tr>
            <th scope="row">Olsztyn PKP</th>
            <td>09:00</td>
            <td>13:00</td>
            <td>16:00</td>
            <td>20:00</td>
            </tr>
        </tbody>
        </table>
        </div>;  


        <div class="text-center p-2">
        <table class="table table-dark table-striped w-auto ">  
        <thead>
            <tr class="bg-primary">
            <th>Stop</th>
            <th>(A)</th>     
            <th>(A)</th>   
            <th>(A)</th>   
            <th>(A)</th>   
            </tr>
        </thead>        
        <tbody>
            <tr>
            <th scope="row">Olsztyn PKP</th>
            <td>09:00</td>
            <td>13:00</td>
            <td>17:00</td>
            <td>21:00</td>
            </tr>
            <tr>
            <th scope="row">Dobre Miasto</th>
            <td>09:30</td>
            <td>13:30</td>
            <td>17:30</td>
            <td>21:30</td>
            </tr>
            <tr>
            <th scope="row">Kraszewo</th>
            <td>09:50</td>
            <td>13:50</td>
            <td>17:50</td>
            <td>21:50</td>
            </tr>
            <tr>
            <th scope="row">Lidzbark Warmiński</th>
            <td>10:00</td>
            <td>14:00</td>
            <td>18:00</td>
            <td>22:00</td>
            </tr>
        </tbody>
        </table>
        </div>; 
    </div>;

Answer №1

To change the layout from rows to columns, simply set the flex direction to column and then align the items using flex-column and align-items-center in Bootstrap.

For more information, refer to the documentation: https://getbootstrap.com/docs/4.4/utilities/flex/

The

<div class="text-center p-2">
within this section is unnecessary. Just apply the text-center class directly to the table element.

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

<div class="mt-3 d-flex align-items-center flex-column">
    <table class="table table-dark table-striped w-auto text-center m-2">  
        <thead>
            <tr class="bg-primary">
            <th>Przystanek</th>
            <th>(A)</th>     
            <th>(B)</th>   
            <th>(C)</th>   
            <th>(D)</th>   
            </tr>
        </thead>        
        <tbody>
            <tr>
            <th scope="row">Station A</th>
            <td>08:00</td>
            <td>12:00</td>
            <td>15:00</td>
            <td>19:00</td>
            </tr>
            <!-- More data follows -->
        </tbody>
        </table>

    <table class="table table-dark table-striped w-auto text-center m-2">  
        <thead>
            <tr class="bg-primary">
            <th>Przystanek</th>
            <th>(A)</th>     
            <th>(B)</th>   
            <th>(C)</th>   
            <th>(D)</th>   
            </tr>
        </thead>       
        <tbody>
            <tr>
            <th scope="row">Station B</th>
            <td>09:00</td>
            <td>13:00</td>
            <td>17:00</td>
            <td>21:00</td>
            </tr>
            <!-- More data follows -->
        </tbody>
    </table>
</div>

Answer №2

Have you considered updating your layout slightly? You can place each table inside a col-12 column, with the second table positioned below the first one. This aligns with how bootstrap layouts function, eliminating the need for extra centering classes. Additionally, by adjusting the columns to col-md-8, you can customize the width of the tables. Utilize offset classes like offset-* to center them horizontally as well.

For reference, here are the Bootstrap DOCS: https://getbootstrap.com/docs/4.5/getting-started/introduction/

/*DEMO*/body{padding:3rem}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet">


<div class="row">
  <div class="col-12 mb-5">
    <table class="table table-dark table-striped">
      <thead><tr class="bg-primary"><th>Stop</th><th>(A)</th><th>(A)</th><th>(A)</th><th>(A)</th></tr></thead><tbody><tr><th scope="row">Example Stop</th> <td>08:00</td><td>12:00</td><td>15:00</td><td>19:00</td></tr><tr><th scope="row">Another Stop</th><td>08:20</td><td>12:20</td><td>15:20</td><td>19:20</td></tr><tr><th scope="row">Yet Another Stop</th><td>08:30</td><td>12:30</td><td>15:30</td><td>19:30</td></tr><tr><th scope="row">Final Stop</th><td>09:00</td><td>13:00</td><td>16:00</td><td>20:00</td></tr></tbody>
    </table>
  </div>
  <div class="col-8 offset-2">
    <table class="table table-dark table-striped">
      <thead><tr class="bg-primary"><th>Stop</th><th>(A)</th><th>(A)</th><th>(A)</th><th>(A)</th></tr></thead><tbody><tr><th scope="row">Final Stop</th><td>09:00</td><td>13:00</td><td>17:00</td><td>21:00</td></tr><tr><th scope="row">Yet Another Stop</th><td>09:30</td><td>13:30</td><td>17:30</td><td>21:30</td></tr><tr><th scope="row">Another Stop</th><td>09:50</td><td>13:50</td><td>17:50</td><td>21:50</td></tr><tr><th scope="row">Example Stop</th><td>10:00</td><td>14:00</td><td>18:00</td><td>22:00</td></tr></tbody>
    </table>
  </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

Unable to show the company logo in the navigation bar

Working on a pizza site and encountering an issue - trying to add the logo of the place to the navbar (which also links to the main page) but it's not displaying. Using Twitter Bootstrap for this project. Here is the code snippet: /*#557c3e green*/ ...

Problem with fixed element on mobile due to viewport width

I am experiencing an issue with the fixed element's width. Here is a code snippet for reference: https://codepen.io/highfield/pen/PKpXGG <body> <nav class="navbar"> <div style="position:absolute; left:20px; top:12px"> & ...

What is the best way to position a small image within a menu?

Is there a way to adjust the position of the arrow image within the <li> element without affecting the layout of the unordered list? Below is the code snippet for reference: body { margin: 0; padding: 0; } nav { width: 100%; background: # ...

The evolving impact of the HTML5 <video> element

I've been attempting to find information on this topic, but unfortunately my search has not been very successful. I vaguely recall coming across a tutorial that covered something like this. Does anyone happen to know how to alter the perspective of a ...

The navigation bar links are unresponsive and the right-aligned navigation items are not displaying correctly

Why are the navbar items "About us" and "Contact us" not clickable? How can we make these items clickable? Also, how do we properly align the navbar item "Login" which is currently positioned at the rightmost part of the navbar? The dropdown menu within t ...

Page alignment cannot be achieved using the body selector, but can be achieved with the div page wrap

Hey there, currently working on a HTML site and encountered an issue with centering the page. The code looks like this: HTML: <body> --Rest of code-- </body> CSS: body{ width:70%; margin-left:auto; margin-right:auto; } ...

In order to ensure proper coverage for phones, tablets, and standard screens across various widths, it is essential to implement the

As I delve into my project focused on responsive design and media queries, I have been studying resources like Aaron Gustavson's book, Adaptive Web Design, and Ben Frain's Responsive Web Design with HTML5 and CSS3, along with various online sourc ...

Arranging DIVs in a vertical layout

Currently, I am working on a design that involves organizing several <DIV> elements in a vertical manner while still maintaining responsiveness. Here are some examples: Wider layout Taller layout I have tried using floats, inline-block display, ...

Browsing using the "touchmove" feature feels extremely laggy

I implemented a workaround to achieve "position:fixed" on mobile devices, specifically my Android device. This involved creating two divs with heights that combined to match the screen height through JavaScript, and adding touch listeners to the bottom div ...

Tips for preventing browsers from losing style information stored in inputs

https://i.sstatic.net/ab9nF.jpg My custom input field has unique CSS styles applied to it, but unfortunately, when the browser auto-fills the information in those fields, the styles get reset. ...

Guide on incorporating clickable hyperlinks in Foundation tooltip

Currently, I am facing an issue with a header tag that has Foundation's tooltip attached to it as per the documentation. The problem is, I want the tooltip to be clickable so that I can display a modal on click, but every time I move away from the div ...

Adding a Bootstrap 4 menu to your WordPress site

Trying to get the Bootstrap 4 navbar menu set up has been a challenge for me. This is how the new Bootstrap 4 navbar menu code appears: <nav class="navbar navbar-expand-lg navbar-dark bg-success"> <button class="navbar-toggler" type="button" d ...

attempting to position an icon directly beside a piece of text

I’m struggling to align an icon beside some text. I attempted using the :before tag, but it’s not working. Could this be because I’m utilizing Google icons? Here's the code snippet I'm trying to replicate: .review { width: 317px; hei ...

Am I using the if statement correctly?

Can someone confirm if I am using the if statement correctly in this script? Or is it not possible to use an if statement within this code? <script type="text/javascript" charset="utf-8"> $(document).ready(function () { $('#customerTable&ap ...

What is the reason for the incompatibility between Bootstrap and Vanilla JavaScript?

Why does my slideshow crash when I use Bootstrap with vanilla JavaScript? It seems like there is a timeout or something... I'm not sure why this is happening. When I remove Bootstrap, the slideshow works fine. Here is my code: I attempted to remove ...

Utilizing dual pseudo elements ::before with varying styles

Utilizing two pseudo elements ::before with different border properties (refer to js fiddle), contrary to the rule that states "you can use only one ::before and one ::after pseudo element," this approach surprisingly worked. How is this possible? https:/ ...

Placing content in bottom and moving towards the left side

On my webpage, I have a div that I would like to fill with content similar to the image. How can I achieve this without resizing the div and instead populate it from bottom to top? ...

Resolved the z-index problem with the header and sidebar placement

Hello there! I have come across an issue that I need some help with. I have a fixed sidebar and header on my website, both of which have drop shadows applied to them. However, I don't want the header to cast a shadow on the sidebar as I want them to ...

Reducing Image Size for Logo Placement in Menu Bar

Hello all, I am a newcomer to the world of web coding. Please bear with me if I ask something that may seem silly or trivial. I recently created a menu bar at the top of my webpage. Below that, there is a Div element containing an image. My goal is to make ...

Tips for adjusting the width of a div to accommodate word wrapping

Can a div element be resized when word wrap occurs? To illustrate, take a look at this example from a similar query: .mypost { border: 1px solid Peru; margin: auto; min-width: 200px; display: inline-block; background: red; } .test { margin: ...