ChatGPT解决这个技术问题 Extra ChatGPT

Add line break to ::after or ::before pseudo-element content

I do not have access to the HTML or PHP for a page and can only edit via CSS. I've been doing modifications on a site and adding text via the ::after or ::before pseudo-elements and have found that escape Unicode should be used for things such as a space before or after the added content.

How do I add multiple lines in the content property?

In example the HTML break line element is only to visualize what I would like to achieve:

#headerAgentInfoDetailsPhone::after {
  content: 'Office: XXXXX <br /> Mobile: YYYYY ';
}
CSS is not for adding or editing content, it is for controlling how content displays
@SamithaHewawasam While I agree with your statement, it looks like the poster has found themselves in a situation where they cannot edit the HTML. For small, simple content additions, I can see why this would be useful/necessary.
I would also argue that line breaks are far more format-related than content related. The only way you "see" a line break is by observing a format change in the content.

D
David Avsajanishvili

The content property states:

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.

So you can use:

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

http://jsfiddle.net/XkNxs/

When escaping arbitrary strings, however, it's advisable to use \00000a instead of \A, because any number or [a-f] character followed by the new line may give unpredictable results:

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

For a moment I thought "why \A and not \n" - then I remembered newline is 0x0A :)
I tried this in with print media query, it doesn't work. is it a bug or it is the default behaviour? Without print media, it works.
Thanks for mentioning pre-wrap! I was having problems with #headerAgentInfoDetailsPhone not wrapping any more in Blink engine web browsers like Chrome. Firefox is not susceptible to the white-space: pre; property propagating back up to the element.
Babel is giving me The only valid numeric escape in strict mode is '\0'
b
brennanyoung

Nice article explaining the basics (does not cover line breaks, however).

A Whole Bunch of Amazing Stuff Pseudo Elements Can Do

If you need to have two inline elements where one breaks into the next line within another element, you can accomplish this by adding a pseudo-element :after with content:'\A' and white-space: pre

HTML

<h3>
    <span class="label">This is the main label</span>
    <span class="secondary-label">secondary label</span>
</h3>

CSS

.label:after {
    content: '\A';
    white-space: pre;
}

C
Cédric NICOLAS

I had to have new lines in a tooltip. I had to add this CSS on my :after :

.tooltip:after {
  width: 500px;
  white-space: pre;
  word-wrap: break-word;
}

The word-wrap seems necessary.

In addition, the \A didn't work in the middle of the text to display, to force a new line.

 &#13;&#10; 

worked. I was then able to get such a tooltip :

https://i.stack.imgur.com/efIsR.png


H
Hemerson Varela

You may try this

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

Js Fiddle


S
Szymon

For people who will going to look for 'How to change dynamically content on pseudo element adding new line sign" here's answer

Html chars like &#13;&#10; will not work appending them to html using JavaScript because those characters are changed on document render

Instead you need to find unicode representation of this characters which are U+000D and U+000A so we can do something like

var el = document.querySelector('div'); var string = el.getAttribute('text').replace(/, /, '\u000D\u000A'); el.setAttribute('text', string); div:before{ content: attr(text); white-space: pre; }

Hope this save someones time, good luck :)


m
martoncsukas

Add line break to ::after or ::before pseudo-element content

.yourclass:before {
    content: 'text here first \A  text here second';
    white-space: pre;
}

C
Community

Found this question here that seems to ask the same thing: Newline character sequence in CSS 'content' property?

Looks like you can use \A or \00000a to achieve a newline


m
marko-36
<p>Break sentence after the comma,<span class="mbr"> </span>in case of mobile version.</p>
<p>Break sentence after the comma,<span class="dbr"> </span>in case of desktop version.</p>

The .mbr and .dbr classes can simulate line-break behavior using CSS display:table. Useful if you want to replace real
.

Check out this demo Codepen: https://codepen.io/Marko36/pen/RBweYY,
and this post on responsive site use: Responsive line-breaks: simulate <br /> at given breakpoints.


Asked for a CSS not HTML solution.
But if someone else was searching for "Add line break to ::after or ::before pseudo-element content" and landed here, but actually could edit his HTML, they might this helpful, right?