ToB企服应用市场:ToB评测及商务社交产业平台

标题: 前端必知必会-CSS 按钮 [打印本页]

作者: 水军大提督    时间: 2024-11-27 11:49
标题: 前端必知必会-CSS 按钮

CSS 按钮Buttons

了解如何使用 CSS 设置按钮样式。
基本按钮样式


示例
  1. .button {
  2. background-color: #04AA6D; /* 绿色 */
  3. border: none;
  4. color: white;
  5. padding: 15px 32px;
  6. text-align: center;
  7. text-decoration: none;
  8. display: inline-block;
  9. font-size: 16px;
  10. }
复制代码
按钮颜色


使用 background-color 属性更改按钮的配景颜色:
示例
  1. .button1 {background-color: #04AA6D;} /* 绿色 */
  2. .button2 {background-color: #008CBA;} /* 蓝色 */
  3. .button3 {background-color: #f44336;} /* 红色 */
  4. .button4 {background-color: #e7e7e7; color: black;} /* 灰色 */
  5. .button5 {background-color: #555555;} /* 黑色 */
复制代码
按钮大小


使用 font-size 属性更改按钮的字体大小:
示例
  1. .button1 {font-size: 10px;}
  2. .button2 {font-size: 12px;}
  3. .button3 {font-size: 16px;}
  4. .button4 {font-size: 20px;}
  5. .button5 {font-size: 24px;}
复制代码
使用 padding 属性更改按钮的填充:

示例
  1. .button1 {padding: 10px 24px;}
  2. .button2 {padding: 12px 28px;}
  3. .button3 {padding: 14px 40px;}
  4. .button4 {padding: 32px 16px;}
  5. .button5 {padding: 16px;}
复制代码
圆角按钮


使用 border-radius 属性为按钮添加圆角:
示例
  1. .button1 {border-radius: 2px;}
  2. .button2 {border-radius: 4px;}
  3. .button3 {border-radius: 8px;}
  4. .button4 {border-radius: 12px;}
  5. .button5 {border-radius: 50%;}
复制代码
彩色按钮边框


使用 border 属性为按钮添加彩色边框:
示例
  1. .button1 {
  2. background-color: white;
  3. color: black;
  4. border: 2px solid #04AA6D; /* 绿色 */
  5. }
  6. ...
复制代码
可悬停按钮


使用 :hover 选择器在将鼠标移到按钮上时更改按钮的样式。
提示:使用 transition-duration 属性确定“悬停”结果的速度:
示例
  1. .button {
  2. transition-duration: 0.4s;
  3. }
  4. .button:hover {
  5. background-color: #04AA6D; /* 绿色 */
  6. color: white;
  7. }
  8. ...
复制代码
阴影按钮

阴影按钮悬停时阴影
使用 box-shadow 属性为按钮添加阴影:

示例
  1. .button1 {
  2. box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
  3. }
  4. .button2:hover {
  5. box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19);
  6. }
复制代码
已禁用按钮


使用 opacity 属性为按钮添加透明度(创建“已禁用”外观)。
提示:您还可以添加值为“not-allowed”的 cursor 属性,这样当您将鼠标悬停在按钮上时,将表现“克制停车标记”:
示例
  1. .disabled {
  2. opacity: 0.6;
  3. cursor: not-allowed;
  4. }
复制代码
按钮宽度


默认环境下,按钮的大小由其文本内容决定(与内容一样宽)。使用 width 属性可更改按钮的宽度:
示例
  1. .button1 {width: 250px;}
  2. .button2 {width: 50%;}
  3. .button3 {width: 100%;}
复制代码
按钮组


删除边距并为每个按钮添加 float:left 以创建按钮组:
示例
  1. .button {
  2. float: left;
  3. }
复制代码
带边框的按钮组


使用 border 属性创建带边框的按钮组:
示例
  1. .button {
  2. float: left;
  3. border: 1px solid green;
  4. }
复制代码
垂直按钮组


使用 display:block 而不是 float:left 将按钮分组到相互下方,而不是并排:
示例
  1. .button {
  2. display: block;
  3. }
复制代码
图像上的按钮


  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>Button on Image</h2>
  37. <p>Add a button on an image:</p>
  38. <div class="container">
  39.   <img src="img_lights.jpg" alt="Snow" style="width:100%">
  40.   <button class="btn">Button</button>
  41. </div>
  42. </body>
  43. </html>
复制代码
示例
悬停时添加箭头:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .button {
  6.   display: inline-block;
  7.   border-radius: 4px;
  8.   background-color: #f4511e;
  9.   border: none;
  10.   color: #FFFFFF;
  11.   text-align: center;
  12.   font-size: 28px;
  13.   padding: 20px;
  14.   width: 200px;
  15.   transition: all 0.5s;
  16.   cursor: pointer;
  17.   margin: 5px;
  18. }
  19. .button span {
  20.   cursor: pointer;
  21.   display: inline-block;
  22.   position: relative;
  23.   transition: 0.5s;
  24. }
  25. .button span:after {
  26.   content: '\00bb';
  27.   position: absolute;
  28.   opacity: 0;
  29.   top: 0;
  30.   right: -20px;
  31.   transition: 0.5s;
  32. }
  33. .button:hover span {
  34.   padding-right: 25px;
  35. }
  36. .button:hover span:after {
  37.   opacity: 1;
  38.   right: 0;
  39. }
  40. </style>
  41. </head>
  42. <body>
  43. <h2>Animated Button</h2>
  44. <button class="button" style="vertical-align:middle"><span>Hover </span></button>
  45. </body>
  46. </html>
复制代码
示例
单击时添加“按下”结果:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .button {
  6.   display: inline-block;
  7.   padding: 15px 25px;
  8.   font-size: 24px;
  9.   cursor: pointer;
  10.   text-align: center;
  11.   text-decoration: none;
  12.   outline: none;
  13.   color: #fff;
  14.   background-color: #04AA6D;
  15.   border: none;
  16.   border-radius: 15px;
  17.   box-shadow: 0 9px #999;
  18. }
  19. .button:hover {background-color: #3e8e41}
  20. .button:active {
  21.   background-color: #3e8e41;
  22.   box-shadow: 0 5px #666;
  23.   transform: translateY(4px);
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <h2>Animated Button - "Pressed Effect"</h2>
  29. <button class="button">Click Me</button>
  30. </body>
  31. </html>
复制代码
单击
示例
悬停时淡入:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="viewport" content="width=device-width, initial-scale=1">
  5. <style>
  6. .button {
  7.   background-color: #f4511e;
  8.   border: none;
  9.   color: white;
  10.   padding: 16px 32px;
  11.   text-align: center;
  12.   font-size: 16px;
  13.   margin: 4px 2px;
  14.   opacity: 0.6;
  15.   transition: 0.3s;
  16.   display: inline-block;
  17.   text-decoration: none;
  18.   cursor: pointer;
  19. }
  20. .button:hover {opacity: 1}
  21. </style>
  22. </head>
  23. <body>
  24. <h2>Animated Button - Fade in Effect</h2>
  25. <button class="button">Hover Over Me</button>
  26. </body>
  27. </html>
复制代码
淡入
示例
单击时添加“荡漾”结果:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .button {
  6.   position: relative;
  7.   background-color: #04AA6D;
  8.   border: none;
  9.   font-size: 28px;
  10.   color: #FFFFFF;
  11.   padding: 20px;
  12.   width: 200px;
  13.   text-align: center;
  14.   transition-duration: 0.4s;
  15.   text-decoration: none;
  16.   overflow: hidden;
  17.   cursor: pointer;
  18. }
  19. .button:after {
  20.   content: "";
  21.   background: #f1f1f1;
  22.   display: block;
  23.   position: absolute;
  24.   padding-top: 300%;
  25.   padding-left: 350%;
  26.   margin-left: -20px !important;
  27.   margin-top: -120%;
  28.   opacity: 0;
  29.   transition: all 0.8s
  30. }
  31. .button:active:after {
  32.   padding: 0;
  33.   margin: 0;
  34.   opacity: 1;
  35.   transition: 0s
  36. }
  37. </style>
  38. </head>
  39. <body>
  40. <h2>Animated Button - Ripple Effect</h2>
  41. <button class="button">Click Me</button>
  42. </body>
  43. </html>
复制代码

总结

本文介绍了CSS 按钮的使用,如有问题欢迎私信和批评

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4