CSS网格如何对齐可变高度的行内容

问题描述 投票:1回答:1

我有两行四列的结构,其中有可变长度的字符串。在启用自动行的情况下,行可以扩展以证明内容的合理性。我有一个按钮,每个网格的末端,我想与网格的底部对齐。如果可以通过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个回答
0
投票

将此添加到您的代码中:

.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

© www.soinside.com 2019 - 2024. All rights reserved.