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>

8
  • 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 Commented Apr 10, 2019 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 Commented Apr 10, 2019 at 20:53
  • add grid-template-rows:auto 1fr (on the grid container) or use align-items:flex-start (on the flex container) Commented Apr 10, 2019 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). Commented Apr 10, 2019 at 21:01
  • Try .magazine-item {align-items : start} Commented Apr 10, 2019 at 21:10

1 Answer 1

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:

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it goes up, but it's not at the same line as the second text (the text on right). How could I fix that?
I added link references to my answer that may assist you further. @ValentinEmilCudelcu

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.