0

I am appending a Glyphicon to an element using a pseudo element. See this JsFiddle - hover the headline and notice the icon. I would like the icon itself to become transparent inside the white circle...to cut through the background so see the color beneath. I can't just add certain color...I need more a flexible solution. I'm also aware of background-clip and mix-blend-mode mentioned here, but to my knowledge they don't seem to work on pseudo icons.

I tried with:

h1:hover::after {
  background-color: #fff;
  color: transparent;
}

And it does make the icon transparent, but in a way so that it disappears completely.

Markup:

<h1>This is a long headline. An icon is appended automatically</h1>

CSS:

h1 {
  color: #fff;
  display: inline-block;
  padding-right: 30px;
}

h1::after {
  color: #fff;
  border: 1px solid #fff;
  top: -3px;
  left: 8px;
  border-radius: 50%;
  padding: 4px 0;
  display: inline-block;
  width: 30px;
  position: relative;
  height: 30px;
  text-align: center;
  margin-right: -30px;
  font-family: 'Glyphicons Halflings';
  font-size: 18px;
  font-weight: 700;
  line-height: 1;
  content: "\e258";
}

h1:hover::after {
  background-color: #fff;
  color: transparent;
}

Can it be done using css only and no extra markup?

Meek
  • 3,086
  • 9
  • 38
  • 64

1 Answers1

0

Use something else like FontAwesome where you will find both icon ready to use

h1 {
  color: #fff;
  display: inline-block;
  padding-right: 30px;
}

h1::after {
  color: #fff;
  display: inline-block;
  font-family: "Font Awesome 5 Free";
  font-size: 28px;
  font-weight: 700;
  content: '\f054';
  line-height: 34px;
  vertical-align: middle;
  margin-left: 10px;
  border: 2px solid;
  border-radius: 50%;
  padding: 0 8px;
}

h1:hover::after {
  content: '\f138';
  font-size: 34px;
  margin-left: 5px;
  border: none;
  padding: 0;
}

body {
  background: red;
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css">
<h1>This is a long headline. An icon is appended automatically</h1>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
  • Thank you. I don't mean to be ungrateful. Your solution works fine, but I was looking for a more generic solution, so that I don't have to introdure a new font icon set. – Meek Jan 11 '21 at 07:35