How do I add spacing between rows of a card-deck in bootstrap

二次信任 提交于 2021-02-20 03:57:41

问题


I fixed the size of each columns so that there's always 4 deck-items per row and the card-deck automatically goes to the next row whenever there is more than 4 deck-items in the row. But the spacing between two rows are very small and it looks like as if they are overlapping. As I am new to bootstrap I cant figure out how to add spacing between them. I tried to use margin but it didnt worked.
This is my code for card-deck

<div class="card-deck">
    @foreach ($animes as $anime)
        <div class="col-sm-3">
        <div class="card h-100">
            <img class="card-img-top" src="/storage/cover_images/{{$anime->cover_image}}" alt="{{$anime->name}}">
            <div class="card-body">
                <p class="card-text text-center"> <strong>{{$anime->name}}</strong></p>
            </div>
            <a class="card-footer btn btn-primary bg-primary" href="/shows/{{$anime->id}}"> Explore </a>
        </div>
        </div>
    @endforeach
</div>

回答1:


You can override the main Bootstrap styles (don't edit the actual source code) so that each card has a margin bottom or top.

.card {
    margin-bottom: 1em;
}

Creating your own style is fine too but, I would not edit the col-sm-3 because that would be a universal project change and you can run into issues later on.

Additionally, if you want to use another built in spacing utility from bootstrap try editing the html with:

.mb-1, .mb-2, ... , .mb-5 (same works for padding). All you have to do is choose a spacing property (m or p for margin or padding), choose a side/direction (t or b for top or bottom), and the size (1-5 and auto).

Docs: https://getbootstrap.com/docs/4.3/utilities/spacing/

HTML example (adding mb-4 to your card):

<div class="card-deck">
    @foreach ($animes as $anime)
        <div class="col-sm-3">
        <div class="card h-100 mb-4">
            <img class="card-img-top" src="/storage/cover_images/{{$anime->cover_image}}" alt="{{$anime->name}}">
            <div class="card-body">
                <p class="card-text text-center"> <strong>{{$anime->name}}</strong></p>
            </div>
            <a class="card-footer btn btn-primary bg-primary" href="/shows/{{$anime->id}}"> Explore </a>
        </div>
        </div>
    @endforeach
</div>

This will add a margin to the bottom of your card. I personally use margin bottom but it's up to you.




回答2:


You can just add some margin to col-sm-3. Then you can define the space between them:

.col-sm-3 {
  /*enter the space here:*/
  margin-top:20px;
}

or create an own class, if you do not want to modify the class for the whole project, where you define the space:

.someClass {
  /*enter the space here:*/
  margin-top:20px;
}

...
<div class="col-sm-3 someClass">
...

Have a look at this fiddle: https://jsfiddle.net/zdra058w/




回答3:


You should simply use the Spacing Utilities as provided by Bootstrap. Margin-Bottom-3

@foreach ($animes as $anime)
    <div class="card mb-3">
    </div>
@endforeach

Also the use of .col inside a card-deck is counter-intuitive. See my previous answer to override the flex-basis if you want to tackle columns. The .h-100 can be eliminated as well with this approach since card-decks are an alternative for the legendary "equal heights".

DEMO



来源:https://stackoverflow.com/questions/58955997/how-do-i-add-spacing-between-rows-of-a-card-deck-in-bootstrap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!