css怎么制作加号
使用伪元素创建加号
通过 ::before 和 ::after 伪元素生成横竖两条线,组合成加号形状。核心是利用绝对定位和 transform 居中。
.plus-icon {
position: relative;
width: 24px;
height: 24px;
}
.plus-icon::before,
.plus-icon::after {
content: "";
position: absolute;
background-color: black;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.plus-icon::before {
width: 16px;
height: 2px;
}
.plus-icon::after {
width: 2px;
height: 16px;
}
使用边框实现加号
通过元素的边框和旋转特性快速生成加号。这种方法适合简单的加号图标。
.plus-border {
width: 20px;
height: 20px;
position: relative;
}
.plus-border::before {
content: "";
position: absolute;
border-left: 2px solid black;
height: 100%;
left: 50%;
transform: translateX(-50%);
}
.plus-border::after {
content: "";
position: absolute;
border-top: 2px solid black;
width: 100%;
top: 50%;
transform: translateY(-50%);
}
使用背景渐变生成加号
CSS 线性渐变可以绘制加号的两条线,适合不需要伪元素的场景。
.plus-gradient {
width: 20px;
height: 20px;
background-image:
linear-gradient(to bottom, black, black),
linear-gradient(to right, black, black);
background-size:
2px 16px,
16px 2px;
background-position: center;
background-repeat: no-repeat;
}
SVG 实现加号
使用内联 SVG 可以创建精确控制的加号,支持颜色和大小调整。
<svg class="plus-svg" width="24" height="24" viewBox="0 0 24 24">
<path d="M12 4V20M4 12H20" stroke="black" stroke-width="2"/>
</svg>
Unicode 字符直接显示
最简单的实现方式,直接使用 Unicode 加号字符。

<span class="plus-char">+</span>
.plus-char {
font-size: 24px;
line-height: 1;
}
每种方法适用于不同场景:伪元素适合自定义样式,Unicode 适合快速实现,SVG 适合需要缩放的情况。可根据项目需求选择合适方案。






