css - 在::after或::before伪元素内容中添加换行符

标签 css pseudo-element

我无法访问页面的 HTML 或 PHP,只能通过 CSS 进行编辑。我一直在对网站进行修改并通过 ::after::before 伪元素添加文本,并且发现转义 Unicode 应该用于诸如此类的事情作为添加内容之前或之后的空格。

如何在 content 属性中添加多行?

例如,HTML 换行元素只是为了可视化我想要实现的目标:

#headerAgentInfoDetailsPhone::after {
  content: 'Office: XXXXX <br /> Mobile: YYYYY ';
}

最佳答案

content属性状态:

Authors may include newlines in the generated content by writing the "\A" escape sequence in one of the strings after the 'content' property. This inserted line break is still subject to the 'white-space' property. See "Strings" and "Characters and case" for more information on the "\A" escape sequence.

所以你可以使用:

#headerAgentInfoDetailsPhone:after {
  content:"Office: XXXXX \A Mobile: YYYYY ";
  white-space: pre; /* or pre-wrap */
}

http://jsfiddle.net/XkNxs/

但是,当转义任意字符串时,建议使用 \00000a 而不是 \A,因为任何数字或 [a-f] 字符接下来是新行可能会给出 unpredictable results :

function addTextToStyle(id, text) {
  return `#${id}::after { content: "${text.replace(/"/g, '\\"').replace(/\n/g, '\\00000a')} }"`;
}

关于css - 在::after或::before伪元素内容中添加换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17047694/

相关文章:

html - 扩展 float div的宽度

html - 仅使用 css/scss 使 td 元素与输入元素宽度相同?

html - 如何使用 CSS 将图像定位在最右侧?

javascript - 使用 JS 将 REST 数据附加到矩阵网格

css - 隐藏有 child 背景的边界半径和溢出

css - :after pseudo element not working in Safari

html - 弹出框在 overflow hidden 的 div 内不可见

html - 引用链接似乎没有连接到 CSS

css - 带有CSS内容的div,伪元素不可见后

javascript - 在滚动条上淡化伪元素 - JS 或 jQuery