157

I want to show a paragraph from database into a table cell.

The result is a large 1 line, ignoring how its organised in database. ignoring 'enters' for example (new lines)

I want to show it exactly according to how it's written in database.

For example if paragraph is saved like this:

hello ,
my name is x.

I want it to be showed exactly like that, instead of:

hello, myname is x.
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Shady
  • 1,701
  • 2
  • 13
  • 14

15 Answers15

215

You want to use the CSS white-space:pre applied to the appropriate <td>. To do this to all table cells, for example:

td { white-space:pre }

Alternatively, if you can change your markup, you can use a <pre> tag around your content. By default web browsers use their user-agent stylesheet to apply the same white-space:pre rule to this element.

The PRE element tells visual user agents that the enclosed text is "preformatted". When handling preformatted text, visual user agents:

  • May leave white space intact.
  • May render text with a fixed-pitch font.
  • May disable automatic word wrap.
  • Must not disable bidirectional processing.
Phrogz
  • 296,393
  • 112
  • 651
  • 745
  • Thumbs up, `white-space` is a CSS 2.1 property that's widely supported. Tested on IE8, works great. – leesei Apr 19 '13 at 02:26
  • I'm in a situation now where `white-space: pre;` is being ignored when applied in a `` tag. Using `
    ` to wrap our content works fine, but due to the use of this table (exporting to excel), large #s of columns and rows balloon the size of the exported file and cause excel to crash upon reading the file. (odd, I know). Has anyone else seen this or have an idea for a solution?
    – JoeBrockhaus Aug 28 '13 at 19:20
  • 1
    This solution is far from being perfect. It results that Excel produces mutiple lines for the cells that have linebreaks. The only correct answer can be found here: http://www.bennadel.com/blog/1095-maintaining-line-breaks-in-an-html-excel-file.htm – Elmue Aug 05 '16 at 01:47
82
style="white-space:pre-wrap; word-wrap:break-word"

This would solve the issue of new line. pre tag would add additional CSS than required.

moveson
  • 5,103
  • 1
  • 15
  • 32
Jajikanth pydimarla
  • 1,512
  • 13
  • 11
  • 2
    This is a nice, targeted solution. The `
    ` tag does all kinds of things beyond just allowing newline characters, but these style settings solve only the problem presented.
    – moveson Nov 08 '17 at 18:24
  • I had a problem in which my data contains new line. Other solution like replacing text with
    or
    or
     were being treated as text only. This solution works perfectly overcoming that problem.
    – leocrimson Apr 23 '19 at 16:07
  • Fantastic!! This work very well for me. I had a JDataTable which listed mutliple text and I needed you css to wrap the text properly! Day saver Thanks alot! – PatsonLeaner Jul 09 '19 at 10:29
  • 1
    This works for me in terms of preserving line breaks, but adds a line break to the top of every cell... any solutions? – jtr13 Nov 09 '19 at 20:33
  • I so want to upvote your answer but it's on 69... – JOSEPH Blessingh Dec 28 '21 at 23:22
37

Wrap the content in a <pre> (pre-formatted text) tag

<pre>hello ,
my name is x.</pre>
Joyce Babu
  • 19,602
  • 13
  • 62
  • 97
31

Two suggestions to solving this problem:

SOLUTION 1: <div style="white-space:pre;">{database text}</div> or <pre>{database text}</pre>

This is good solution if your text has no html tags or css properties. Also allows to maintain tabs for example.

SOLUTION 2: Replace \n with <p></p> or <br/>

This is a solution if you would just like to add break-lines, without losing other text properties or formatting. An example in php would be $text = str_replace("\n","<br />",$database_text);

You can also use <p></p> or <div></div>, but this requires a bit more text parsing.

Daniel Vila Boa
  • 670
  • 5
  • 13
13

Hi I needed to do the same thing! Don't ask why but I was generating a html using python and needed a way to loop through items in a list and have each item take on a row of its own WITHIN A SINGLE CELL of a table.

I found that the br tag worked well for me. For example:

<!DOCTYPE html>
<HTML>
    <HEAD>
        <TITLE></TITLE>
    </HEAD>
    <BODY>
  <TABLE>
            <TR>
                <TD>
                    item 1 <BR>
                    item 2 <BR>
                    item 3 <BR>
                    item 4 <BR>
                </TD>
            </TR>
        </TABLE>
  </BODY>

This will produce the output that I wanted.

chumbaloo
  • 671
  • 6
  • 16
12

the below code works like magic to me >>

td { white-space:pre-line }
Sumon Bappi
  • 1,937
  • 8
  • 38
  • 82
9

On your server-side code, replace the new lines (\n) with <br/>.

If you're using PHP, you can use nl2br()

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
6

I use the html code tag after each line (see below) and it works for me.

George Benson </br> 123 Main Street </br> New York, Ny 12344 </br>

Rene
  • 95
  • 1
  • 1
4

I added only <br> inside the <td> and it works good, break the line!

Zvi
  • 577
  • 6
  • 19
2

For my case, I can use like this.

td { white-space:pre-line , word-break: break-all}
Zhong Ri
  • 2,556
  • 1
  • 19
  • 23
1

If you have a string variable with \n in it, that you want to put inside td, you can try

<td>
    {value
        .split('\n')
        .map((s, index) => (
            <React.Fragment key={index}>
                {s}
                <br />
            </React.Fragment>
        ))}
</td>
Eugene Ihnatsyeu
  • 1,299
  • 13
  • 15
0

I found a lot but all of them dont work for me. If you retrieved a long text from your db and put it on the table but just wanna do the front-end styling to break the word to the new line.

For my case, white-space: break-spaces; work for me

Kopi Bryant
  • 1,300
  • 1
  • 15
  • 30
0

if you are working in only HTML you can use \n or <br> or <pre> any tag but if you are doing CRUD operations

" In my case, I'm using Jquery for creating and updating an 'td.html()' and retrieve 'td.html(input)'. "

0

you can use break tag in html but in excel it creates problem,it divides text in to two cells so add in this way and add this css in in it

<br style="mso-data-placement:same-cell;"/>

addin this css in br tag in html on downloading excel it will work as you have described

-1

Add a new CSS rule for td called white-space:nowrap.

td { white-space:nowrap }
node_modules
  • 4,790
  • 6
  • 21
  • 37
RuchDi
  • 250
  • 3
  • 14