CSS按钮是一种通过HTML和CSS创建的用户界面元素,其主要目的是提供直观且易于使用的交互性。其底层原理是通过使用各种样式属性和伪类来实现各种按钮效果。使用CSS可以设置按钮的背景颜色、边框、文字颜色、内边距、对齐方式、装饰和显示类型等属性。
一、基本按钮样式
button 属性可以用来设置CSS基本按钮样式,如:
background-color: #4CAF50; /* Green */
.button {
background-color: #4CAF50; /* Green */
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin:4px 2px;
cursor:pointer;
}
.button {
background-color: #4CAF50; /* Green */
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin:4px 2px;
cursor:pointer;
}
二、按钮颜色
background-colo 属性可用来设置按钮颜色:
.button1 {background-color: #4CAF50;} /* 绿色 */
.button2 {background-color: #008CBA;} /* 蓝色 */
.button3 {background-color: #f44336;} /* 红色 */
.button4 {background-color: #e7e7e7; color: black;} /* 灰色 */
.button5 {background-color: #555555;} /* 黑色 */
.button1 {background-color: #4CAF50;} /* 绿色 */
.button2 {background-color: #008CBA;} /* 蓝色 */
.button3 {background-color: #f44336;} /* 红色 */
.button4 {background-color: #e7e7e7; color: black;} /* 灰色 */
.button5 {background-color: #555555;} /* 黑色 */
.button1 {background-color: #4CAF50;} /* 绿色 */
.button2 {background-color: #008CBA;} /* 蓝色 */
.button3 {background-color: #f44336;} /* 红色 */
.button4 {background-color: #e7e7e7; color: black;} /* 灰色 */
.button5 {background-color: #555555;} /* 黑色 */
三、按钮大小
font-size 属性可用来设置按钮大小:
.button1 {font-size: 10px;}
.button2 {font-size: 12px;}
.button3 {font-size: 16px;}
.button4 {font-size: 20px;}
.button5 {font-size: 24px;}
.button1 {font-size: 10px;}
.button2 {font-size: 12px;}
.button3 {font-size: 16px;}
.button4 {font-size: 20px;}
.button5 {font-size: 24px;}
.button1 {font-size: 10px;}
.button2 {font-size: 12px;}
.button3 {font-size: 16px;}
.button4 {font-size: 20px;}
.button5 {font-size: 24px;}
四、圆角按钮
border-radius 属性可以用来设置圆角按钮:
.button1 {border-radius: 2px;}
.button2 {border-radius: 4px;}
.button3 {border-radius: 8px;}
.button4 {border-radius: 12px;}
.button5 {border-radius: 50%;}
.button1 {border-radius: 2px;}
.button2 {border-radius: 4px;}
.button3 {border-radius: 8px;}
.button4 {border-radius: 12px;}
.button5 {border-radius: 50%;}
.button1 {border-radius: 2px;}
.button2 {border-radius: 4px;}
.button3 {border-radius: 8px;}
.button4 {border-radius: 12px;}
.button5 {border-radius: 50%;}
五、边框颜色
border 属性可用来设置按钮边框颜色:
border: 2px solid #4CAF50; /* Green */
.button1 {
background-color: white;
color: black;
border: 2px solid #4CAF50; /* Green */
}
...
.button1 {
background-color: white;
color: black;
border: 2px solid #4CAF50; /* Green */
}
...
六、按钮阴影
box-shadow 属性可添加按钮阴影:
box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19);
.button1 {
box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
}
.button2:hover {
box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19);
}
.button1 {
box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
}
.button2:hover {
box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19);
}
七、按钮宽度
可以使用width 属性来设置按钮的宽度,如果要设置固定宽度可以使用像素 (px) 为单位,如果要设置响应式的按钮可以设置为百分比:
.button1 {width: 250px;}
.button2 {width: 50%;}
.button3 {width: 100%;}
.button1 {width: 250px;}
.button2 {width: 50%;}
.button3 {width: 100%;}
八、按钮组
float 顺序属性可以设置按钮组:
.button {
float: left;
}
九、禁用按钮
可以使用 opacity 属性为按钮添加透明度 (看起来类似 “disabled” 属性效果)。可以添加 cursor 属性并设置为 “not-allowed” 来设置一个禁用的图片:
.disabled {
opacity: 0.6;
cursor: not-allowed;
}
.disabled {
opacity: 0.6;
cursor: not-allowed;
}