0

So I'm trying to display cards within a grid. I would like the card size to be based on the grid size and then to overlay text on top of it that correctly matches the width.

Here's my implementation so far but it seems like the div/img no longer respects the grid's sizes

for (let i = 0; i < 15; i++)
{
  $("#grid").append(`
    <div class="item">
     <div>
        <img src="http://via.placeholder.com/250x350" />
        <div class="text">
          sadfsd
        </div>
   </div>
    </div>
  `);  
}
.flex {
  border: 1px solid black;
  display: flex;
  flex-direction: column;
  height: 100vh;
}
.footer {
  height: 20%;
}
.upper {
  flex: 1;
  overflow: auto;
}

#grid {
  border: 1px solid black;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr;
  height: 100%;
  position: relative;
  max-width: 100%;
  box-sizing: border-box;
}

.item {
  display: inline-block;               
  text-align: center;                    
  position: relative;
}

.inner {
  border: 1px solid red;
  max-height: 100%;
  max-width: 100%;

  
}

.text {
  position: absolute;
  bottom: 10%;
  left: 50%;
  transform: translateX(-50%);
  height: 30%;
  border: 1px solid black;
  width: 90%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="flex">
  <div class='upper'>
    <div id="grid">
    </div>  
  </div>
  <div class="footer">
    footer
  </div>
</div>

What I want is it to look something like this (of course with the text matching the width of the image)

for (let i = 0; i < 15; i++)
{
  $("#grid").append(`
    <div class="item">
      <img src="http://via.placeholder.com/150x350" />
      <div class="text">
        text
      </div>
    </div>
  `);  
}
.flex {
  border: 1px solid black;
  display: flex;
  flex-direction: column;
  height: 100vh;
}
.footer {
  height: 20%;
}
.upper {
  flex: 1;
  overflow: auto;
}

#grid {
  border: 1px solid black;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr;
  height: 100%;
  position: relative;
  max-width: 100%;
  box-sizing: border-box;                  /*  added  */
}

.item {
  position: relative;
}

.text {
  border: 1px solid black;
  position: absolute;
  bottom: 10%;
  left: 50%;
  transform: translateX(-50%);
  width: 90%;
  height: 30%;
}

img {
  display: block;                         
  margin: 0 auto;                         
  max-height: 100%;
  max-width: 100%;           
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<div class="flex">
  <div class='upper'>
    <div id="grid">
    </div>  
  </div>
  <div class="footer">
    footer
  </div>
</div>
A. L
  • 11,695
  • 23
  • 85
  • 163
  • 1
    The question is a bit unclear. This may be a problem of grid item minimum sizing. See here: https://stackoverflow.com/q/43311943/3597276 – Michael Benjamin Nov 13 '17 at 01:12
  • @Michael_B I want all the cards to fit into the grid like the second code snippet. However, I need that `position: absolute` div/text to be positioned within the dimensions of the image. Is that possible? – A. L Nov 13 '17 at 03:16
  • Yes, it is possible when done like this: https://stackoverflow.com/a/47259037/2827823 – Asons Nov 13 '17 at 07:33

0 Answers0