Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selector for just the text of a div? [duplicate]

I am trying to have the cursor change when the user hovers over the text of my div rather than the div itself. I currently have :

div {
  width: 500px;
  height: 500px;
  background-color: green;
}
div:hover {
  cursor: pointer
}
<div>
  Hello
</div>

Is there a way for me to somehow select just the text of a div and apply a hover effect on just the text itself.

like image 271
Pipeline Avatar asked Jan 02 '16 01:01

Pipeline


2 Answers

Since you can't select text nodes in the CSS, you would have to wrap it in order to select it.

div {
  width: 500px;
  height: 500px;
  background-color: green;
}
div span:hover {
  cursor: pointer;
}
<div>
  <span>Hello</span>
</div>

However, if you want a work-around, you could add the text via a pseudo element like this:

div {
  width: 500px;
  height: 500px;
  background-color: green;
}
div:after {
  content: 'Hello';
  cursor: pointer;
}
<div></div>
like image 67
Josh Crozier Avatar answered Nov 01 '22 07:11

Josh Crozier


div {
  width: 500px;
  height: 500px;
  background-color: green;
}
div p {
  cursor: pointer;
}
<div>
<p>Hello</p>
</div>
like image 44
Pooja Khokhani Avatar answered Nov 01 '22 05:11

Pooja Khokhani