0

I have a div nested inside another div and I want it to be at the bottom of the parent div. I was able to get this to work but there is a small issue. The text in the nested div extends below the parent div. How can I prevent this? Note: This is not extra space below the image. The bottom of the div is not aligning with the bottom of the image.

.imgOverlayTitle {
    position: absolute;
    top: 0;
    right: 0;
    left: 0;
    margin: auto;
}

.imgOverlayDesc {
    position: absolute;
    bottom: 0;
    right: 0;
    left: 0;
    margin: auto;
}
<div style="position: relative; display: inline-block;">
    <div class="imgOverlayTitle">Title goes here</div>
    <div class="imgOverlayDesc">Description goes here</div>
    <img src="http://www.hillspet.com/HillsPetUS/v1/portal/en/us/cat-care/images/HP_PCC_md_0130_cat53.jpg" />
</div>
Johann
  • 27,536
  • 39
  • 165
  • 279

1 Answers1

3

A solution would be to make the container div a flexbox. See below.

.imgOverlayTitle {
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  margin: auto;
}

.imgOverlayDesc {
  position: absolute;
  bottom: 0;
  right: 0;
  left: 0;
  margin: auto;
}
<div style="position: relative; display: flex;">
  <div class="imgOverlayTitle">Title goes here</div>
  <div class="imgOverlayDesc">Description goes here</div>
  <img src="http://www.hillspet.com/HillsPetUS/v1/portal/en/us/cat-care/images/HP_PCC_md_0130_cat53.jpg" />
</div>
Gerard
  • 15,418
  • 5
  • 30
  • 52