【问题标题】:CSS Grid how to align variable height row contentCSS Grid如何对齐可变高度的行内容
【发布时间】:2020-07-26 01:14:19
【问题描述】:

我有一个两行四列的结构,其中包含一个可变长度的字符串。启用自动行后,行可以拉伸以证明内容的合理性。我有一个按钮和每个网格的末端,我想将它们与网格的底部对齐。如果可以使用 css 网格轻松实现,有人可以帮助我吗?

html,
body {
  padding: 0;
  margin: 0;
}

input {
  font-size: 14px;
  font-family: Helvetica, sans-serif;
}

body {
  background-color: #BBB;
  font-family: Helvetica, sans-serif;
  padding-bottom: 100px;
}

h2,
h3 {
  margin: 0 0 .75em 0;
}


/* first example */

.container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-auto-rows: minmax(250px, auto);
}

.card {
  border-right: 2px solid grey;
  border-bottom: 2px solid grey;
  padding: 1em;
}

.grid {
  display: grid
}
<!-- first example -->
<div class="container">
  <div class="card">
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
      has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
      publishing software like Aldus PageMaker including versions of Lorem Ipsum.

    </p>
    <p class="grid"><button> Submit Me!</button></p>
  </div>

  <div class="card">
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it.
    </p>
    <p class="grid"><button> Submit Me!</button></p>
  </div>
  <div class="card">
    <p>Lorem Ipsum

    </p>
    <p class="grid"><button> Submit Me!</button></p>
  </div>

  <div class="card">
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
      has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
      publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>

    <p class="grid"><button> Submit Me!</button></p>

  </div>
  <div class="card">
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type.

    </p>
    <p class="grid"><button> Submit Me!</button></p>

  </div>
  <div class="card">
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
      has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
      publishing software like Aldus PageMaker including versions of Lorem Ipsum.

    </p>
    <p class="grid"><button> Submit Me!</button></p>
  </div>

  <div class="card">
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambledh the release of Letraset sheets containing
      Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

    </p>
    <p class="grid">
      <button> Submit Me!</button>
    </p>
  </div>
</div>

这是 CodePen: https://codepen.io/awaisdar001/pen/wvKaEvx

【问题讨论】:

    标签: css grid css-grid


    【解决方案1】:

    将此添加到您的代码中:

    .card {
        border-right: 2px solid grey;
        border-bottom: 2px solid grey;
        padding: 1em;
    
        /* new */
        display: flex;
        flex-direction: column;
        justify-content: space-between; /* pins flex items to opposite ends */
    }
    

    这也有效:

    .card {
        display: flex;
        flex-direction: column;
    }
    
    .grid {
        margin-top: auto; /* "pushes" flex item from the top, pinning it to the bottom */
    }
    

    revised codepen

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-05
      • 1970-01-01
      • 2018-10-08
      • 2020-09-30
      • 1970-01-01
      • 1970-01-01
      • 2018-11-04
      • 1970-01-01
      相关资源
      最近更新 更多