css制作加号
使用 CSS 制作加号
方法一:使用伪元素
通过 ::before 和 ::after 伪元素创建水平和垂直线条,组合成加号。
.plus-icon {
position: relative;
width: 20px;
height: 20px;
}
.plus-icon::before,
.plus-icon::after {
content: "";
position: absolute;
background-color: #000;
}
.plus-icon::before {
width: 100%;
height: 2px;
top: 50%;
transform: translateY(-50%);
}
.plus-icon::after {
width: 2px;
height: 100%;
left: 50%;
transform: translateX(-50%);
}
方法二:使用边框
利用元素的边框和 transform 旋转实现加号效果。

.plus-box {
width: 20px;
height: 20px;
position: relative;
}
.plus-box::before {
content: "";
position: absolute;
top: 0;
left: 50%;
width: 2px;
height: 100%;
background: #000;
transform: translateX(-50%);
}
.plus-box::after {
content: "";
position: absolute;
top: 50%;
left: 0;
width: 100%;
height: 2px;
background: #000;
transform: translateY(-50%);
}
方法三:使用 Flex 布局
通过 Flex 布局和子元素组合实现加号。

.plus-container {
display: flex;
justify-content: center;
align-items: center;
width: 20px;
height: 20px;
}
.plus-horizontal,
.plus-vertical {
background-color: #000;
}
.plus-horizontal {
width: 100%;
height: 2px;
}
.plus-vertical {
width: 2px;
height: 100%;
position: absolute;
}
方法四:使用 Unicode 字符
直接使用 Unicode 加号字符,通过 CSS 调整样式。
.plus-symbol {
font-size: 24px;
line-height: 1;
color: #000;
}
<span class="plus-symbol">+</span>
方法五:使用 SVG
通过内联 SVG 实现加号,灵活性高且可缩放。
<svg class="plus-svg" width="20" height="20" viewBox="0 0 20 20">
<line x1="0" y1="10" x2="20" y2="10" stroke="#000" stroke-width="2" />
<line x1="10" y1="0" x2="10" y2="20" stroke="#000" stroke-width="2" />
</svg>
样式调整建议
- 修改
background-color或stroke可改变加号颜色。 - 调整
width和height可改变加号大小。 - 通过
transform可旋转或缩放加号。
以上方法可根据实际需求选择,伪元素方案适合纯 CSS 场景,SVG 方案适合需要动态调整的场景。






