I have a node in my html which is as below
<h2> My Text </h2>
Is there any way to style h2 element on the basis of its Text(without using any class)
eg.
h2[text='My Text']{
color:red;
}
Please note that I don't want to use javascript code
I have a node in my html which is as below
<h2> My Text </h2>
Is there any way to style h2 element on the basis of its Text(without using any class)
eg.
h2[text='My Text']{
color:red;
}
Please note that I don't want to use javascript code
no, you can't do it with just css. there was a proposal for a contains
selector but it wasn't realized. currently javascript is the only way.
If you can attach some identifier like a class to the element you can style it normally, but to answer your questions, no you can't select a element in css by it's content
Html5
<h2 class="YourClass"> My text </h2>
Css
.YourClass {
color:red
}
For what i understood is that you want to style h2. I dont know why do you want to use javascript instead of css3.
Can you show us the div which the h2 is in? You can use a path selector.
<div>
<nav>
<h2>My Text</h2>
</nav>
<div>
div nav h2 {
color:red
}
I'm afraid there isn't any way of doing this using just css. You can of course do this with javascript but since it's not an option for you then I suggest creating and adding classes according to the text will somewhat closer to what you expecting.
You could use the first line nth selector -
h2::first-line {
color: red;
}
More info on nth selectors here - https://css-tricks.com/a-call-for-nth-everything/