CSS“内容”属性中的换行符序列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9062988/
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-30 02:46:29  来源:igfitidea点击:

Newline character sequence in CSS 'content' property?

css

提问by Saeed Neamati

Is it possible to use newlinecharacter in CSS contentproperty to force a line break? Something like:

是否可以在 CSS属性中使用换行符content来强制行?就像是:

figcaption:before
{
    content: 'Figure \n' + attr(title);
}

回答by Jukka K. Korpela

figcaption:before
{
    content: 'Figure \a' attr(title);
    white-space: pre;
}

Note that in the contentattribute value, concatenation is expressed just by whitespace, not by a “+” sign. The escape notation \ain a CSS string literal indicates a linebreak character.

请注意,在content属性值中,连接仅由空格表示,而不是由“+”号表示。\aCSS 字符串文字中的转义符号表示换行符。

回答by álvaro González

The contentproperty accepts a stringand:

内容属性接受一个字符串和:

A string cannot directly contain a newline. To include a newline in a string, use an escape representing the line feed character in ISO-10646 (U+000A), such as "\A" or "\00000a". This character represents the generic notion of "newline" in CSS.

字符串不能直接包含换行符。要在字符串中包含换行符,请使用表示 ISO-10646 (U+000A) 中换行符的转义符,例如“\A”或“\00000a”。这个字符代表了 CSS 中“换行符”的通用概念。

(No idea about actual browser support.)

(不知道实际的浏览器支持。)

You can check Using character escapes in markup and CSSfor reference about the escape syntax, which essentially is:

您可以查看在标记和 CSS 中使用字符转义以获取有关转义语法的参考,其本质上是:

  • \20ACmust be followed by a space if the next character is one of a-f, A-F, 0-9
  • \0020ACmust be 6 digits long, no space needed (but can be included)
  • \20AC如果下一个字符是 af、AF、0-9 之一,则必须后跟一个空格
  • \0020AC必须是 6 位数字,不需要空格(但可以包含)

NOTE: use \00000arather than just \Awhen escaping an arbitrary string, because if the newline is followed by a number or any character from [a-f]range, this may give an undesired result.

注意:使用\00000a而不仅仅是\A在转义任意字符串时,因为如果换行符后跟数字或[a-f]范围内的任何字符,这可能会产生不希望的结果