css 制作按钮
基础按钮样式
创建一个简单的按钮,使用CSS设置背景色、边框、内边距和圆角:
.button {
background-color: #4CAF50; /* 绿色背景 */
border: none; /* 无边框 */
color: white; /* 白色文字 */
padding: 15px 32px; /* 内边距 */
text-align: center; /* 文字居中 */
text-decoration: none; /* 无下划线 */
display: inline-block; /* 行内块元素 */
font-size: 16px; /* 字体大小 */
margin: 4px 2px; /* 外边距 */
cursor: pointer; /* 鼠标指针样式 */
border-radius: 8px; /* 圆角 */
}
悬停效果
添加鼠标悬停时的交互效果:
.button:hover {
background-color: #45a049; /* 悬停时更深的绿色 */
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2); /* 添加阴影 */
}
点击效果
为按钮添加按下时的效果:

.button:active {
background-color: #3e8e41; /* 点击时更深的绿色 */
transform: translateY(2px); /* 向下移动2像素 */
}
禁用状态
设置禁用状态的样式:
.button:disabled {
opacity: 0.6; /* 降低不透明度 */
cursor: not-allowed; /* 改变鼠标指针 */
}
渐变按钮
使用CSS渐变创建更现代的按钮:

.gradient-button {
background: linear-gradient(to right, #4CAF50, #2E8B57); /* 从左到右的渐变 */
color: white;
padding: 15px 32px;
border: none;
border-radius: 8px;
font-size: 16px;
cursor: pointer;
}
边框按钮
创建带边框的按钮样式:
.border-button {
background-color: transparent;
color: #4CAF50;
padding: 14px 30px;
border: 2px solid #4CAF50;
border-radius: 8px;
font-size: 16px;
cursor: pointer;
transition: all 0.3s; /* 添加过渡效果 */
}
.border-button:hover {
background-color: #4CAF50;
color: white;
}
图标按钮
在按钮中添加图标:
.icon-button {
background-color: #4CAF50;
color: white;
padding: 12px 24px 12px 40px;
border: none;
border-radius: 8px;
font-size: 16px;
cursor: pointer;
position: relative;
}
.icon-button::before {
content: "→";
position: absolute;
left: 15px;
top: 50%;
transform: translateY(-50%);
}
阴影按钮
添加更明显的阴影效果:
.shadow-button {
background-color: #4CAF50;
color: white;
padding: 15px 32px;
border: none;
border-radius: 8px;
font-size: 16px;
cursor: pointer;
box-shadow: 0 4px 12px rgba(46, 139, 87, 0.5);
transition: box-shadow 0.3s;
}
.shadow-button:hover {
box-shadow: 0 6px 16px rgba(46, 139, 87, 0.6);
}






