Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

word-wrap: break-word with div inside td

I'm trying to use the CSS word-wrap property with break-word value. I want to use this inside a td, and apparently need to use a additional div tag for this to work. fine.

I tried to build a simplified use-case:

HTML:

<table class="sectors">
  <tr>
    <td><div>HURTEAUX / Jean-Baptiste mr)</div></td>
    <td><div>CHEUNJGgdfgdfvfsefsdfsdfsdfsdvvvHJG / Samuel mr</div></td>
    <td><div>CHEUNG / Samuel mr)</div></td>
  </tr>
</table>

CSS

.sectors td {
  border: 1px solid #000;
}

.sectors td div {
  width: 150px;
  word-wrap: break-word;
  border: 1px solid red;
}

The problem

Firefox, Chrome and Safari behave as expected (at least as I expect).

screenshot on Firefox 3.5

But IE (IE6, IE7, IE8) has a strange behavior: the text is properly wrapped inside the div (red border), but it seems the td (black border) is allocating extra space for no reason.

screenshot on IE7

Any workaround or explanation?

like image 1000
JB Hurteaux Avatar asked Nov 03 '09 15:11

JB Hurteaux


People also ask

How do you break a word in TD?

There are two methods to wrap table cell <td> content using CSS which are given below: Using word-wrap property: This property is used to allow long words to break and wrap onto the next line. Using word-break property: This property is used to specify how to break the word when the word reached at end of the line.

How do I force wrap text in a div?

If you've faced the situation when you need to wrap words in a <div>, you can use the white-space property with the "pre-wrap" value to preserve whitespace by the browser and wrap the text when necessary and on line breaks. Also, you'll need the word-wrap property.

How do I wrap text in a cell in a table?

Use the border-collapse property set to "collapse" and table-layout property set to "fixed" on the <table> element. Also, specify the width of the table. Then, set the word-wrap property to its "break-word" value for <td> elements and add border and width to them.

What is overflow wrap break word?

The overflow-wrap property in CSS allows you to specify that the browser can break a line of text inside the targeted element onto multiple lines in an otherwise unbreakable place. This helps to avoid an unusually long string of text causing layout problems due to overflow. .example { overflow-wrap: break-word; }


1 Answers

Adding overflow:hidden to the div should sort it out. Can't offer an explanation though, sorry!

like image 161
keirog Avatar answered Oct 19 '22 03:10

keirog