title: CSS text wrapping collapse container
body: <p>I'm looking for a HTML/CSS solution to a problem we've encountered on a site we're building.
  I am happy to implement a JavaScript based solution if I need to, but I'd prefer it was handled natively.</p>
  
  <p>We have content managed text which needs to sit inside a designated area but wrap if it exceeds the available width.
  Behind the text is a background colour with opacity. </p>
  
  <p>When the text is short, due to the float, the container collapses to the width of the text. 
  When the text is long, and a wrap occurs, the container hangs out at the maximum width, even though the text inside has wrapped underneath, so there's a small section of background colour on the right side (which isn't big enough for the wrapped word)
  I want the container to collapse to the edge of the previous word so it doesn't "look like" there is space for the wrapped word, when it is very close.</p>
  
  <p><strong>HTML</strong></p>
  
  <pre><code>&lt;div&gt;
      &lt;p&gt;Crack the Shutters Open Wide for Parkside Paradise&lt;/p&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>CSS</strong></p>
  
  <pre><code>body div {
      background-color: #AAFF3B;
      max-width:80%;
      padding:20px;
      display:inline-block;
      float:left;
  }
  body p {
      display:inline-block;
      background-color: #FFAA3B;
      position: relative;   
      float:left;
      white-space:pre-line;
      clear:left;
  }
  </code></pre>
  
  <p>Here is a JSFiddle: <a href="http://jsfiddle.net/nmuot8bm/3/" rel="nofollow noreferrer">http://jsfiddle.net/nmuot8bm/3/</a></p>
  
  <p>If you look at the 3rd example, you can see a small amount of padding on the right hand side of the orange box, where the word porttitor has wrapped underneath to a new line but the orange container still sits at the maximum width, despite the float.
  If line breaks are introduced by the content editors (e.g. between vestibulum and porttitor as per example 4) then the container collapses correctly.</p>
  
  <p>What I think is happening is the container grows before the text wraps and the browser doesn't recompute the width after wrapping?</p>
  
  <p>Here's a picture of my test case shown in the JSFiddle: <a href="https://i.stack.imgur.com/RmqMj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RmqMj.png" alt="enter image description here"></a></p>
  
  <p>Here is a picture of the fault on the staging site (before separated out to a JSFiddle):
  <a href="https://i.stack.imgur.com/OqZFP.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OqZFP.jpg" alt="example 2"></a></p>
  
  <p>You can see that the text has wrapped, but the container has not collapsed, leaving a big gap of background colour.</p>
  
  <p>n.b. We can emulate this by doing <code>text-align:justify</code> but then the spacing between the words is not consistent with the rest of the text on the site.</p>
  
  <p>edit: I see that this question may be a duplicate. I swear I did research before I posted!</p>
  
  <p><a href="https://stackoverflow.com/questions/13672760/max-width-adjusts-to-fit-text">max-width adjusts to fit text?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/12377826/css-width-max-width-on-line-wrap">CSS Width / Max-Width on Line Wrap?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/33244477/wrapping-text-forces-containing-element-to-max-width">Wrapping text forces containing element to max-width</a></p>
  
  <p><a href="https://stackoverflow.com/questions/14596213/shrink-div-to-text-thats-wrapped-to-its-max-width">Shrink DIV to text that&#39;s wrapped to its max-width?</a></p>
  
  <p>I think that the general consensus/conclusion is that it is not possible without bleeding edge CSS and I should use a JavaScript solution.</p>
  
  <p>I have added a few more examples to my JSFiddle: <a href="http://jsfiddle.net/nmuot8bm/6/" rel="nofollow noreferrer">http://jsfiddle.net/nmuot8bm/6/</a>
  including the JavaScript solution from here: <a href="https://stackoverflow.com/a/33246364/647728">https://stackoverflow.com/a/33246364/647728</a></p>