最大宽度<pre>,不换行

Maximum width of <pre>, not wrap text

我在我的 html 中使用 <pre> 标签,它分配了页面的整个宽度,而我需要让它与文本一样长。请注意,我的文字太短,但 <pre> 占满了页面的整个宽度,因此我没有考虑将我的文字换行到另一行,因为在 SO 中提到了一些问题。

来自 W3Schools:

The width attribute of <pre> is not supported in HTML5. Use CSS instead.

CSS syntax: <div style="width:200px;overflow:auto"><pre>Some
text</pre></div>

CSS Example: Pre-formatted text with a fixed width

来源:HTML PRE width Attribute

不过,正如您所指出的,这不是动态的。试试这两种方法:

pre {display:table; margin:auto;}

<pre style="border: 1px solid black; margin:auto; display:inline-block;">
    aaa
    bbbbbbbbbbbbbbbbbbbbbbbbbbbbb
    ccc
</pre>

来源:Auto width for PRE element, using CSS?