Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pseudo Element Hover and text-decoration

Given the following html:

<a href="#"><span data-icon="✪"></span>A Link</a>

Is it possible to change the text-decoration (to none) on hover? The color will change, the text-decoration will not.

CSS:

a { text-decoration: none; }
a:hover{ text-decoration: underline; }
a:hover [data-icon]:before{
  /*Doesn't work*/
  text-decoration: none;

  /*Works*/
  color: red;
}

jsfiddle

like image 240
Mike Henderson Avatar asked Jan 12 '23 22:01

Mike Henderson


1 Answers

As I explained in this other answer, you can use display: inline-block to prevent text-decoration set to an ancestor from propagating to your element:

a {
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
}
a > [data-icon]:before {
  display: inline-block; /* Avoid text-decoration propagation from `a` */
  content: attr(data-icon);
}
a:hover > [data-icon]:before {
  color: red;
}
<a href="#"><span data-icon="✪"></span>A Link</a>
like image 56
Oriol Avatar answered Jan 16 '23 01:01

Oriol