CSS 按钮

学习如何使用 CSS 设置按钮样式。


基本按钮样式

实例
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .button {
  6. background-color: #4CAF50;
  7. border: none;
  8. color: white;
  9. padding: 15px 32px;
  10. text-align: center;
  11. text-decoration: none;
  12. display: inline-block;
  13. font-size: 16px;
  14. margin: 4px 2px;
  15. cursor: pointer;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <h1>CSS 按钮</h1>
  21. <button>默认按钮</button>
  22. <a href="#" class="button">链接按钮</a>
  23. <button class="button">按钮</button>
  24. <input type="button" class="button" value="输入按钮">
  25. </body>
  26. </html>

按钮颜色

请使用 background-color 属性更改按钮的背景色:

实例
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .button {
  6. background-color: #4CAF50; /* 绿色 */
  7. border: none;
  8. color: white;
  9. padding: 15px 32px;
  10. text-align: center;
  11. text-decoration: none;
  12. display: inline-block;
  13. font-size: 16px;
  14. margin: 4px 2px;
  15. cursor: pointer;
  16. }
  17. .button2 {background-color: #008CBA;} /* 蓝色 */
  18. .button3 {background-color: #f44336;} /* 黑色 */
  19. .button4 {background-color: #e7e7e7; color: black;} /* 灰色 */
  20. .button5 {background-color: #555555;} /* 黑色 */
  21. </style>
  22. </head>
  23. <body>
  24. <h1>按钮颜色</h1>
  25. <p>通过 background-color 属性改变按钮的背景色:</p>
  26. <button class="button">绿色</button>
  27. <button class="button button2">蓝色</button>
  28. <button class="button button3">红色</button>
  29. <button class="button button4">灰色</button>
  30. <button class="button button5">黑色</button>
  31. </body>
  32. </html>

按钮尺寸

请使用 font-size 属性更改按钮的字体大小:

实例
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .button {
  6. background-color: #4CAF50; /* 绿色 */
  7. border: none;
  8. color: white;
  9. padding: 15px 32px;
  10. text-align: center;
  11. text-decoration: none;
  12. display: inline-block;
  13. margin: 4px 2px;
  14. cursor: pointer;
  15. }
  16. .button1 {font-size: 10px;}
  17. .button2 {font-size: 12px;}
  18. .button3 {font-size: 16px;}
  19. .button4 {font-size: 20px;}
  20. .button5 {font-size: 24px;}
  21. </style>
  22. </head>
  23. <body>
  24. <h1>按钮大小</h1>
  25. <p>通过 font-size 属性改变按钮的字体大小:</p>
  26. <button class="button button1">10px</button>
  27. <button class="button button2">12px</button>
  28. <button class="button button3">16px</button>
  29. <button class="button button4">20px</button>
  30. <button class="button button5">24px</button>
  31. </body>
  32. </html>

请使用 padding 属性更改按钮的内边距:

实例
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .button {
  6. background-color: #4CAF50; /* 绿色 */
  7. border: none;
  8. color: white;
  9. text-align: center;
  10. text-decoration: none;
  11. display: inline-block;
  12. font-size: 16px;
  13. margin: 4px 2px;
  14. cursor: pointer;
  15. }
  16. .button1 {padding: 10px 24px;}
  17. .button2 {padding: 12px 28px;}
  18. .button3 {padding: 14px 40px;}
  19. .button4 {padding: 32px 16px;}
  20. .button5 {padding: 16px;}
  21. </style>
  22. </head>
  23. <body>
  24. <h1>按钮大小</h1>
  25. <p>通过 padding 属性改变按钮的内边距:</p>
  26. <button class="button button1">10px 24px</button>
  27. <button class="button button2">12px 28px</button>
  28. <button class="button button3">14px 40px</button>
  29. <button class="button button4">32px 16px</button>
  30. <button class="button button5">16px</button>
  31. </body>
  32. </html>

圆角按钮

请使用 border-radius 属性为按钮添加圆角:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .button {
  6. background-color: #4CAF50; /* 绿色 */
  7. border: none;
  8. color: white;
  9. padding: 20px;
  10. text-align: center;
  11. text-decoration: none;
  12. display: inline-block;
  13. font-size: 16px;
  14. margin: 4px 2px;
  15. cursor: pointer;
  16. }
  17. .button1 {border-radius: 2px;}
  18. .button2 {border-radius: 4px;}
  19. .button3 {border-radius: 8px;}
  20. .button4 {border-radius: 12px;}
  21. .button5 {border-radius: 50%;}
  22. </style>
  23. </head>
  24. <body>
  25. <h1>圆角按钮</h1>
  26. <p>通过 border-radius 属性为按钮添加圆角:</p>
  27. <button class="button button1">2px</button>
  28. <button class="button button2">4px</button>
  29. <button class="button button3">8px</button>
  30. <button class="button button4">12px</button>
  31. <button class="button button5">50%</button>
  32. </body>
  33. </html>

彩色的按钮边框

请使用 border 属性为按钮添加彩色边框:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .button {
  6. background-color: #4CAF50; /* 绿色 */
  7. border: none;
  8. color: white;
  9. padding: 15px 32px;
  10. text-align: center;
  11. text-decoration: none;
  12. display: inline-block;
  13. font-size: 16px;
  14. margin: 4px 2px;
  15. cursor: pointer;
  16. }
  17. .button1 {
  18. background-color: white;
  19. color: black;
  20. border: 2px solid #4CAF50;
  21. }
  22. .button2 {
  23. background-color: white;
  24. color: black;
  25. border: 2px solid #008CBA;
  26. }
  27. .button3 {
  28. background-color: white;
  29. color: black;
  30. border: 2px solid #f44336;
  31. }
  32. .button4 {
  33. background-color: white;
  34. color: black;
  35. border: 2px solid #e7e7e7;
  36. }
  37. .button5 {
  38. background-color: white;
  39. color: black;
  40. border: 2px solid #555555;
  41. }
  42. </style>
  43. </head>
  44. <body>
  45. <h1>有颜色的按钮边框</h1>
  46. <p>请使用 border 属性为按钮添加边框:</p>
  47. <button class="button button1">绿色</button>
  48. <button class="button button2">蓝色</button>
  49. <button class="button button3">黑色</button>
  50. <button class="button button4">灰色</button>
  51. <button class="button button5">黑色</button>
  52. </body>
  53. </html>

可悬停按钮


当鼠标移动到按钮上方时,使用 :hover 选择器可更改按钮的样式。

提示:请使用 transition-duration 属性来确定“悬停”效果的速度:

实例
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .button {
  6. background-color: #4CAF50; /* Green */
  7. border: none;
  8. color: white;
  9. padding: 16px 32px;
  10. text-align: center;
  11. text-decoration: none;
  12. display: inline-block;
  13. font-size: 16px;
  14. margin: 4px 2px;
  15. transition-duration: 0.4s;
  16. cursor: pointer;
  17. }
  18. .button1 {
  19. background-color: white;
  20. color: black;
  21. border: 2px solid #4CAF50;
  22. }
  23. .button1:hover {
  24. background-color: #4CAF50;
  25. color: white;
  26. }
  27. .button2 {
  28. background-color: white;
  29. color: black;
  30. border: 2px solid #008CBA;
  31. }
  32. .button2:hover {
  33. background-color: #008CBA;
  34. color: white;
  35. }
  36. .button3 {
  37. background-color: white;
  38. color: black;
  39. border: 2px solid #f44336;
  40. }
  41. .button3:hover {
  42. background-color: #f44336;
  43. color: white;
  44. }
  45. .button4 {
  46. background-color: white;
  47. color: black;
  48. border: 2px solid #e7e7e7;
  49. }
  50. .button4:hover {background-color: #e7e7e7;}
  51. .button5 {
  52. background-color: white;
  53. color: black;
  54. border: 2px solid #555555;
  55. }
  56. .button5:hover {
  57. background-color: #555555;
  58. color: white;
  59. }
  60. </style>
  61. </head>
  62. <body>
  63. <h1>可悬停的按钮</h1>
  64. <p>使用 :hover 选择器在鼠标移动到按钮上时改变其样式。</p>
  65. <p><b>提示:</b>请使用 transition-duration 属性来确定悬停效果的速度:</p>
  66. <button class="button button1">绿色</button>
  67. <button class="button button2">蓝色</button>
  68. <button class="button button3">红色</button>
  69. <button class="button button4">灰色</button>
  70. <button class="button button5">黑色</button>
  71. </body>
  72. </html>

阴影按钮

请使用 box-shadow 属性为按钮添加阴影:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .button {
  6. background-color: #4CAF50; /* Green */
  7. border: none;
  8. color: white;
  9. padding: 15px 32px;
  10. text-align: center;
  11. text-decoration: none;
  12. display: inline-block;
  13. font-size: 16px;
  14. margin: 4px 2px;
  15. cursor: pointer;
  16. -webkit-transition-duration: 0.4s; /* Safari */
  17. transition-duration: 0.4s;
  18. }
  19. .button1 {
  20. box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
  21. }
  22. .button2:hover {
  23. box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24),0 17px 50px 0 rgba(0,0,0,0.19);
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <h1>阴影按钮</h1>
  29. <p>使用 box-shadow 属性为按钮添加阴影:</p>
  30. <button class="button button1">阴影按钮</button>
  31. <button class="button button2">悬停时的阴影</button>
  32. </body>
  33. </html>

禁用的按钮

请使用 opacity 属性为按钮添加透明度(创建“禁用”外观)。

提示:您还可以添加带有 "not-allowed" 值的 cursor 属性,当您将鼠标悬停在按钮上时,该属性会显示 "no parking sign"(禁停标志):

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .button {
  6. background-color: #4CAF50; /* 绿色 */
  7. border: none;
  8. color: white;
  9. padding: 15px 32px;
  10. text-align: center;
  11. text-decoration: none;
  12. display: inline-block;
  13. font-size: 16px;
  14. margin: 4px 2px;
  15. cursor: pointer;
  16. }
  17. .disabled {
  18. opacity: 0.6;
  19. cursor: not-allowed;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <h1>被禁用的按钮</h1>
  25. <p>使用 opacity 属性向按钮添加一定的透明度(使它看上去已被禁用)</p>
  26. <button class="button">正常按钮</button>
  27. <button class="button disabled">被禁用的按钮</button>
  28. </body>
  29. </html>

按钮宽度



默认情况下,按钮的大小取决于其文本内容(与内容的宽度一样)。请使用 width 属性来更改按钮的宽度:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .button {
  6. background-color: #4CAF50; /* Green */
  7. border: none;
  8. color: white;
  9. padding: 15px 32px;
  10. text-align: center;
  11. text-decoration: none;
  12. display: inline-block;
  13. font-size: 16px;
  14. margin: 4px 2px;
  15. cursor: pointer;
  16. }
  17. .button1 {width: 250px;}
  18. .button2 {width: 50%;}
  19. .button3 {width: 100%;}
  20. </style>
  21. </head>
  22. <body>
  23. <h1>按钮宽度</h1>
  24. <p>使用 width 属性来改变按钮的宽度:</p>
  25. <p><b>提示:</b>请使用像素设置固定宽度,并为响应式按钮使用百分百(例如其父元素的 50%)。请调整窗口大小来查看效果。</p>
  26. <button class="button button1">250px</button><br>
  27. <button class="button button2">50%</button><br>
  28. <button class="button button3">100%</button>
  29. </body>
  30. </html>

按钮分组

删除外边距并向每个按钮添加 float:left,来创建按钮组:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .btn-group .button {
  6. background-color: #4CAF50; /* 绿色 */
  7. border: none;
  8. color: white;
  9. padding: 15px 32px;
  10. text-align: center;
  11. text-decoration: none;
  12. display: inline-block;
  13. font-size: 16px;
  14. cursor: pointer;
  15. float: left;
  16. }
  17. .btn-group .button:hover {
  18. background-color: #3e8e41;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <h1>按钮组</h1>
  24. <p>删除外边距并浮动按钮,来创建一个按钮组:</p>
  25. <div class="btn-group">
  26. <button class="button">Button</button>
  27. <button class="button">Button</button>
  28. <button class="button">Button</button>
  29. <button class="button">Button</button>
  30. </div>
  31. <p style="clear:both"><br>请记得之后清除浮动,否则这个 p 元素会向按钮浮动。</p>
  32. </body>
  33. </html>

带边框的按钮组

使用 border 属性来创建带边框的按钮组:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .btn-group .button {
  6. background-color: #4CAF50; /* 绿色 */
  7. border: 1px solid green;
  8. color: white;
  9. padding: 15px 32px;
  10. text-align: center;
  11. text-decoration: none;
  12. display: inline-block;
  13. font-size: 16px;
  14. cursor: pointer;
  15. float: left;
  16. }
  17. .btn-group .button:not(:last-child) {
  18. border-right: none; /* 阻止双边框 */
  19. }
  20. .btn-group .button:hover {
  21. background-color: #3e8e41;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <h1>带边框的按钮组</h1>
  27. <p>添加边框,来创建带按钮的按钮组:</p>
  28. <div class="btn-group">
  29. <button class="button">Button</button>
  30. <button class="button">Button</button>
  31. <button class="button">Button</button>
  32. <button class="button">Button</button>
  33. </div>
  34. <p style="clear:both"><br>请记得之后清除浮动,否则这个 p 元素会向按钮浮动。</p>
  35. </body>
  36. </html>

垂直按钮组

使用 display:block 取代 float:left 将按钮上下分组,而不是并排:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .btn-group .button {
  6. background-color: #4CAF50; /* 绿色 */
  7. border: 1px solid green;
  8. color: white;
  9. padding: 15px 32px;
  10. text-align: center;
  11. text-decoration: none;
  12. font-size: 16px;
  13. cursor: pointer;
  14. width: 150px;
  15. display: block;
  16. }
  17. .btn-group .button:not(:last-child) {
  18. border-bottom: none; /* 阻止双边框 */
  19. }
  20. .btn-group .button:hover {
  21. background-color: #3e8e41;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <h1>垂直按钮分组</h1>
  27. <div class="btn-group">
  28. <button class="button">按钮</button>
  29. <button class="button">按钮</button>
  30. <button class="button">按钮</button>
  31. <button class="button">按钮</button>
  32. </div>
  33. </body>
  34. </html>

图像上的按钮

Snow
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .container {
  6. position: relative;
  7. width: 100%;
  8. max-width: 400px;
  9. }
  10. .container img {
  11. width: 100%;
  12. height: auto;
  13. }
  14. .container .btn {
  15. position: absolute;
  16. top: 50%;
  17. left: 50%;
  18. transform: translate(-50%, -50%);
  19. -ms-transform: translate(-50%, -50%);
  20. background-color: #f1f1f1;
  21. color: black;
  22. font-size: 16px;
  23. padding: 16px 30px;
  24. border: none;
  25. cursor: pointer;
  26. border-radius: 5px;
  27. text-align: center;
  28. }
  29. .container .btn:hover {
  30. background-color: black;
  31. color: white;
  32. }
  33. </style>
  34. </head>
  35. <body>
  36. <h2>图片上的按钮</h2>
  37. <div class="container">
  38. <img src="/znadmin/md/1269/0.jpg" alt="Snow" style="width:100%">
  39. <button class="btn">Button</button>
  40. </div>
  41. </body>
  42. </html>

分类导航