css制作加号按钮
使用伪元素创建加号按钮
通过CSS的::before和::after伪元素绘制横竖两条线,组合成加号形状。核心代码示例:
.plus-btn {
width: 40px;
height: 40px;
position: relative;
background: #3498db;
border-radius: 4px;
}
.plus-btn::before,
.plus-btn::after {
content: "";
position: absolute;
background: white;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.plus-btn::before {
width: 60%;
height: 4px;
}
.plus-btn::after {
width: 4px;
height: 60%;
}
使用边框实现加号效果
利用元素的边框属性直接构建加号,减少DOM节点:
.plus-icon {
width: 20px;
height: 20px;
position: relative;
}
.plus-icon:before {
content: "";
position: absolute;
top: 0;
left: 50%;
width: 0;
height: 100%;
border-left: 2px solid black;
}
.plus-icon:after {
content: "";
position: absolute;
top: 50%;
left: 0;
width: 100%;
height: 0;
border-top: 2px solid black;
}
SVG方案实现矢量加号
使用内联SVG确保在任何分辨率下清晰显示:
<button class="svg-plus">
<svg viewBox="0 0 24 24" width="24" height="24">
<path d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z" fill="currentColor"/>
</svg>
</button>
配套CSS控制样式:
.svg-plus {
background: none;
border: 2px solid #2ecc71;
border-radius: 50%;
padding: 8px;
color: #2ecc71;
}
纯Unicode字符方案
直接使用Unicode加号字符配合样式修饰:
<button class="unicode-plus">+</button>
样式优化代码:
.unicode-plus {
width: 36px;
height: 36px;
font-size: 24px;
line-height: 1;
background: #e74c3c;
color: white;
border: none;
border-radius: 50%;
text-align: center;
cursor: pointer;
}
交互动画增强
为加号按钮添加悬停和点击动画效果:

.animated-plus {
transition: all 0.3s ease;
}
.animated-plus:hover {
transform: scale(1.1);
box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}
.animated-plus:active {
transform: scale(0.95);
}






