Bootstrap 3.3.7中如何用CSS实现伪元素Glyphicon hover镂空透底效果
实现方案
可以仅通过CSS实现,无需修改原有HTML标记结构,核心是拆分伪元素职责配合*mix-blend-mode: destination-out*实现真正的挖洞效果。
修改后完整CSS代码
原有HTML保持不变:
<h1>This is a long headline. An icon is appended automatically</h1>
CSS调整如下:
h1 { color: #fff; display: inline-block; padding-right: 30px; position: relative; /* 创建独立混合上下文,避免混合模式影响页面其他元素 */ isolation: isolate; } /* 原圆形边框、背景逻辑移至::after */ h1::after { top: -3px; left: 8px; border: 1px solid #fff; border-radius: 50%; display: inline-block; width: 30px; position: relative; height: 30px; margin-right: -30px; content: ""; } /* 图标逻辑移至::before */ h1::before { color: #fff; top: -3px; left: 8px; width: 30px; position: absolute; height: 30px; text-align: center; padding: 4px 0; font-family: 'Glyphicons Halflings'; font-size: 18px; font-weight: 700; line-height: 1; content: "\e258"; } /* hover状态调整 */ h1:hover::after { background-color: #fff; } h1:hover::before { /* 关键:开启destination-out混合模式,擦除下方白色背景 */ mix-blend-mode: destination-out; /* 仅需保证颜色不透明即可,不影响最终透明效果 */ color: #000; }
核心说明
- 未修改原有HTML标记结构,仅通过CSS调整伪元素职责,将圆形容器和图标拆分为两个独立伪元素层
mix-blend-mode: destination-out会让图标的不透明区域直接擦除下方的白色背景,实现真正的透明挖洞,无需设置固定背景色,可自适应透出任意下方内容- 未hover状态的视觉效果和原有代码完全一致,不会改变默认外观
内容的提问来源于stack exchange,提问作者Meek
相关产品推荐
相关产品推荐

