I’m trying to determine whether it’s possible to create css for an element that supports word-wrap:break-word, but that also expands to take the width of its children when breaking is not possible.
<html>
<style>
.outer {
background-color:red;
word-wrap:break-word;
}
</style>
<div class="outer">
User generated content:
<a href="http://www.google.com">http://anannoyinglylongurlthatcausesthepagetogrowtolongunlessIusewordwrapbreakwordasdfasfasdfasdfasdfasdfasdfsadfasdfadsf</a>
<table>
<tr>
<td>asdfasdfadsffdsasdfasdfsadfafsd</td>
<td>asdfasdfadsffdaasdfassdffaafasds</td>
</tr>
</table>
<img src="http://www.google.com/intl/en_com/images/srpr/logo3w.png"/>
</div>
</html>
In the above sample, the url breaks properly, but the table and img overflow the red outer div if the window becomes narraower than the table.
If I make the outer div display:inline-block or display:table, the red outer div correctly expands to include the content, but the url doesn’t break if the window is narrower than the url.
I only need this to work in WebKit (on Android), and I’m trying to find a CSS only (no Javascript) solution if possible.
Looking at the CSS spec, it’s likely that what I’m trying to do is impossible, although I find the size calculations fairly difficult to decipher. Here are some important bits:
http://www.w3.org/TR/CSS21/visudet.html
So if I want the background of my containing box to grow to be the width of the children, it appears I need to make sure it’s layout is calulated in an inline formatting context:
http://www.w3.org/TR/CSS21/visuren.html#normal-flow
Great. Hopefully the breaking rules also include emergency wrapping possibilities.
http://www.w3.org/TR/2010/WD-css3-text-20101005/#word-wrap
Doesn’t really help; let’s look at the newer draft spec:
http://www.w3.org/TR/css3-text/#overflow-wrap
This isn’t very clear, but if ‘min-content’ instrinsic sizes has something to do with the same calculations used to determine line-breaking possibilities, I might be out of luck.
I ended up just using Javascript to measure the content and decide whether to show it in block or inline context. Sigh.