r/csshelp • • Mar 10 '20

Resolved Really struggling with something basic. Centering a row.

Trying to get the row of cards (bootstrap) to be centered as compared to being held on the left like it is currently. Here is what is looks like now: https://imgur.com/a/Ez6ihM1 and heres html/bootstrap:

    <div class="row m-3">

        {% for each_project in all_projects %}

        <div class="card m-3" style="width: 18rem; height: 18rem;">
            <img src="..." class="card-img-top" alt="...">
                   <div class="card-body">
                    <h5 class="card-title">{{each_project.proj_title}}</h5>
                    <p class="card-text">{{each_project.proj_disc}}</p>
                    <a href="/{{each_project.id}}/" class="btn btn-primary">View Project</a>
                </div>
        </div>
                
        {% endfor %}
        </div>

I also have some External CSS here and there but not enough to matter. Thank you!

10 Upvotes

4 comments sorted by

2

u/Zmodem Moderator Mar 10 '20

Wrap your <div class="card...></div> elements all in a single <div class="card-deck mx-auto"></div> container. Using your above code:

<div class="row m-3">
    <div class="card-deck mx-auto">

        {% for each_project in all_projects %}
        <div class="card m-3" style="width: 18rem; height: 18rem;">
            <img src="..." class="card-img-top" alt="...">
                   <div class="card-body">
                    <h5 class="card-title">{{each_project.proj_title}}</h5>
                    <p class="card-text">{{each_project.proj_disc}}</p>
                    <a href="/{{each_project.id}}/" class="btn btn-primary">View Project</a>
                </div>
        </div>
        {% endfor %}

    </div>
</div>

The card-deck is a great way to let BS know how you plan on spacing the cards within the container. The mx-auto is a BS class for spacing. Here are all of the Bootstrap spacing classes.

2

u/The_reCAPTCHA Mar 11 '20 edited Mar 11 '20

Thank you! I will give this a shot when I get back to the computer :)

edit: worked great thanks for the help.

1

u/Zmodem Moderator Mar 11 '20

worked great thanks for the help.

You're welcome! :)

2

u/permagrinfalcon Mar 10 '20 edited Mar 10 '20

Go with Zmodem's answer. (I'm not versed in bootstrap 4, we only use 3 sparingly)

As long as the cards are side by side using inline-block and not float, text-align: center should do the trick.

.row.m-3 {
    text-align: center;
}
.card.m-3 {
    display: inline-block;
    text-align: left;
}