Search code examples
csstwitter-bootstrapbootstrap-4center

How to center align a bootstrap 4 card horizontal and vertical using bootstrap classes in the row and column?


I've seen multiple examples of how to center a bootstrap card but none of them fits my needs. I'm trying to center a bootstrap card vertical and horizontal so the card is placed exactly in the center of the screen. I want to achieve this by using only bootstrap classes like mx-auto etc. I've been trying for few hours but havent come to a solution yet, this is the code im working with:

<div class="row">
<div class="col-lg-3 col-md-3 col-sm-12">
    <div class="card h-100 login-card">
        <div class="card-body">
            <h5 class="card-title">Login</h5>
            <h6 class="card-subtitle text-muted">You can login here</h6>
        </div>
        <table class="table table-borderless table-hover h-75">
            <tr>
                <td>Test</td>
            </tr>
            <tr>
                <td>Test</td>
            </tr>
            <tr>
                <td>Test</td>
            </tr>
            <tr>
                <td>Test</td>
            </tr>
        </table>
    </div>
</div>

That results in this:

enter image description here

What I'm trying to get:

enter image description here

Hope someone can help me out and find the best solution to achieve this!

Thank you!


Solution

  • CSS:

     .row{
        height: 100vh;
     }
    

    HTML :

    <div class="row d-flex justify-content-center align-items-center">
            <div class="col-lg-3 col-md-3 col-sm-12">
                <div class="card h-100 login-card">
                    <div class="card-body">
                        <h5 class="card-title">Login</h5>
                        <h6 class="card-subtitle text-muted">You can login here</h6>
                    </div>
                    <table class="table table-borderless table-hover h-75">
                        <tr>
                            <td>Test</td>
                        </tr>
                        <tr>
                            <td>Test</td>
                        </tr>
                        <tr>
                            <td>Test</td>
                        </tr>
                        <tr>
                            <td>Test</td>
                        </tr>
                    </table>
                </div>
            </div>