CSS 按钮
学习如何使用 CSS 设置按钮样式。
基本按钮样式
实例
.button {  background-color: #4CAF50; /* Green */  border: none;  color: white;  padding: 15px 32px;  text-align: center;  text-decoration: none;  display: inline-block;  font-size: 16px;}按钮颜色
请使用 background-color 属性更改按钮的背景色:
实例
.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;}请使用 padding 属性更改按钮的内边距:
实例
.button1 {padding: 10px 24px;}.button2 {padding: 12px 28px;}.button3 {padding: 14px 40px;}.button4 {padding: 32px 16px;}.button5 {padding: 16px;}圆角按钮
请使用 border-radius 属性为按钮添加圆角:
实例
.button1 {border-radius: 2px;}.button2 {border-radius: 4px;}.button3 {border-radius: 8px;}.button4 {border-radius: 12px;}.button5 {border-radius: 50%;}彩色的按钮边框
请使用 border 属性为按钮添加彩色边框:
实例
.button1 {  background-color: white;  color: black;  border: 2px solid #4CAF50; /* 绿色 */}...可悬停按钮
当鼠标移动到按钮上方时,使用 :hover 选择器可更改按钮的样式。
提示:请使用 transition-duration 属性来确定“悬停”效果的速度:
实例
.button {  transition-duration: 0.4s;}.button:hover {  background-color: #4CAF50; /* Green */  color: white;}...阴影按钮
请使用 box-shadow 属性为按钮添加阴影:
实例
.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);}禁用的按钮
请使用 opacity 属性为按钮添加透明度(创建“禁用”外观)。
提示:您还可以添加带有 "not-allowed" 值的 cursor 属性,当您将鼠标悬停在按钮上时,该属性会显示 "no parking sign"(禁停标志):
实例
.disabled {  opacity: 0.6;  cursor: not-allowed;}按钮宽度
默认情况下,按钮的大小取决于其文本内容(与内容的宽度一样)。请使用 width 属性来更改按钮的宽度:
实例
.button1 {width: 250px;}.button2 {width: 50%;}.button3 {width: 100%;}