【问题标题】:Bootstrap - cards in a row have a strange widthBootstrap - 连续的卡片有一个奇怪的宽度
【发布时间】:2021-10-18 01:13:06
【问题描述】:

卡片 html 如下所示:

<div class="container-fluid">
  <img src="http://clipart-library.com/img/1279251.png" style="max-width: 100px; filter: invert(1); float: left">
  <h1 style="color: white;">Favourite <br>apps </h1>
  <div class="row flex-row overflow-auto flex-nowrap" id="favourites" style="max-height: 400px">
    <div tabindex="0" class="card" style="max-width: 18rem;" id="ytdroid2">
      <img src="https://tse2.mm.bing.net/th?id=OIP.qHEnapkicCACp91KvwDYUAHaFj" class="card-img-top">
      <div class="card-body" style="height: 200px;">
        <h1 class="card-title" style="color: white;">
          <b>Test 2</b>
        </h1>
      </div>
    </div>
    <div tabindex="0" class="card" style="max-width: 18rem;" id="ytdroid">
      <img src="https://tse2.mm.bing.net/th?id=OIP.qHEnapkicCACp91KvwDYUAHaFj" class="card-img-top">
      <div class="card-body" style="height: 200px;">
        <h1 class="card-title" style="color: white;">
          <b>Test 1</b>
        </h1>
      </div>
    </div>
  </div>
</div>

它会创建:

卡片和卡片主体的背景颜色在 CSS 表单中设置为白色,不透明度为 0.4。如您所见,卡片的宽度大于图像和卡片主体的宽度。如果我从中删除与行相关的类,它们的大小相同,但这将不起作用,因为它们垂直堆叠。我不能使用卡片组,因为我希望它们具有固定宽度并且可以水平滚动,这似乎不适用于卡片组。如何让卡片的宽度与图片和正文的宽度相匹配?

【问题讨论】:

    标签: html css twitter-bootstrap electron bootstrap-5


    【解决方案1】:

    卡片应该放在列内。

    "行是列的包装。每列都有水平填充 (称为排水沟)用于控制它们之间的空间......“

    还有……

    “在构建网格布局时,所有内容都在列中。 Bootstrap 网格的层次结构从 container 到 row 到 column 到 你的内容。 "

    你不想...

    • 使用不带行的 col-* 作为其直接父级。
    • 将内容直接放在行内。

    【讨论】:

      【解决方案2】:

      无论您如何使用网格类(正确或错误),在您的情况下,空间的来源是填充 BS 适用于.card。

      您可以将.p-0 添加到.card 或以其他方式将元素上的填充设置为零。在下面的 sn-p 中,我只在第一张卡上添加了 .p-0 以进行比较:

      div.container-fluid {
        background-color: DarkSlateBlue;
      }
      
      div.card,
      div.card-body {
        background-color: rgba(255, 255, 255, 0.4);
      }
      <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
      
      <div class="container-fluid">
        <img src="http://clipart-library.com/img/1279251.png" style="max-width: 100px; filter: invert(1); float: left">
        <h1 style="color: white;">Favourite <br>apps </h1>
        <div class="row flex-row overflow-auto flex-nowrap" id="favourites" style="max-height: 400px">
          <div tabindex="0" class="card p-0" style="max-width: 18rem;" id="ytdroid2">
            <img src="https://tse2.mm.bing.net/th?id=OIP.qHEnapkicCACp91KvwDYUAHaFj" class="card-img-top">
            <div class="card-body" style="height: 200px;">
              <h1 class="card-title" style="color: white;">
                <b>Test 2 .card.p-0</b>
              </h1>
            </div>
          </div>
          <div tabindex="0" class="card" style="max-width: 18rem;" id="ytdroid">
            <img src="https://tse2.mm.bing.net/th?id=OIP.qHEnapkicCACp91KvwDYUAHaFj" class="card-img-top">
            <div class="card-body" style="height: 200px;">
              <h1 class="card-title" style="color: white;">
                <b>Test 1 .card</b>
              </h1>
            </div>
          </div>
        </div>
      </div>
      
      
      <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-U1DAWAznBHeqEIlVSCgzq+c9gqGAJn5c/t99JyeKa9xxaYpSvHU5awsuZVVFIhvj" crossorigin="anonymous"></script>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-01-07
        • 2018-07-16
        • 2016-09-14
        • 2022-11-11
        • 2013-11-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多