How do I wrap text in a pre tag?

HtmlCss

Html Problem Overview


pre tags are super-useful for code blocks in HTML and for debugging output while writing scripts, but how do I make the text word-wrap instead of printing out one long line?

Html Solutions


Solution 1 - Html

The answer, from this page in CSS:

pre {
    white-space: pre-wrap;       /* Since CSS 2.1 */
    white-space: -moz-pre-wrap;  /* Mozilla, since 1999 */
    white-space: -pre-wrap;      /* Opera 4-6 */
    white-space: -o-pre-wrap;    /* Opera 7 */
    word-wrap: break-word;       /* Internet Explorer 5.5+ */
}

Solution 2 - Html

This works great to wrap text and maintain white-space within the pre-tag:

pre {
    white-space: pre-wrap;
}

Solution 3 - Html

I've found that skipping the pre tag and using white-space: pre-wrap on a div is a better solution.

 <div style="white-space: pre-wrap;">content</div>

Solution 4 - Html

Most succinctly, this forces content to wrap inside of a pre tag without breaking words.

pre {
    white-space: pre-wrap;
    word-break: keep-all
}

Solution 5 - Html

This is what I needed. It kept words from breaking but allowed for dynamic width in the pre area.

word-break: keep-all;

Solution 6 - Html

I suggest forget the pre and just put it in a textarea.

Your indenting will remain and your code wont get word-wrapped in the middle of a path or something.

Easier to select text range in a text area too if you want to copy to clipboard.

The following is a php excerpt so if your not in php then the way you pack the html special chars will vary.

<textarea style="font-family:monospace;" onfocus="copyClipboard(this);"><?=htmlspecialchars($codeBlock);?></textarea>

For info on how to copy text to the clipboard in js see: https://stackoverflow.com/questions/400212/how-to-copy-to-clipboard-in-javascript .

However...

I just inspected the stackoverflow code blocks and they wrap in a <code> tag wrapped in <pre> tag with css ...

code {
  background-color: #EEEEEE;
  font-family: Consolas,Menlo,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New,monospace,serif;
}
pre {
  background-color: #EEEEEE;
  font-family: Consolas,Menlo,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New,monospace,serif;
  margin-bottom: 10px;
  max-height: 600px;
  overflow: auto;
  padding: 5px;
  width: auto;
}

Also the content of the stackoverflow code blocks is syntax highlighted using (I think) http://code.google.com/p/google-code-prettify/ .

Its a nice setup but Im just going with textareas for now.

Solution 7 - Html

I combined @richelectron and @user1433454 answers.
It works very well and preserves the text formatting.

<pre  style="white-space: pre-wrap; word-break: keep-all;">

</pre>

Solution 8 - Html

You can either:

pre { white-space: normal; }

to maintain the monospace font but add word-wrap, or:

pre { overflow: auto; }

which will allow a fixed size with horizontal scrolling for long lines.

Solution 9 - Html

Try using

<pre style="white-space:normal;">. 

Or better throw CSS.

Solution 10 - Html

Use white-space: pre-wrap and vendor prefixes for automatic line breaking inside pres.

Do not use word-wrap: break-word because this just, of course, breaks a word in half which is probably something you do not want.

Solution 11 - Html

The <pre>-Element stands for "pre-formatted-text" and is intended to keep the formatting of the text (or whatever) between its tags. Therefore it is actually not inteded to have automatic word-wrapping or line-breaks within the <pre>-Tag

> Text in a

 element is displayed in a fixed-width font (usually Courier), and it preserves both spaces and line breaks.

source: w3schools.com, emphasises made by myself.

Solution 12 - Html

The Best Cross Browser Way worked for me to get line breaks and shows exact code or text: (chrome, internet explorer, Firefox)

CSS:

xmp{ white-space:pre-wrap; word-wrap:break-word; }

HTML:

<xmp> your text or code </xmp>

Solution 13 - Html

The following helped me:

pre {
    white-space: normal;
    word-wrap: break-word;
}

Thanks

Solution 14 - Html

in case your using prism or any code formatting library, then you will need to modify booth pre and code tags as follow:

pre {
    overflow-x: auto !important;
    white-space: pre-wrap !important;       /* Since CSS 2.1 */
    white-space: -moz-pre-wrap !important;  /* Mozilla, since 1999 */
    white-space: -pre-wrap !important;      /* Opera 4-6 */
    white-space: -o-pre-wrap !important;    /* Opera 7 */
    word-wrap: break-word !important;       /* Internet Explorer 5.5+ */
}

code{
    white-space: normal !important;
}

Solution 15 - Html

<pre> does it's work, but for code there is <code> tag.

Solution 16 - Html

This is what you need to wrap text inside pre tag:

pre {
 white-space: pre-wrap;       /* css-3 */
 white-space: -moz-pre-wrap;  /* Mozilla, since 1999 */
 white-space: -pre-wrap;      /* Opera 4-6 */
 white-space: -o-pre-wrap;    /* Opera 7 */
 word-wrap: break-word;       /* Internet Explorer 5.5+ */
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionadamboxView Question on Stackoverflow
Solution 1 - HtmladamboxView Answer on Stackoverflow
Solution 2 - HtmlRichard McKechnieView Answer on Stackoverflow
Solution 3 - HtmlMason240View Answer on Stackoverflow
Solution 4 - HtmlErin DelacroixView Answer on Stackoverflow
Solution 5 - Htmluser1433454View Answer on Stackoverflow
Solution 6 - HtmlekernerView Answer on Stackoverflow
Solution 7 - HtmlvovahostView Answer on Stackoverflow
Solution 8 - HtmlBobby JackView Answer on Stackoverflow
Solution 9 - HtmlEduardo CampañóView Answer on Stackoverflow
Solution 10 - HtmlFryingggPannnView Answer on Stackoverflow
Solution 11 - Htmlrob_stView Answer on Stackoverflow
Solution 12 - HtmlfeaccoView Answer on Stackoverflow
Solution 13 - Htmluser1020598View Answer on Stackoverflow
Solution 14 - HtmlSaleh AbdulazizView Answer on Stackoverflow
Solution 15 - HtmlpbiesView Answer on Stackoverflow
Solution 16 - HtmlNima HabibollahiView Answer on Stackoverflow