html
css
/ 通用按钮样式 /
button {
background-color: 4CAF50; / 背景色 /
color: white; / 文字颜色 /
padding: 12px 24px; / 内边距 /
border: none; / 移除默认边框 /
border-radius: 4px; / 圆角 /
cursor: pointer; / 鼠标手势 /
font-size: 16px; / 字体大小 /
transition: 0.3s; / 过渡动画 /
/ 悬停效果 /
button:hover {
background-color: 45a049;
/ 按下效果 /
button:active {
transform: scale(0.98); / 点击时轻微缩小 /
/ 禁用状态 /
button:disabled {
background-color: cccccc;
cursor: not-allowed;
css
gradient-btn {
background: linear-gradient(45deg, ff6b6b, ff8e53);
color: white;
padding: 12px 30px;
border-radius: 25px;
border: none;
box-shadow: 0 4px 15px rgba(0,0,0,0.2);
gradient-btn:hover {
background: linear-gradient(45deg, ff8e53, ff6b6b);
css
border-btn {
background: transparent;
border: 2px solid 3498db;
color: 3498db;
padding: 10px 20px;
transition: all 0.3s;
border-btn:hover {
background: 3498db;
color: white;
html
下载
icon-btn {
background: 2c3e50;
padding: 10px 20px;
color: white;
icon-btn i {
margin-right: 8px;
css
/ 移动端适配 /
@media (max-width: 768px) {
button {
width: 100%; / 全宽按钮 /
padding: 15px; / 增大触摸区域 /
font-size: 18px;
/ 按钮组布局 /
button-group {
display: flex;
gap: 10px; / 按钮间距 /
justify-content: center;
html
点击次数: 0
let count = 0;
document.getElementById('myBtn').addEventListener('click', function {
count++;
document.getElementById('counter').textContent = count;
});
1. 可访问性:添加 `aria-label` 属性(图标按钮适用)
2. 禁用状态:使用 `disabled` 属性而不是 CSS 类
3. 视觉反馈:始终提供 `:hover` 和 `:active` 状态
4. 文件规范:图标按钮推荐使用 SVG 或 icon-font
5. 性能:避免使用大尺寸图片作为按钮背景
可以根据具体需求组合使用这些代码片段,并通过 CSS 自定义属性(CSS Variables)实现主题化控制。
版权声明: 知妳网保留所有权利,部分内容为网络收集,如有侵权,请联系QQ793061840删除,添加请注明来意。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态
