Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
444 views
in Technique[技术] by (71.8m points)

How to make Glyphicon transparent on hover using css (Bootstrap 3.3.7)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...