css制作加号按钮
使用伪元素创建加号按钮
通过CSS伪元素::before和::after可以轻松实现加号效果。创建一个正方形按钮,并用伪元素绘制横竖两条线:
.plus-button {
width: 40px;
height: 40px;
position: relative;
background-color: #3498db;
border-radius: 4px;
cursor: pointer;
}
.plus-button::before,
.plus-button::after {
content: "";
position: absolute;
background-color: white;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.plus-button::before {
width: 60%;
height: 2px;
}
.plus-button::after {
width: 2px;
height: 60%;
}
使用边框实现加号效果
利用CSS边框属性直接绘制加号形状,适合需要简约风格的场景:
.add-btn {
width: 24px;
height: 24px;
background: none;
border: 2px solid #2ecc71;
position: relative;
}
.add-btn::before {
content: "";
position: absolute;
width: 14px;
height: 2px;
background: #2ecc71;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.add-btn::after {
content: "";
position: absolute;
width: 2px;
height: 14px;
background: #2ecc71;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
SVG加号按钮
使用内联SVG创建分辨率无关的加号图标,适合需要缩放而不失真的场景:
<button class="svg-plus">
<svg viewBox="0 0 24 24" width="24" height="24">
<path d="M12 4v16m8-8H4" stroke="currentColor" stroke-width="2"/>
</svg>
</button>
配套CSS样式:
.svg-plus {
width: 40px;
height: 40px;
background: #e74c3c;
border: none;
border-radius: 50%;
color: white;
padding: 8px;
cursor: pointer;
}
动画效果增强
为加号按钮添加悬停动画提升交互体验:

.animated-plus {
width: 50px;
height: 50px;
background: #9b59b6;
border: none;
border-radius: 50%;
position: relative;
transition: transform 0.3s;
}
.animated-plus::before,
.animated-plus::after {
content: "";
position: absolute;
background: white;
transition: all 0.3s;
}
.animated-plus::before {
width: 24px;
height: 4px;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.animated-plus::after {
width: 4px;
height: 24px;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.animated-plus:hover {
transform: rotate(90deg);
}
.animated-plus:hover::before {
width: 28px;
}
.animated-plus:hover::after {
height: 28px;
}






