I have an <div> container with an <img> and an ::after pseudo-element. Like so:
<div class="container" data-caption="this caption should be as wide as the image and then wrap">
  <img>
</div>
.container {
  display: inline-block
}
.container::after {
  display:      block
  background:   #aabbaa
  content:      attr(data-caption)
  line-height:  40px
  padding:      0 1rem
}    
The container should get its width from the contained image while the ::after element should wrap its content accordingly, like this:
Instead the after element does not wrap - see this codepen.


 
     
    