CSS 按钮
学习如何使用 CSS 设置按钮样式。
基本按钮样式
实例
<!DOCTYPE html><html><head><style>.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;}</style></head><body><h1>CSS 按钮</h1><button>默认按钮</button><a href="#" class="button">链接按钮</a><button class="button">按钮</button><input type="button" class="button" value="输入按钮"></body></html>
按钮颜色
请使用 background-color 属性更改按钮的背景色:
实例
<!DOCTYPE html><html><head><style>.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;}.button2 {background-color: #008CBA;} /* 蓝色 */.button3 {background-color: #f44336;} /* 黑色 */.button4 {background-color: #e7e7e7; color: black;} /* 灰色 */.button5 {background-color: #555555;} /* 黑色 */</style></head><body><h1>按钮颜色</h1><p>通过 background-color 属性改变按钮的背景色:</p><button class="button">绿色</button><button class="button button2">蓝色</button><button class="button button3">红色</button><button class="button button4">灰色</button><button class="button button5">黑色</button></body></html>
按钮尺寸
请使用 font-size 属性更改按钮的字体大小:
实例
<!DOCTYPE html><html><head><style>.button {background-color: #4CAF50; /* 绿色 */border: none;color: white;padding: 15px 32px;text-align: center;text-decoration: none;display: inline-block;margin: 4px 2px;cursor: pointer;}.button1 {font-size: 10px;}.button2 {font-size: 12px;}.button3 {font-size: 16px;}.button4 {font-size: 20px;}.button5 {font-size: 24px;}</style></head><body><h1>按钮大小</h1><p>通过 font-size 属性改变按钮的字体大小:</p><button class="button button1">10px</button><button class="button button2">12px</button><button class="button button3">16px</button><button class="button button4">20px</button><button class="button button5">24px</button></body></html>
请使用 padding 属性更改按钮的内边距:
实例
<!DOCTYPE html><html><head><style>.button {background-color: #4CAF50; /* 绿色 */border: none;color: white;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;margin: 4px 2px;cursor: pointer;}.button1 {padding: 10px 24px;}.button2 {padding: 12px 28px;}.button3 {padding: 14px 40px;}.button4 {padding: 32px 16px;}.button5 {padding: 16px;}</style></head><body><h1>按钮大小</h1><p>通过 padding 属性改变按钮的内边距:</p><button class="button button1">10px 24px</button><button class="button button2">12px 28px</button><button class="button button3">14px 40px</button><button class="button button4">32px 16px</button><button class="button button5">16px</button></body></html>
圆角按钮
请使用 border-radius 属性为按钮添加圆角:
<!DOCTYPE html><html><head><style>.button {background-color: #4CAF50; /* 绿色 */border: none;color: white;padding: 20px;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;margin: 4px 2px;cursor: pointer;}.button1 {border-radius: 2px;}.button2 {border-radius: 4px;}.button3 {border-radius: 8px;}.button4 {border-radius: 12px;}.button5 {border-radius: 50%;}</style></head><body><h1>圆角按钮</h1><p>通过 border-radius 属性为按钮添加圆角:</p><button class="button button1">2px</button><button class="button button2">4px</button><button class="button button3">8px</button><button class="button button4">12px</button><button class="button button5">50%</button></body></html>
彩色的按钮边框
请使用 border 属性为按钮添加彩色边框:
<!DOCTYPE html><html><head><style>.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;}.button1 {background-color: white;color: black;border: 2px solid #4CAF50;}.button2 {background-color: white;color: black;border: 2px solid #008CBA;}.button3 {background-color: white;color: black;border: 2px solid #f44336;}.button4 {background-color: white;color: black;border: 2px solid #e7e7e7;}.button5 {background-color: white;color: black;border: 2px solid #555555;}</style></head><body><h1>有颜色的按钮边框</h1><p>请使用 border 属性为按钮添加边框:</p><button class="button button1">绿色</button><button class="button button2">蓝色</button><button class="button button3">黑色</button><button class="button button4">灰色</button><button class="button button5">黑色</button></body></html>
可悬停按钮
当鼠标移动到按钮上方时,使用 :hover 选择器可更改按钮的样式。
提示:请使用 transition-duration 属性来确定“悬停”效果的速度:
实例
<!DOCTYPE html><html><head><style>.button {background-color: #4CAF50; /* Green */border: none;color: white;padding: 16px 32px;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;margin: 4px 2px;transition-duration: 0.4s;cursor: pointer;}.button1 {background-color: white;color: black;border: 2px solid #4CAF50;}.button1:hover {background-color: #4CAF50;color: white;}.button2 {background-color: white;color: black;border: 2px solid #008CBA;}.button2:hover {background-color: #008CBA;color: white;}.button3 {background-color: white;color: black;border: 2px solid #f44336;}.button3:hover {background-color: #f44336;color: white;}.button4 {background-color: white;color: black;border: 2px solid #e7e7e7;}.button4:hover {background-color: #e7e7e7;}.button5 {background-color: white;color: black;border: 2px solid #555555;}.button5:hover {background-color: #555555;color: white;}</style></head><body><h1>可悬停的按钮</h1><p>使用 :hover 选择器在鼠标移动到按钮上时改变其样式。</p><p><b>提示:</b>请使用 transition-duration 属性来确定悬停效果的速度:</p><button class="button button1">绿色</button><button class="button button2">蓝色</button><button class="button button3">红色</button><button class="button button4">灰色</button><button class="button button5">黑色</button></body></html>
阴影按钮
请使用 box-shadow 属性为按钮添加阴影:
<!DOCTYPE html><html><head><style>.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;-webkit-transition-duration: 0.4s; /* Safari */transition-duration: 0.4s;}.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);}</style></head><body><h1>阴影按钮</h1><p>使用 box-shadow 属性为按钮添加阴影:</p><button class="button button1">阴影按钮</button><button class="button button2">悬停时的阴影</button></body></html>
禁用的按钮
请使用 opacity 属性为按钮添加透明度(创建“禁用”外观)。
提示:您还可以添加带有 "not-allowed" 值的 cursor 属性,当您将鼠标悬停在按钮上时,该属性会显示 "no parking sign"(禁停标志):
<!DOCTYPE html><html><head><style>.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;}.disabled {opacity: 0.6;cursor: not-allowed;}</style></head><body><h1>被禁用的按钮</h1><p>使用 opacity 属性向按钮添加一定的透明度(使它看上去已被禁用)</p><button class="button">正常按钮</button><button class="button disabled">被禁用的按钮</button></body></html>
按钮宽度
默认情况下,按钮的大小取决于其文本内容(与内容的宽度一样)。请使用 width 属性来更改按钮的宽度:
<!DOCTYPE html><html><head><style>.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;}.button1 {width: 250px;}.button2 {width: 50%;}.button3 {width: 100%;}</style></head><body><h1>按钮宽度</h1><p>使用 width 属性来改变按钮的宽度:</p><p><b>提示:</b>请使用像素设置固定宽度,并为响应式按钮使用百分百(例如其父元素的 50%)。请调整窗口大小来查看效果。</p><button class="button button1">250px</button><br><button class="button button2">50%</button><br><button class="button button3">100%</button></body></html>
按钮分组
删除外边距并向每个按钮添加 float:left,来创建按钮组:
<!DOCTYPE html><html><head><style>.btn-group .button {background-color: #4CAF50; /* 绿色 */border: none;color: white;padding: 15px 32px;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;cursor: pointer;float: left;}.btn-group .button:hover {background-color: #3e8e41;}</style></head><body><h1>按钮组</h1><p>删除外边距并浮动按钮,来创建一个按钮组:</p><div class="btn-group"><button class="button">Button</button><button class="button">Button</button><button class="button">Button</button><button class="button">Button</button></div><p style="clear:both"><br>请记得之后清除浮动,否则这个 p 元素会向按钮浮动。</p></body></html>
带边框的按钮组
使用 border 属性来创建带边框的按钮组:
<!DOCTYPE html><html><head><style>.btn-group .button {background-color: #4CAF50; /* 绿色 */border: 1px solid green;color: white;padding: 15px 32px;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;cursor: pointer;float: left;}.btn-group .button:not(:last-child) {border-right: none; /* 阻止双边框 */}.btn-group .button:hover {background-color: #3e8e41;}</style></head><body><h1>带边框的按钮组</h1><p>添加边框,来创建带按钮的按钮组:</p><div class="btn-group"><button class="button">Button</button><button class="button">Button</button><button class="button">Button</button><button class="button">Button</button></div><p style="clear:both"><br>请记得之后清除浮动,否则这个 p 元素会向按钮浮动。</p></body></html>
垂直按钮组
使用 display:block 取代 float:left 将按钮上下分组,而不是并排:
<!DOCTYPE html><html><head><style>.btn-group .button {background-color: #4CAF50; /* 绿色 */border: 1px solid green;color: white;padding: 15px 32px;text-align: center;text-decoration: none;font-size: 16px;cursor: pointer;width: 150px;display: block;}.btn-group .button:not(:last-child) {border-bottom: none; /* 阻止双边框 */}.btn-group .button:hover {background-color: #3e8e41;}</style></head><body><h1>垂直按钮分组</h1><div class="btn-group"><button class="button">按钮</button><button class="button">按钮</button><button class="button">按钮</button><button class="button">按钮</button></div></body></html>
图像上的按钮
<!DOCTYPE html><html><head><style>.container {position: relative;width: 100%;max-width: 400px;}.container img {width: 100%;height: auto;}.container .btn {position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);-ms-transform: translate(-50%, -50%);background-color: #f1f1f1;color: black;font-size: 16px;padding: 16px 30px;border: none;cursor: pointer;border-radius: 5px;text-align: center;}.container .btn:hover {background-color: black;color: white;}</style></head><body><h2>图片上的按钮</h2><div class="container"><img src="/znadmin/md/1269/0.jpg" alt="Snow" style="width:100%"><button class="btn">Button</button></div></body></html>