简体   繁体   English

如何在 pre 标签中包装文本?

[英]How do I wrap text in a pre tag?

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? pre标签对于 HTML 中的代码块和编写脚本时调试 output 非常有用,但是如何使文本自动换行而不是打印出一长行?

The answer, from this page in CSS:答案,来自 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+ */
}

This works great to wrap text and maintain white-space within the pre -tag:这非常适用于包装文本并在pre -tag 中保持空白:

pre {
    white-space: pre-wrap;
}

我发现跳过 pre 标签并使用 white-space: pre-wrap 在 div 上是一个更好的解决方案。

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

Most succinctly, this forces content to wrap inside of a pre tag without breaking words.最简洁的是,这会强制内容包装在pre标签内,而不会破坏单词。

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

This is what I needed.这就是我所需要的。 It kept words from breaking but allowed for dynamic width in the pre area.它可以防止单词中断,但允许预区域中的动态宽度。

word-break: keep-all;

I suggest forget the pre and just put it in a textarea.我建议忘记 pre 并将其放在 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.以下是 php 摘录,因此如果您不在 php 中,那么您打包 html 特殊字符的方式会有所不同。

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

For info on how to copy text to the clipboard in js see: How do I copy to the clipboard in JavaScript?有关如何在 js 中将文本复制到剪贴板的信息,请参阅: 如何在 JavaScript 中复制到剪贴板? . .

However...然而...

I just inspected the stackoverflow code blocks and they wrap in a <code> tag wrapped in <pre> tag with css ...我刚刚检查了 stackoverflow 代码块,它们用 css 包裹在 <pre> 标签中的 <code> 标签中......

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/ .此外,stackoverflow 代码块的内容是使用(我认为) http://code.google.com/p/google-code-prettify/突出显示的语法。

Its a nice setup but Im just going with textareas for now.这是一个不错的设置,但我现在只使用 textareas。

I combined @richelectron and @user1433454 answers.我结合了@richelectron 和@user1433454 的答案。
It works very well and preserves the text formatting.它工作得很好并保留了文本格式。

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

</pre>

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.这将允许固定大小的长线水平滚动。

Try using尝试使用

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

Or better throw CSS.或者更好地抛出 CSS。

Use white-space: pre-wrap and some prefixes for automatic line breaking inside pre s.使用white-space: pre-wrap和一些前缀来在pre s 内自动换行。

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.不要使用word-wrap: break-word因为这当然只是将一个单词分成两半,这可能是您不想要的。

The Best Cross Browser Way worked for me to get line breaks and shows exact code or text: (chrome, internet explorer, Firefox)最佳跨浏览器方式为我工作以获得换行符并显示确切的代码或文本: (chrome,Internet Explorer,Firefox)

CSS: CSS:

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

HTML: HTML:

<xmp> your text or code </xmp>

This is what you need to wrap text inside pre tag:这是您需要将文本包装在 pre 标记内的内容:

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+ */
}

The following helped me:以下帮助了我:

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

Thanks谢谢

The <pre> -Element stands for "pre-formatted-text" and is intended to keep the formatting of the text (or whatever) between its tags. <pre> -Element 代表“预格式化文本”,旨在保持其标签之间的文本(或其他)格式。 Therefore it is actually not inteded to have automatic word-wrapping or line-breaks within the <pre> -Tag因此实际上并不打算在<pre> -Tag 中自动换行或换行

Text in a element is displayed in a fixed-width font (usually Courier), and it preserves both spaces and line breaks .元素中的文本以固定宽度字体(通常是 Courier)显示,并且保留了空格和换行符

source: w3schools.com , emphasises made by myself.资料来源:w3schools.com ,强调自己制作。

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;
}

<pre>可以工作,但是对于代码,有<code>标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM