Rendering a chat thread in CSS and JavaScript

I thought rendering chat bubbles would be straightforward, but I found it tricky to get the CSS right and make the bubbles "shrink-wrap" to fit the text.