1

So I have an h1 tag in my html, with text in it.

<h1>123</h1>



h1:before{
    content:"?????";
    }

Now I want this text to be used in my css before pseudo class, and the text is dynamic, it will be changed during the runtime of the app, so it's not static. I need the before content always reflect the current inner text.

Help?

1

1 Answer 1

2

You can use the attr (attribute) fot that. I searched on web, and found this article: css-before-and-after

CSS:

h1::before {
  content: attr(data-dynamic);
  margin-right: 10px;
  
}

HTML :

<h1 data-dynamic='something' > Magic </h1>

Result:

Something Magic


So, you pass in data-dynamic(or another name) what you want in the before or after content.

Sign up to request clarification or add additional context in comments.

3 Comments

I don't have this attribute on the h1 tag. I need access to the inner text and not any other attributes
Read about that: element.before(), but use JavaScript or JQUERY. I can show you, case it is not clear yet.
OK. Nice to learn new stuff. I thought there is a css way to do this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.