Moslem Ben Dhaou
Moslem Ben Dhaou

Reputation: 7005

How to auto break text in an HTML cell?

I am generating an HTML page which is later converted as a PDF showing a certain grid of data. For that I am using an HTML table. The table used to stretch whenever the text is long causing half of it to disappear from the PDF (to the left).

I managed to fix the table width using table-layout: fixed. Now I am facing the case where the text is either showing on top of each other or it no longer visible if it is too long. Here is a sample:

I am looking for a convenient way to auto-add a line break whenever the text reaches the cell boundaries.

Upvotes: 0

Views: 895

Answers (2)

Jaykumar Patel
Jaykumar Patel

Reputation: 27614

You should use word-break CSS property.

Check this jsFiddle

HTML

<table class="ex2" border="1" width="100%">
    <tr>
        <td width="5%">1000000000000000000000000000</td>
        <td width="95%">1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000</td>
    </tr>
</table>

CSS

table.ex2 {
    table-layout:fixed;
    word-break: break-all;
}

Upvotes: 2

Kawinesh S K
Kawinesh S K

Reputation: 3220

If you want the words to be breaked according to the boundaries like the below pic

enter image description here

Use the CSS code

word-break: break-all;

Upvotes: 2

Related Questions