0

I'm trying to wrap a long word. I have seen this post : How to prevent long words from breaking my div?

It works great in a simple case like this :

.wrapWords
{
    white-space: pre;           /* CSS 2.0 */
    white-space: pre-wrap;      /* CSS 2.1 */
    white-space: pre-line;      /* CSS 3.0 */
    white-space: -pre-wrap;     /* Opera 4-6 */
    white-space: -o-pre-wrap;   /* Opera 7 */
    white-space: -moz-pre-wrap; /* Mozilla */
    white-space: -hp-pre-wrap;  /* HP Printers */
    word-wrap: break-word;      /* IE 5+ */

}

<!-- This wraps correctly -->
<div style="width:145px;">
    <div class="wrapWords" style="width:100%;">
        <a href="#">AAAAAAAAAAAAAAAAAA</a>
    </div>
<div>

But my case has two nested tables like this :

<!-- This doesn't work -->
<table style="width:100%;">
    <tr>
        <td style="width:145px;">
            <table style="width:100%;">
                <tr>
                    <td style="width:100%;">
                        <div class="wrapWords" style="width:100%;">
                            <a href="#">BBBBBBBBBBBBBBBBBB</a>
                        </div>
                    </td>
                    <td>
                    </td>
                </tr>
            </table>
        </td>
        <td>
        </td>
    </tr>
</table>

You can test this code here http://jsfiddle.net/ZmnQ6/4/

Community
  • 1
  • 1
KVM
  • 878
  • 3
  • 13
  • 22
  • Do you want to wrap words (properly), or to bre ak the m? Your wording suggests the former, your code the latter. Two very different questions, with very different answers. – Jukka K. Korpela Oct 23 '13 at 09:43
  • I want a very long word like "AAAAAAAAAAAAAAAAAAAAAAA" to wrap inside it's container without changing his width. – KVM Oct 23 '13 at 12:19
  • “AAAAAAAAAAAAAAAAAAAAAAA” is not a word, at least not a word that you could properly wrap (with hyphenation). – Jukka K. Korpela Oct 23 '13 at 12:23
  • For instance "anticonstitutionnellement" is a long french word. If I place it in a small container, I don't won't the container to resize. I rather see the word cut at an arbitrary position. – KVM Oct 23 '13 at 13:20
  • Well, then you should specify that you want to break words. But I don’t see why you would do that instead of proper word division. – Jukka K. Korpela Oct 23 '13 at 13:22
  • How could the browser know how to divide properly a word without any spaces or hyphens ? – KVM Oct 23 '13 at 13:25
  • In many ways. But as I wrote, this is a different question. Try searching for *hyphenation* among questions at SO. – Jukka K. Korpela Oct 23 '13 at 13:48

3 Answers3

4

table is taking the table-layout: auto; by default so as per contents increase the width also increases so you need to set table-layout to fixed.

table{
    table-layout: fixed;
}

demo

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
0

There are 2 completely different algorithms for table layout: with or without table-layout: fixed

Former will adapt width of cells to the author's will
Latter will adapt width of cells to the content (the relative quantity/width/whatever of content in cells)

FelipeAls
  • 21,711
  • 8
  • 54
  • 74
0

I think this is what you are looking for.

WORKING DEMO

The HTML:

<!-- This wraps correctly -->
<div style="width:145px;">
    <div class="wrapWords" style="width:100%;">
        <a href="#">AAAAAAAAAAAAAAAAAA</a>
    </div>
<div>

<!-- This doesn't work -->
<table style="width:100%;">
    <tr>
        <td style="width:145px;">
            <table style="width:100%;">
                <tr>
                    <td style="width:130px;">
                        <div class="wrapWords" style="width:inherit; display:inline-block;">
                            <a href="#">BBBBBBBBBBBBBBBBBB</a>
                        </div>
                    </td>
                    <td>
                    </td>
                </tr>
            </table>
        </td>
        <td>
        </td>
    </tr>
</table>

The Logic:

The div and td have different display characteristics. You need to make your div which is nested inside the td to change its display to, for instance here inline-block with a fixed width to achive what you are looking for.

Hope this helps.

Nitesh
  • 15,425
  • 4
  • 48
  • 60