Bootstrap - 连续卡片的宽度奇怪

Bootstrap - cards in a row have a strange width

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

卡片应放在列内。

"Rows are wrappers for columns. Each column has horizontal padding (called a gutter) for controlling the space between them... "

还有...

"When building grid layouts, all content goes in columns. The hierarchy of Bootstrap’s grid goes from container to row to column to your content. "

你不想...

  • 使用不带行的 col-*,因为它是直接父级。
  • 将内容直接放在行内。

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

您可以将 .p-0 添加到 .card 或以其他方式将元素的填充设置为零。在下面的代码片段中,我仅将 .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>