纯CSS实现中间透明的关闭图标

一、背景和意义

关闭图标是网页中最常见的图标之一。有时候为了美观,需要把关闭图标中间的叉叉做做透明的:

让UI设计师弄一个中间透明的png图片是一种解决方案,但相比之下纯CSS实现的关闭图标占用流量更少、加载速度快、且方便修改图标的颜色。本文给出纯CSS实现中间透明的关闭图标的简单示例。

二、代码示例

2.1 创建背景图

首先,为了方便看出中间透明的效果,先弄一张windows操作系统的经典背景图:

html 复制代码
<style>
    .origin {
        width: 320px;
        height: 240px;
        background-size: cover;
        background-image: url("https://5b0988e595225.cdn.sohucs.com/images/20171010/2ab85e31fee24a36b3e9bb79d71885b2.jpeg");
    }
</style>
<div class="origin">
</div>

效果如下:

2.2 用svg画叉叉图标

用svg画叉叉还比较简单,先从(0, 0)到(100, 100)两个坐标之间画一条从左上到右下的线,再从(0, 100)到(100, 0)两个坐标之间画一条从左下到右上的线:

html 复制代码
<style>
    .origin {
        width: 320px;
        height: 240px;
        background-size: cover;
        background-image: url("https://5b0988e595225.cdn.sohucs.com/images/20171010/2ab85e31fee24a36b3e9bb79d71885b2.jpeg");
    }
    .line {
        stroke: #888;
        stroke-width: 17;
    }
</style>
<div class="origin">
    <svg xmlns="https://www.w3.org/2000/svg" viewBox="0 0 100 100">
        <line class="line" x1="0" y1="0" x2="100" y2="100" />
        <line class="line" x1="0" y1="100" x2="100" y2="0" />
    </svg>
</div>

展示效果为:

2.3 调整叉叉的大小和背景

给svg外面包一层a标签,控制其大小,并使用白色做为背景色:

html 复制代码
<style>
    .origin {
        width: 320px;
        height: 240px;
        background-size: cover;
        background-image: url("https://5b0988e595225.cdn.sohucs.com/images/20171010/2ab85e31fee24a36b3e9bb79d71885b2.jpeg");
    }
    .close {
        display: block;
        background-color: #fff;
        padding: 0.75rem;
        width: 2rem;
        border-radius: 50%;
    }
    .line {
        stroke: #888;
        stroke-width: 17;
    }
</style>
<div class="origin">
    <a class="close" href="javascript:void(0)">
        <svg xmlns="https://www.w3.org/2000/svg" viewBox="0 0 100 100">
            <line class="line" x1="0" y1="0" x2="100" y2="100" />
            <line class="line" x1="0" y1="100" x2="100" y2="0" />
        </svg>
    </a>
</div>

为了方便对比,这里特意地将图标做得大一些,当然在实际应用中一般不做那么大。展示效果如下:

2.4 将图标中间叉叉变透明

为图标增加mix-blend-mode属性,取值hard-light,可以将叉叉变成透明,修改后的代码如下:

html 复制代码
<style>
    .origin {
        width: 320px;
        height: 240px;
        background-size: cover;
        background-image: url("https://5b0988e595225.cdn.sohucs.com/images/20171010/2ab85e31fee24a36b3e9bb79d71885b2.jpeg");
    }
    .close {
        mix-blend-mode: hard-light;
        display: block;
        background-color: #fff;
        padding: 0.75rem;
        width: 2rem;
        border-radius: 50%;
    }
    .line {
        stroke: #888;
        stroke-width: 17;
    }
</style>
<div class="origin">
    <a class="close" href="javascript:void(0)">
        <svg xmlns="https://www.w3.org/2000/svg" viewBox="0 0 100 100">
            <line class="line" x1="0" y1="0" x2="100" y2="100" />
            <line class="line" x1="0" y1="100" x2="100" y2="0" />
        </svg>
    </a>
</div>

运行效果为:

2.5 其他优化

关闭图标一般放右上角,给图标加一个float: right可以将其挪到右上角,另外图标内的叉叉四个角太尖锐不好看,增加border-radius: 0.15rem可以变圆润一点:

html 复制代码
<style>
    .origin {
        width: 320px;
        height: 240px;
        background-size: cover;
        background-image: url("https://5b0988e595225.cdn.sohucs.com/images/20171010/2ab85e31fee24a36b3e9bb79d71885b2.jpeg");
    }
    .close {
        float: right;
        mix-blend-mode: hard-light;
        display: block;
        background-color: #fff;
        padding: 0.75rem;
        width: 2rem;
        border-radius: 50%;
    }
    .close svg {
        border-radius: 0.15rem;
    }
    .line {
        stroke: #888;
        stroke-width: 17;
    }
</style>
<div class="origin">
    <a class="close" href="javascript:void(0)">
        <svg xmlns="https://www.w3.org/2000/svg" viewBox="0 0 100 100">
            <line class="line" x1="0" y1="0" x2="100" y2="100" />
            <line class="line" x1="0" y1="100" x2="100" y2="0" />
        </svg>
    </a>
</div>

展示效果如下:

相关推荐
张3蜂1 天前
Python 四大 Web 框架对比解析:FastAPI、Django、Flask 与 Tornado
前端·python·fastapi
南风知我意9571 天前
【前端面试5】手写Function原型方法
前端·面试·职场和发展
qq_12498707531 天前
基于Java Web的城市花园小区维修管理系统的设计与实现(源码+论文+部署+安装)
java·开发语言·前端·spring boot·spring·毕业设计·计算机毕业设计
小安驾到1 天前
【前端的坑】vxe-grid表格tooltip提示框不显示bug
前端·vue.js
去码头整点薯条981 天前
python第五次作业
linux·前端·python
沐墨染1 天前
Vue实战:自动化研判报告组件的设计与实现
前端·javascript·信息可视化·数据分析·自动化·vue
局外人LZ1 天前
Uniapp脚手架项目搭建,uniapp+vue3+uView pro+vite+pinia+sass
前端·uni-app·sass
爱上妖精的尾巴1 天前
8-5 WPS JS宏 match、search、replace、split支持正则表达式的字符串函数
开发语言·前端·javascript·wps·jsa
为什么不问问神奇的海螺呢丶1 天前
n9e categraf redis监控配置
前端·redis·bootstrap
云飞云共享云桌面1 天前
推荐一些适合10个SolidWorks设计共享算力的服务器硬件配置
运维·服务器·前端·数据库·人工智能