HTML 将文本对齐与 <pre> 标签对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8919776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 12:37:20  来源:igfitidea点击:

HTML justify text-align with <pre> tag

htmlfixed-widthpretext-justify

提问by julianfperez

I have a problem with the text-align "justify" inside a paragraph with format and fixed width, because I want the paragraph text in HTML to appear totally justified (like it would happen with a MS Word file). However, this does not happen. What I need to modify to achieve it?

我对具有格式和固定宽度的段落内的文本对齐“对齐”有问题,因为我希望 HTML 中的段落文本显示完全对齐(就像 MS Word 文件会发生的那样)。然而,这不会发生。我需要修改什么来实现它?

     <pre style="color: #000000; font-family: Arial, Helvetica, sans-serif; font-size: medium; width: 500px; text-align: justify;"> 

I have a problem with the text-align "justify" inside a paragraph with format and fixed width, because I want the paragraph text in HTML to appear totally justified (like it would happen with a MS Word file). However, this does not happen. What I need to modify to achieve it?

        </pre>

EDIT:if I replace the <pre>tag with <p>tag, my code works properly. Can somebody explain me why? Perhaps I am using a wrong use of <pre>tag.

编辑:如果我用<pre>标签替换<p>标签,我的代码可以正常工作。有人可以解释我为什么吗?也许我使用了错误的<pre>标签。

回答by Devalli

  pre {
    text-align: left;
    white-space: pre-line;
  }

回答by Kobra

To justify text in <pre>you need to add white-space: normal;in your styles, but in my opinion this misses the point using the <pre>tag.

要使文本对齐,<pre>您需要添加white-space: normal;样式,但在我看来,这忽略了使用<pre>标签的要点。

-- Edit --

- 编辑 -

Instead of <pre>tag you can just use <div>with appropriate styles. It can imitate <pre>tag very well and is easier to play with.

<pre>您可以使用<div>适当的样式代替标签。它可以<pre>很好地模仿标签,并且更容易玩。

回答by Victor

Use tags:

使用标签:

<pre><code>
Your text
Your text
</code></pre>

Remove all spaces preceding your text, all indentation.

删除文本前的所有空格,所有缩进。