【问题标题】:How to keep three cards on the same line in bootstrap-5?如何在 bootstrap-5 中保持三张卡在同一行?
【发布时间】:2021-07-10 00:37:17
【问题描述】:

我有一个 Django 循环模板,它循环数据库中的前三张卡片。但他们并没有保持在同一条线上。只有两张是,但我想要三张卡在同一行。

<div class="row">
    {% for jax in u_jaxes|slice:"0:3" %}
    <div class="col-5 m-2">
    <div id="card2" class="card">
        <div class="card-body"> 
            <a href="{% url 'edit_jax' jax.title %}">{{ jax.title|truncatechars:21 }}</a><br><br>
            <img src="https://cdn1.iconfinder.com/data/icons/logotypes/32/badge-html-5-128.png" alt="Badge, html, html5, achievement, award, reward, trophy"/ height="17" width="17"> HTML, CSS, JS
            <small><span style="float:right">
                {{ jax.date_created|timesince }} ago
            </span></small>
            <a href="{% url 'edit_jax' jax.title %}" class="stretched-link"></a>
        </div>
    </div>
    </div>
    {% endfor %}
    </div>

css

<style>
        body {
            background-color: #f5f5f5;
        }
        #plus {
            border: none;
            background: none;
            color: blue;
            border: 1px solid #E6E6FA;
            background: #E6E6FA;
            border-radius: 5px;
            text-decoration: none;
            padding: 5px;
        }
        #plus:hover {
            border: none;
            background: none;
            color: white;
            border: 1px solid #E6E6FA;
            background: #1974D2;
            border-radius: 5px;
            transform: 4s;
        }
        #card {
            width: 214px;
        }
        #card2 {
            width: 309px;
            margin-left: -8px;
        }
        #card2 a {
            text-decoration: none;
            font-size: 17px;
        }
        #see {
            text-decoration: none;
            font-weight: bold;
            display: block;
            margin-top: 10px;
        }
        #img {
            width: 20px;
            height: 20px;
            border-radius: 50%;
            object-fit: cover;
            margin-bottom: 2px;
        }
        .row {
            width: 730px;
        }
        #sp {
            margin-top: 5px;
        }
    </style>

请注意,卡片之间存在特定间距。我也希望间距相同,同时三张卡片在同一行。

对于 css,我认为您需要关心的只是 card2 的东西,包括悬停和所有内容。我也认为row。而已。你可以忽略其余的。但只是为了确保,您可以根据需要检查所有 css。

【问题讨论】:

    标签: python html css django bootstrap-5


    【解决方案1】:

    检查this - 大约一半

    引导行使用display: flex 并且会自动换行到下一行(如果空间不足),除非您使用flex-wrap: no-wrap 之类的

    <div class='flex-nowrap'>With Inline BootStrap</div>
    OR
    myDivInCSS {
      flex-wrap: no-wrap;
    }
    

    至于项目之间的间距 - 我假设您需要在 flex 行上使用 justify-content: space-evenlyspace-between

    Check out the docs!

    【讨论】:

    • 嗨,我真的不知道我需要把你给的这段代码放在哪里。您能否根据我的代码添加此代码,即复制我的代码并将其粘贴到您的答案中,并在其中添加解决方案以供我理解。谢谢!
    【解决方案2】:

    您可以在网格的帮助下轻松完成。

    只要做:

    <div class="row">
        <div class="col-4">...</div>
        <div class="col-4">...</div>
        <div class="col-4">...</div>
    </div>
    

    <link href="https://getbootstrap.com/docs/5.0/dist/css/bootstrap.min.css" rel="stylesheet">
    
    <div class="row">
        <div class="col-4">
        <div class="card">
        <img src="https://miro.medium.com/fit/c/470/353/1*_ed9L5YlfdKC-D_THYCdJQ@2x.jpeg" class="card-img-top" alt="...">
      <div class="card-body">
        <h5 class="card-title">Card title</h5>
        <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
        <a href="#" class="btn btn-primary">Go somewhere</a>
      </div>
    </div>
        </div>
        
       <div class="col-4">
        <div class="card">
         <img src="https://miro.medium.com/fit/c/470/353/1*_ed9L5YlfdKC-D_THYCdJQ@2x.jpeg" class="card-img-top" alt="...">
      <div class="card-body">
        <h5 class="card-title">Card title</h5>
        <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
        <a href="#" class="btn btn-primary">Go somewhere</a>
      </div>
    </div>
        </div>
        
       <div class="col-4">
        <div class="card">
         <img src="https://miro.medium.com/fit/c/470/353/1*_ed9L5YlfdKC-D_THYCdJQ@2x.jpeg" class="card-img-top" alt="...">
      <div class="card-body">
        <h5 class="card-title">Card title</h5>
        <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
        <a href="#" class="btn btn-primary">Go somewhere</a>
      </div>
    </div>
        </div>
        
    </div>

    【讨论】:

    • 嗨,你能告诉我如何根据我的代码来做吗?就像我在描述中给出的那样。谢谢!
    • 将 col-5 替换为 col-4。那么它应该可以工作了。
    • 嗨,我试过了,但它所做的只是将两张卡片均匀地放在同一行,第三张卡片仍然在不同的线上。它不工作。你有替代品吗?另外,我认为这可能是我的 css 有问题,你也想看看我的 css 吗?
    • 你用的是bootstrap那我为什么要找css呢?
    • 不,我的意思是为引导卡设置宽度和高度,我使用了一些 CSS。你想看看吗? IT 可能有问题。但是您的解决方案不起作用。有替代品吗?
    【解决方案3】:

    你需要在你的 Css 中删除这一行

    .row {
        width: 730px;
     }
    

    并将col-5 替换为col-4。然后它应该工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-29
      • 2018-06-11
      • 2022-01-27
      • 2021-02-28
      • 2021-02-28
      • 2015-07-22
      • 2013-10-01
      • 1970-01-01
      相关资源
      最近更新 更多