Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS pseudo-element content value with line-break via attr inserted by Javascript

There's a similar discussion here : CSS data attribute new line character & pseudo-element content value

Problem is, this didn't work if the attr is set via Javascript

I understand that \A doesn't work in attr, but now 
 doesn't work on attr via Javascript, is there any way to get this working?

const ele = document.getElementById('my-ele')
ele.classList.add('loading');
ele.setAttribute('loading-text', 'Your file is being generated...
This may take some minutes');
.loading::after {
  content: attr(loading-text);
}
<div id="my-ele"></div>
like image 271
Mojimi Avatar asked Feb 20 '19 14:02

Mojimi


People also ask

How do I target a pseudo-element in Javascript?

You can't select pseudo elements in jQuery because they are not part of DOM. But you can add a specific class to the parent element and control its pseudo elements in CSS. Show activity on this post. We can also rely on custom properties (aka CSS variables) in order to manipulate pseudo-element.

What is attr in CSS?

The attr() CSS function is used to retrieve the value of an attribute of the selected element and use it in the stylesheet. It can also be used on pseudo-elements, in which case the value of the attribute on the pseudo-element's originating element is returned.

When would you use the :: before or :: after pseudo-element in your CSS?

Special welcome offer: get $100 of free credit. CSS ::before and ::after pseudo-elements allow you to insert “content” before and after any non-replaced element (e.g. they work on a <div> but not an <input> ). This effectively allows you to show something on a web page that might not be present in the HTML content.


Video Answer


1 Answers

Use a plain newline character (\n in your JavaScript string), fix the getElementById() call (get rid of "#"), and add white-space: pre-wrap; to the CSS.

const ele = document.getElementById('my-ele')
ele.classList.add('loading');
ele.setAttribute('loading-text', 'Your file is being generated...\nThis may take some minutes');
.loading::after {
  white-space: pre-wrap;
  content: attr(loading-text);
}
<div id="my-ele"></div>
like image 108
Pointy Avatar answered Sep 21 '22 07:09

Pointy