简体   繁体   中英

<pre> tag meaning and the white-space CSS property

<pre> tag is commonly known as " formatted text". 格式化文本”。

There is also a white-space CSS property which enables pre -like behaviour for any tag. However on it's description page at W3Schools and at CSS3 specs page this behavior is described as a " serving space algorithm". 占空间算法”。 In the same spec there is only a single place in which 'white-space' is referenced as a preformatted text.

Is this nonsense or maybe <pre> was initially a shorthand for a "preserving space" tag?

What's the background of this tag? I'm not a professional HTML designer, I know what's the meaning of the both versions, but I don't know how it is referenced or was referenced by people. Is naming it 'preserving space' is a correct technical term which no-one uses?

Of the two links you gave, W3Schools is probably not the best place to go for a reliably accurate definition like this.

And while the MDN page that you linked to is a much better resource, it is not the official CSS3 spec that you think it is; you'll find the official spec at http://www.w3.org/TR/CSS2/text.html#propdef-white-space

If you read the official spec, you'll find that the behaviour is not described using either of the phrases you mentioned in the question. (The word 'preserved' does appear, but only in the context of newlines).

The <pre> tag itself predates CSS; it was in very early versions of HTML, and was defined in those as "Preformatted text", but from a CSS perspective there is no specific meaning to the letters pre , other than referring back to the behaviour of the original HTML element of that name.

Early W3C documents, such as Hypertext Markup Language - 2.0 (dating from 1995) all call the <pre> element Preformatted text .
Although some files also refer to the fact that <pre> "preserves spaces", they do indicate that's a result of the preformatting, not the intended purpose of this element.
For instance, the HTML 3.2 Reference Specification says

The PRE element can be used to include preformatted text. User agents render this in a fixed pitch font, preserving spacing associated with white space characters such as space and newline characters.

The PRE-tag was and is used to display text with whitespace preserved. So text does not wrap, newlines are not ignored, spaces are not ignored.

It is however possible to undo this any way you like with CSS (or Javascript).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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