4

I'm attempting to create a max-width bounding box which will both wrap text (on spaces, no word-breaking allowed) and shrinkwrap to the width of the longest line of text. For a demo of the various shrinkwrap methods, see http://www.brunildo.org/test/IEMshrink-to-fit.html

I chose the "float" method, but in my testing, none of the methods accomplished my desired effect.

In the example code below (also available with live-preview at jsbin), I show what happens when you let the words wrap themselves, and what happens when you insert a <br /> line break tag. Using <br /> manually results in exactly the effect that I'm looking for, while omitting it wraps the text correctly, but forces the white box to take the entire max-width as its width, which I'd like to avoid.

<style>
  div#wrapper { background: #ddd; padding: 10px; }
  header { display: block; float: left; max-width: 320px; background: #fff; margin-bottom: 20px; clear: both; }
  #wrapper:after { content: " "; clear: both; display: table; }
</style>

<div id="wrapper">
  <header>
     <h1>Diane Von Furstenberg</h1>
  </header>
  <header>
    <h1>Diane Von<br />Furstenberg</h1>
  </header>
</div>

Here's a screenshot of the problem with some elaboration:

Screenshot of Shrinkwrap CSS issue

I've created a JS method to manually insert the <br /> tag as a stopgap measure, but I imagine there must be some way to do this properly using only CSS/HTML. Thanks for the help!

acdcjunior
  • 132,397
  • 37
  • 331
  • 304
Nano
  • 151
  • 1
  • 7
  • Can you add images to show what is happening and what you are expecting to happen. Including your code and reproducing your problem is usually enough (which you have done), but in this case I don't think it is obvious what the problem is. – My Head Hurts Dec 29 '11 at 16:42
  • @MyHeadHurts sure, one moment while I update the question with screenshots. – Nano Dec 29 '11 at 16:57
  • I don't think you can do that as the wrapper counts the space as fully used (and it's not enough, so it has to wrap). You could do it with JS, or go with a similar effect as on http://jsbin.com/ipixiy/edit#html,live – mreq Dec 29 '11 at 17:30

2 Answers2

1

Changing the display of the h1 to table-caption is close to what you want, in Google Chrome. But it's not perfect and I can't really recommend that as a solution wholeheartedly, mainly due to not testing it in any other browsers.

Adam
  • 23
  • 6
  • Works in latest Chrome and FF by my testing. If anyone else can confirm in IE 7+ I'll accept this answer. Thanks for the tip! – Nano Dec 29 '11 at 21:12
  • IE8+, Chrome (All), FF (All), Opera 8+, Safari (All) according to [this site](http://www.browsersupport.net/CSS/display%3Atable-caption). – Jess Telford Feb 08 '12 at 03:11
  • This probably doesn't do what you want in the general case; see my answer below for screenshots. – Martin Jul 16 '15 at 18:05
0

Not sure if browser behavior has changed since the earlier 'table-caption' answer was posted, but it does not currently work (using Chromium 41):

enter image description here

In reality, it seems the desired behavior is not possible with pure CSS (as of 2015). Further explanation and a lightweight Javascript workaround are available in another SO question: max-width adjusts to fit text?

Community
  • 1
  • 1
Martin
  • 518
  • 7
  • 11