【问题标题】:Make a card that fits all content制作适合所有内容的卡片
【发布时间】:2020-12-14 09:11:36
【问题描述】:

我想在我的新闻网站(学校项目)的移动版本上制作 8 张卡片。我希望所有卡片都具有相同的比例,但是,我为卡片保存在计算机上的图像大小有所不同(其中一些更高)。

有没有办法让图片、标题和文本都适合卡片,而无需在将每张图片添加到项目之前调整其大小?

I wish for all cards to look like this

How the cards look like when the image size differ

代码:

img {
  max-width: 100%;
  max-height: 100%;
  display: block;  
}

.cards {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  margin: 5px;
}


.card{
  display: flex;
  flex-direction: column;
  padding: 5px;
  margin: 20px;
  margin-bottom: 5px;
  height: 280px;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

【问题讨论】:

    标签: css card


    【解决方案1】:

    只需为您的图片指定一个高度值,该高度值是您希望图片达到的最高值。您必须将宽度设置为自动以避免拉伸图片。

    img {
        height: 300px; /* play around with this number until you get the height you need */
        width: auto;
        max-width: 100%;
    }
    

    【讨论】:

    • 这样高度会均匀,但是如果图片的比例不一样,边上会出现一些缝隙。
    【解决方案2】:

    通常我将图像作为背景并将其居中放置。您也可以将所有背景属性放在一起,但如果您使用 php,我建议将 background-image 放入 style 标记中,其余的都作为类属性。

    css解释

    overflow: hidden; -> 这隐藏了图像的额外边界

    background-size: cover; -> 这使得图像根据容器大小尽可能地拉伸/调整大小(事实上我使用的是 600x800 的图像)Syntax

    background-position: center center; -> 如果您通常修剪图像,沿所有侧面均匀修剪它会更安全,但这取决于您。 Syntax

    奖励:只是为了确保避免小图像像图案一样重复,您还可以添加: 背景重复:不重复; 如果你设置了 background-size: cover 你不需要这个。

    /* where the magic happens */
    .img-as-bg {
      height: 300px;
      overflow: hidden;
      background-size: cover;
      background-position: center center;
    }
    
    
    /* extra style copied from your source */
    .cards {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      margin: 5px;
    }
    
    
    .card{
      display: flex;
      flex-direction: column;
      padding: 5px;
      margin: 20px;
      margin-bottom: 5px;
      height: 280px;
      box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
    }
    <div class="cards">
      <div class="card" style="width: 18rem;">
        <div class="img-as-bg" style="background-image: url('https://via.placeholder.com/600x800/0055ff');"></div>
        <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="#">Read more</a>
        </div>
      </div>
    
      <div class="card" style="width: 18rem;">
        <div class="img-as-bg" style="background-image: url('https://via.placeholder.com/500x900/00ffff');"></div>
        <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="#">Read more</a>
        </div>
      </div>
    </div>

    【讨论】:

      猜你喜欢
      • 2021-02-26
      • 2020-09-13
      • 1970-01-01
      • 2013-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-12
      • 1970-01-01
      相关资源
      最近更新 更多