Here is the HTML code I am working with:
<div class="image">Image</div>
<div class="legend">
Legend<br/>
Width can vary, but height needs to be adjustable as well.<br/>
The middle of the legend should align exactly at the bottom of the image, regardless of their respective heights.
</div>
<div class="following">The following text must come right after the legend, irrespective of their sizes.</div>
This is my desired outcome :
https://i.sstatic.net/iQAfZ.png
This is what I attempted :
.image {
height:100px;
background-color:red;
}
.legend {
transform:translateY(-50%);
background-color:blue;
width:300px;
margin:0 auto
}
.following {
background-color:yellow;
margin-top:-45px;
}
This is what I ended up with : https://i.sstatic.net/8IJ4N.png
The issue is that I do not want any gap between the legend and the following text.
You can find the complete code attempt on CodePen here.
Question : Is there a way to achieve the desired outcome without using JS?
(Just for reference, I have also provided a solution involving JavaScript)