Grid aspect ratio with text and image covering all spare space

Viewed 93

I am trying to get a grid of cards with a text and an image where the image cover all card spare space. But I can't get the text to show, the text is scrolled off the card which has an overflow: hidden. (https://jsfiddle.net/s8gxuza0/)

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(275px, 1fr));
  gap: 24px;
}

.cell {
  position: relative;
  overflow: hidden;
  border-radius: 6px;
  padding-top: 100%;
  background: red;
}

.link {
  text-decoration: none;
  color: inherit;
}

.content {
  position: absolute;
  top: 0px;
  width: 100%;
  height: 100%;
}

.image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.text {
  padding: 16px;
}
<div class="grid">
  <div class="cell">
    <a class="link" href="https://stackoverflow.com/questions/65760464/grid-aspect-ratio-with-text-and-image-covering-all-spare-space" target="_blank">
      <div class="content">
        <img class="image" src="https://images.unsplash.com/photo-1499492568083-9115ab12d0d2?auto=format%2Ccompress&fit=crop&w=400" />
        <div class="text">
          text 1
        </div>
      </div>
    </a>
  </div>
  <div class="cell">
    <a class="link" href="https://stackoverflow.com/questions/65760464/grid-aspect-ratio-with-text-and-image-covering-all-spare-space" target="_blank">
      <div class="content">
        <img class="image" src="https://images.unsplash.com/photo-1533743409942-b91130480a7a?auto=format%2Ccompress&fit=crop&w=400" />
        <div class="text">
          text 2<br>
          more text
        </div>
      </div>
    </a>
  </div>
  <div class="cell">
    <a class="link" href="https://stackoverflow.com/questions/65760464/grid-aspect-ratio-with-text-and-image-covering-all-spare-space" target="_blank">
      <div class="content">
        <img class="image" src="https://images.unsplash.com/photo-1505569127510-bde1536937bc?auto=format%2Ccompress&fit=crop&w=400" />
        <div class="text">
          text 3
        </div>
      </div>
    </a>
  </div>
  <div class="cell">
    <a class="link" href="https://stackoverflow.com/questions/65760464/grid-aspect-ratio-with-text-and-image-covering-all-spare-space" target="_blank">
      <div class="content">
        <img class="image" src="https://images.unsplash.com/photo-1531852855896-94f2949b34fe?auto=format%2Ccompress&fit=crop&w=400" />
        <div class="text">
          text 4<br>
          more text
        </div>
      </div>
    </a>
  </div>
</div>

I too tryed with display: flex and flex: 1 1 auto but i didn't get it to work either.

Something like this, but keeping the aspect ratio of the whole card (image + text = aspect-ratio 1/1):

enter image description here

For example, as a diagram:

enter image description here

1 Answers

AND THIS FINAL VERSION

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(275px, 1fr));
  gap: 24px;
}

.cell {
  position: relative;
  overflow: hidden;
  border-radius: 6px;
  padding-top: 100%;
  background: red;
}

.link {
  text-decoration: none;
  color: inherit;
}

.content {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  display: flex;
  flex-direction: column;
}

.image {
  width: 100%;
  height: 100%;
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
}

.text {
  padding: 16px;
  height: fit-content;
  box-sizing: border-box;
}
<div class="grid">
  <div class="cell">
    <a class="link" href="https://stackoverflow.com/questions/65760464/grid-aspect-ratio-with-text-and-image-covering-all-spare-space" target="_blank">
      <div class="content">
        <div class="image" style="background-image: url(https://images.unsplash.com/photo-1499492568083-9115ab12d0d2?auto=format%2Ccompress&fit=crop&w=400);"></div>
        <div class="text">
          text 1
        </div>
      </div>
    </a>
  </div>
  <div class="cell">
    <a class="link" href="https://stackoverflow.com/questions/65760464/grid-aspect-ratio-with-text-and-image-covering-all-spare-space" target="_blank">
      <div class="content">
        <div class="image" style="background-image: url(https://images.unsplash.com/photo-1533743409942-b91130480a7a?auto=format%2Ccompress&fit=crop&w=400);"></div>
        <div class="text">
          text 2<br> more text
        </div>
      </div>
    </a>
  </div>
  <div class="cell">
    <a class="link" href="https://stackoverflow.com/questions/65760464/grid-aspect-ratio-with-text-and-image-covering-all-spare-space" target="_blank">
      <div class="content">
        <div class="image" style="background-image: url(https://images.unsplash.com/photo-1505569127510-bde1536937bc?auto=format%2Ccompress&fit=crop&w=400);"></div>
        <div class="text">
          text 3
        </div>
      </div>
    </a>
  </div>
  <div class="cell">
    <a class="link" href="https://stackoverflow.com/questions/65760464/grid-aspect-ratio-with-text-and-image-covering-all-spare-space" target="_blank">
      <div class="content">
        <div class="image" style="background-image: url(https://images.unsplash.com/photo-1531852855896-94f2949b34fe?auto=format%2Ccompress&fit=crop&w=400);"></div>
        <div class="text">
          text 4<br> more text
        </div>
      </div>
    </a>
  </div>
</div>

Related