【问题标题】:How to Place Image Grid into Bootstrap Card如何将图像网格放入引导卡
【发布时间】:2019-11-10 23:02:42
【问题描述】:

我有一张带有下面图片的引导卡。 我想插入一个图片网格,而不是一张图片。我将如何进行?尝试结合会产生带有空白的错位设计

引导卡代码:

<div class="card" style="width: 18rem;">
    <img class="card-img-top" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/210284/cape-town-768x432.jpg" alt="Card image cap">
    <div class="card-body">
        <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>
    </div>
</div>

网格图像代码:

<div class="grid-container">
    <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/210284/san-fransisco-768x432.jpg" alt="san francisco">
    <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/210284/london-768x432.jpg" alt="london">
    <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/210284/new-york-768x432.jpg" alt="new york">
    <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/210284/cape-town-768x432.jpg" alt="cape town">
</div>

.grid-container {
    display: grid;
    grid-template-columns: 100px 100px;
    grid-gap: 0em;
    padding: 0px;
}

img {
    width: 100%;
    height: auto;
    padding: 0px;
}

尝试合并会产生带有空白的错位设计:

<div class="card" style="width: 18rem;">
    <div class="grid-container">
        <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/210284/san-fransisco-768x432.jpg" alt="san francisco">
        <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/210284/london-768x432.jpg" alt="london">
        <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/210284/new-york-768x432.jpg" alt="new york">
        <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/210284/cape-town-768x432.jpg" alt="cape town">
    </div>
    <div class="card-body">
        <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>
    </div>
</div>

使用 MVC 网络核心

其他资源学习: Bootstrap cards not showing like in examples

【问题讨论】:

    标签: html css twitter-bootstrap asp.net-core


    【解决方案1】:

    这是您的解决方案。

    .card {
      width:18rem;
    }
    
    .grid-container {
        display: grid;
        margin:auto;
        grid-template-columns: auto auto; /* The grid-template-columns property specifies the number (and the widths) of columns in a grid layout. */
    }
    
    img {
        width: 100%;
        height: auto;
        padding: 0px;
    }
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
    
    <div class="card">
        <div class="grid-container">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/210284/san-fransisco-768x432.jpg" alt="san francisco">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/210284/london-768x432.jpg" alt="london">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/210284/new-york-768x432.jpg" alt="new york">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/210284/cape-town-768x432.jpg" alt="cape town">
        </div>
        <div class="card-body">
            Some quick example text to build on the card title and make up the bulk of the card's content.
        </div>
    </div>

    【讨论】:

      【解决方案2】:

      将列宽从100px 更改为1fr,无论.card 的宽度如何,它都可以工作

      .card {
        width:18rem;
        background: pink;
      }
      
      .grid-container {
          display: grid;
          grid-template-columns: repeat(2, 1fr);
          grid-gap: 0;
          padding: 0px;
      }
      
      img {
          width: 100%;
          height: auto;
          padding: 0px;
      }
      <div class="card">
          <div class="grid-container">
              <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/210284/san-fransisco-768x432.jpg" alt="san francisco">
              <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/210284/london-768x432.jpg" alt="london">
              <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/210284/new-york-768x432.jpg" alt="new york">
              <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/210284/cape-town-768x432.jpg" alt="cape town">
          </div>
          <div class="card-body">
              <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>
          </div>
      </div>

      【讨论】:

      【解决方案3】:

      试试这个:

      CSS

      img {
        width: 49%;
      }
      

      或者最好将自定义 CSS 类添加到卡片内的所有图像并提供其width: 49%

      codepen working demo

      【讨论】:

      • 我用的是mvc核心,这里不工作,49%有什么意义?看起来像它的调整边距等
      • 我将有大量的图片、尺寸、文字,所以寻找始终有效的解决方案
      猜你喜欢
      • 2014-01-15
      • 1970-01-01
      • 2016-01-01
      • 2021-02-05
      • 2015-01-29
      • 2018-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多