Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display image in css grid?

Tags:

html

css

css-grid

I am trying to make a css grid for photos but the img-element doesn't display the image in the grid item. There's nothing wrong with the url I am using since it works on other parts of the page but not in the grid system.

.wrapper {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-column-gap: 25px;
  grid-row-gap: 25px;
}

.wrapper > div img {
  max-width: 100%;
  max-height: 100%;
}
<div id="content">
  <div class="wrapper">
    <div><img src="https://placehold.it/100x100" alt=""></div>
    <div>Two</div>
    <div>Three</div>
    <div>Four</div>
    <div>Five</div>
  </div>
</div>

What am I doing wrong?

like image 281
Sandra Avatar asked Sep 01 '25 02:09

Sandra


2 Answers

You can set image width in wrapper class

.wrapper {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-column-gap: 25px;
  grid-row-gap: 25px;
}

.wrapper > div img {
  max-width: 100%;
}
<div id="content">
  <div class="wrapper">
    <div><img src="https://placehold.it/100x100" alt=""></div>
    <div>Two</div>
    <div>Three</div>
    <div>Four</div>
    <div>Five</div>
  </div>
</div>
like image 64
İbrahim YANIK Avatar answered Sep 02 '25 15:09

İbrahim YANIK


It is happening for combination of two CSS properties. One is display: grid; in .wrapper selector and other is max-height: 100%; in .wrapper > div img selector.

Firstly you can avoid by remove max-height: 100%; from selector but if you need keep the max-height: 100%; property then you need to add display: block; in .wrapper > div img selector like following:

.wrapper > div img {
    max-width: 100%;
    max-height: 100%;
    display: block;
}
like image 42
Hanif Avatar answered Sep 02 '25 16:09

Hanif