I need to align images and text as in the bottom example; so the image aligns to the top of the text when the text has 2 lines. In my code it’s aligning to the center:
.product-legend {
font-size: 12px;
max-width: 210px;
}
.product-legend-img {
display: inline-block;
position: absolute;
}
.product-legend-text {
display: inline-block;
font-size: 12px;
width: 200px;
margin-left: 40px;
}
<div>
<div class="product-legend-img">img</div>
<div class="product- legend-text">NEW! this is a test</div>
</div>
<div>
<div class="product-legend-img">img</div>
<div class="product- legend-text">NEW! this is a test of a line with two lines of text</div>
</div>
<div>
<div class="product-legend-img">img</div>
<div class="product- legend-text">More features</div>
</div>
<div>
<div class="product-legend-img">img</div>
<div class="product- legend-text">Another line</div>
</div>
I tried setting the elements as span, and setting a height but I can’t seem to vertically-align the images.
2
Answers
I guess you mean something like this?
I used a flexbox because it’s simpler to position the text this way. So the list is displayed in columns (this is not necessarily needed
flex-direction: column
as this is also a lists default as long as the items do not havedisplay: inline | inline-block
) and the items are displayed in rows (flex-direction: row
is the default value, so by settingdisplay: flex
it’s a row). Each list-item has (due todisplay: flex
& row (again, default value)) the same height. Its childs too meaning image (wrapper,<picture>
) and the text wrapper (.text
) have the same height (info: thats not the case if they are displayed in columns). You can position smaller elements like the text withalign-items: start
separately. See css tricks flexbox guide for further info on flexbox.You just need
display: flex;
on the parent, remove the space from the clasname, and get rid ofposition: absolute;