3

I am trying to add 2 images with different heights and some text (height of text is different). I'm using Grid to create same height for divs with images (don't want to use height, because I don't know the height of images).

I don't know why, when I use display:grid the text is aligned in middle of the div. Some idea? I wasn't able to find out why.

* {
  box-sizing: border-box;
}

.magazine-items {
  width: 500px;
  display: flex;
  flex-flow: row wrap;
  align-items: start
}

.magazine-item {
  width: 50%;
  display: grid;
  border: 1px solid red;
}

.magazine-item img {
  width: 100%;
}

.double-text {
  width: 80%;
  margin: auto;
}
<div class="magazine-items">
  <div class="magazine-item">
    <div class="img">
      <img src="https://www.onlinekoupelny.cz/image/catalog/magazin/ELLE-EFFE_small.jpg" alt="">
    </div>
    <div class="text">
      <p>Porovnání detailů designu série Elle a Effe</p>
    </div>
  </div>
  <div class="magazine-item">
    <div class="img">
      <img src="https://www.onlinekoupelny.cz/image/catalog/magazin/Level_small.jpg" alt="">
    </div>
    <div class="text">
      <p>Vyvážené linie s citem pro maximální funkčnost, zaoblené tvary a nadčasová elegance činí z řady Level jednu z nejoblíbenějších sérií značky Paffoni. Vyvážené linie s citem pro maximální funkčnost, zaoblené tvary a nadčasová elegance činí z řady
        Level jednu z nejoblíbenějších sérií značky Paffoni.Vyvážené linie s citem pro maximální funkčnost, zaoblené tvary a nadčasová elegance činí z řady Level jednu z nejoblíbenějších sérií značky Paffoni.</p>
    </div>
  </div>
</div>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
  • it's not really aligned but this is the logical result of the grid calculation since you didn't define any template. Inspect the element to better see – Temani Afif Apr 10 '19 at 20:50
  • @TemaniAfif and can I align it at top? My problem is that I won't know how many divs there's gone by. It can by 2 or more. So, I do't know how to define the template without knowing the number of divs – Valentin Emil Cudelcu Apr 10 '19 at 20:53
  • add `grid-template-rows:auto 1fr` (on the grid container) or use `align-items:flex-start` (on the flex container) – Temani Afif Apr 10 '19 at 20:55
  • @TemaniAfif Already tried it, but the left text goes upper than the right and that's beucase of the height of images, which are different (1189px and 1459px). – Valentin Emil Cudelcu Apr 10 '19 at 21:01
  • Try `.magazine-item {align-items : start}` – RadekF Apr 10 '19 at 21:10
  • @RadekF `.magazine-item {align-items : start}` has no effect. but when I use it on `magazine-items` text goes top, but is not at the same line as the second text (check the snippet). My problem is that I cannot use `height` property, becaue i want it to work on every image, no matter what is width and height – Valentin Emil Cudelcu Apr 10 '19 at 21:14
  • Do you have some css on the text class? – RadekF Apr 10 '19 at 21:37
  • Nope. Maybe in the future. But now, it doesn't have any CSS – Valentin Emil Cudelcu Apr 10 '19 at 21:54

1 Answers1

1

p elements have default top and bottom margins.

These margins make your text appear centered in their containers.

enter image description here

Make them smaller or remove them.

Add this to your code:

p { margin-top: 0 }

You may also want to remove the descender space below images.

Add this to your code:

img { vertical-align: bottom; }

/* new */
p   { margin-top: 0; }
img { vertical-align: bottom; }


* {
  box-sizing: border-box;
}

.magazine-items {
  width: 500px;
  display: flex;
  flex-flow: row wrap;
  align-items: start
}

.magazine-item {
  width: 50%;
  display: grid;
  border: 1px solid red;
}

.magazine-item img {
  width: 100%;
}

.double-text {
  width: 80%;
  margin: auto;
}
<div class="magazine-items">
  <div class="magazine-item">
    <div class="img">
      <img src="https://www.onlinekoupelny.cz/image/catalog/magazin/ELLE-EFFE_small.jpg" alt="">
    </div>
    <div class="text">
      <p>Porovnání detailů designu série Elle a Effe</p>
    </div>
  </div>
  <div class="magazine-item">
    <div class="img">
      <img src="https://www.onlinekoupelny.cz/image/catalog/magazin/Level_small.jpg" alt="">
    </div>
    <div class="text">
      <p>Vyvážené linie s citem pro maximální funkčnost, zaoblené tvary a nadčasová elegance činí z řady Level jednu z nejoblíbenějších sérií značky Paffoni. Vyvážené linie s citem pro maximální funkčnost, zaoblené tvary a nadčasová elegance činí z řady
        Level jednu z nejoblíbenějších sérií značky Paffoni.Vyvážené linie s citem pro maximální funkčnost, zaoblené tvary a nadčasová elegance činí z řady Level jednu z nejoblíbenějších sérií značky Paffoni.</p>
    </div>
  </div>
</div>

More information:

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701