首頁 > web前端 > css教學 > 主體

如何在 CSS 中的偽元素內容 ::after 或 ::before 新增換行符?

Susan Sarandon
發布: 2024-11-15 05:43:02
原創
434 人瀏覽過

How to Add Line Breaks to ::after or ::before Pseudo-Element Content in CSS?

Adding Line Breaks to ::after or ::before Pseudo-Element Content

Question:

How do you add multiple lines to the content property of a ::after or ::before pseudo-element when access to the markup is limited to CSS?

Answer:

The 'content' property allows line breaks to be inserted using the "\A" escape sequence. However, to prevent line wrapping, set the 'white-space' property to 'pre' or 'pre-wrap'.

Example:

#headerAgentInfoDetailsPhone:after {
  content: "Office: XXXXX \A Mobile: YYYYY ";
  white-space: pre;
}
登入後複製

For more robust escaping, use "\00000a" instead of "\A" to handle arbitrary input, as seen in this code:

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

以上是如何在 CSS 中的偽元素內容 ::after 或 ::before 新增換行符?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板