css 制作按钮样式
基础按钮样式
使用 background-color、color、padding 和 border-radius 创建基础按钮:
.btn {
background-color: #4CAF50;
color: white;
padding: 12px 24px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
悬停效果
通过 :hover 伪类添加交互效果:
.btn:hover {
background-color: #45a049;
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
}
点击状态
使用 :active 模拟按下效果:
.btn:active {
transform: translateY(1px);
box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}
禁用状态
通过 :disabled 设置不可用样式:
.btn:disabled {
opacity: 0.6;
cursor: not-allowed;
}
渐变按钮
使用 linear-gradient 创建渐变背景:
.gradient-btn {
background: linear-gradient(to right, #ff7e5f, #feb47b);
color: white;
border: none;
padding: 12px 24px;
border-radius: 25px;
}
边框按钮
创建带边框的简洁按钮:
.outline-btn {
background-color: transparent;
color: #4CAF50;
border: 2px solid #4CAF50;
padding: 10px 20px;
border-radius: 4px;
}
图标按钮
结合 Font Awesome 或 SVG 图标:
.icon-btn {
padding: 10px 15px 10px 40px;
background-image: url('icon.png');
background-repeat: no-repeat;
background-position: 10px center;
}
动画按钮
添加 CSS 过渡效果:
.animate-btn {
transition: all 0.3s ease;
position: relative;
overflow: hidden;
}
.animate-btn:after {
content: "";
position: absolute;
top: 50%;
left: 50%;
width: 5px;
height: 5px;
background: rgba(255,255,255,0.5);
opacity: 0;
border-radius: 100%;
transform: scale(1, 1) translate(-50%);
transform-origin: 50% 50%;
}
.animate-btn:hover:after {
animation: ripple 1s ease-out;
}
3D 按钮效果
通过阴影创建立体感:

.btn-3d {
box-shadow: 0 6px 0 #2c7a3d, 0 5px 15px rgba(0,0,0,0.2);
position: relative;
top: 0;
}
.btn-3d:active {
box-shadow: 0 2px 0 #2c7a3d, 0 1px 5px rgba(0,0,0,0.2);
top: 4px;
}






