Using ‘word-wrap: break-word’ within a table

October 20th, 2012 by Laeeq | No comments

As you can implement word wrapping efficiently with <div> element. But it creates problem when we implement it with <table> table element. In below example, when we use “word-wrap:break-word” with <div> element and it works great.

  1. <div style=“width:100%; word-wrap: break-word;”>
  2. AAAAAAAAAAAAAAAAA
  3. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
  4. </div>

Step 1: When the browser is wide enough, A+ and B+ are on the same line.

  1. AAAAAAAAAAAAAAAAA ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ

Step 2: As you narrow the browser, A+ and B+ are put on separate lines.

  1. AAAAAAAAAAAAAAAAA
  2. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ

Step 3: When b+ can no longer fit, it is broken and put on two lines.

  1. AAAAAAAAAAAAAAAAA
  2. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
  3. ZZZZZZ

All works great,  but when we use <table> instead of <div>,  it creates  problem.

  1. <table style=“width:100%; word-wrap:break-word;”>
  2. <tr>
  3. <td>
  4. AAAAAAAAAAAAAAAAA
  5. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
  6. </td>
  7. </tr>
  8. </table>

The table stops narrowing after step 2 and step 3 doesn’t ever happen. The break-word doesn’t seem to be filtering down.

Use “table-layout: fixed” with table:

So here we will use “table-layout: fixed” which will get force the cells to fit within table.

  1. <table style=“width:100%; word-wrap:break-word;table-layout: fixed;”>
  2. <tr>
  3. <td>
  4. AAAAAAAAAAAAAAAAA
  5. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
  6. </td>
  7. </tr>
  8. </table>

The “table-layout: fixed” property will fixed the table width with its defined width and help “word-wrap:break-word” to work.

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

  • Apply Multiple CSS classes for a single element
  • Free! CSS Box Machine
  • Popular Free HTML5 and CSS Templates
  • Awesome Responsive CSS Frameworks
  •  

     

    1. No comments yet.
    1. No trackbacks yet.