List 3 bootstrap cards per row using template loop

huangapple go评论71阅读模式
英文:

List 3 bootstrap cards per row using template loop

问题

我想将卡片列出如下:

List 3 bootstrap cards per row using template loop

到目前为止,这是我的代码。我的卡片垂直逐个列出。我如何实现这一点?

{% extends 'base_content.html' %}
{% block content %}
{% for item in CATEGORY_CHOICES %}
<div class="row" style="justify-content: center;">
<div class="col-sm-3">
  <div class="card">
    <div class="card-body">
      <h5 class="card-title">{{ item.1 }}</h5>
    </div>
  </div>
</div>
{% endfor %}
{% endblock %}
英文:

I'd like to list the cards as down below:

List 3 bootstrap cards per row using template loop

This is the code so far. My cards are listed one by one vertically. How can I achieve this?

{% extends &#39;base_content.html&#39; %}
{% block content %}
{% for item in CATEGORY_CHOICES %}
&lt;div class=&quot;row&quot; style=&quot;justify-content: center;&quot;&gt;
&lt;div class=&quot;col-sm-3&quot;&gt;
  &lt;div class=&quot;card&quot;&gt;
    &lt;div class=&quot;card-body&quot;&gt;
      &lt;h5 class=&quot;card-title&quot;&gt;{{ item.1 }}&lt;/h5&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
{% endfor %}
{% endblock %}

答案1

得分: 1

  1. 当前的循环为每个项目创建一个新的“row”,这就是为什么每张卡都堆叠在一起。它只应该循环“col”,而不是“row”。

  2. col-sm-3 表示“在sm及以上的屏幕上每列使用12份中的3份空间”,这意味着在sm断点及以上显示4列(而不是3列)。如果你想在sm及以上的屏幕上每行显示3张卡,可以使用 col-sm-4,或者如果你希望在任何时候都显示3张卡,可以使用 col-4

{% extends 'base_content.html' %}
{% block content %}

<!-- 不要在这里循环 -->
<div class="row justify-content-center">

  <!-- 只循环列 -->
  {% for item in CATEGORY_CHOICES %}
  <div class="col-sm-4"> <!-- 不是 "col-sm-3" -->
    <div class="card">
      <div class="card-body">
        <h5 class="card-title">{{ item.1 }}</h5>
      </div>
    </div>
  </div>
  {% endfor %}

</div>

{% endblock %}
英文:
  1. The loop currently creates a new row for each item, which is why every card is stacking. It should only loop col, not row.

  2. col-sm-3 means "use 3/12 space per column at sm and above" which means 4 columns (not 3) at the sm breakpoint and above. Use col-sm-4 to display 3 cards per row at sm and above, or just col-4 if you want 3 cards per row at all times.

{% extends &#39;base_content.html&#39; %}
{% block content %}

&lt;!-- don&#39;t loop here --&gt;
&lt;div class=&quot;row justify-content-center&quot;&gt;

  &lt;!-- only loop the columns --&gt;
  {% for item in CATEGORY_CHOICES %}
  &lt;div class=&quot;col-sm-4&quot;&gt; &lt;!-- not &quot;col-sm-3&quot; --&gt;
    &lt;div class=&quot;card&quot;&gt;
      &lt;div class=&quot;card-body&quot;&gt;
        &lt;h5 class=&quot;card-title&quot;&gt;{{ item.1 }}&lt;/h5&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  {% endfor %}

&lt;/div&gt;

{% endblock %}

huangapple
  • 本文由 发表于 2023年2月14日 01:40:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439415.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定