阿西河

所有教程

公众号
🌙
阿西河前端的公众号

我的收藏

    最近访问  (文章)

      教程列表

      抓包专区
      测试专区

      CSS3 按钮

      CSS 按钮

      本章节我们为大家介绍使用 CSS 来制作按钮。


      基本按钮样式

      代码

      <style>
      .btn {
          border: none;
          color: #FFFFFF;
          padding: 15px 32px;
          text-align: center;
          -webkit-transition-duration: 0.4s;
          transition-duration: 0.4s;
          margin: 16px 0 !important;
          text-decoration: none;
          font-size: 16px;
          background-color: rgb(48, 110, 50);
          border-top-color: initial;
          border-right-color: initial;
          border-bottom-color: initial;
          border-left-color: initial;
          color: rgb(255, 255, 255);
          text-decoration-color: initial;
      }
      </style>
      <button class="btn">CSS 按钮</button>
      <button style="margin-left:25px;">默认按钮</button>
      

      渲染效果如下:


      .button {
          background-color: #4CAF50; /* Green */
          border: none;
          color: white;
          padding: 15px 32px;
          text-align: center;
          text-decoration: none;
          display: inline-block;
          font-size: 16px;
      }
      

      按钮颜色

      Green Blue Red Gray Black

      我们可以使用 background-color 属性来设置按钮颜色:

      <style>
          .button1 {background-color: #4CAF50;} /* Green */
          .button2 {background-color: #008CBA;} /* Blue */
          .button3 {background-color: #f44336;} /* Red */ 
          .button4 {background-color: #e7e7e7; color: black;} /* Gray */ 
          .button5 {background-color: #555555;} /* Black */
      </style>
      
      <button type="button" class="btn button1">Green</button>
      <button type="button" class="btn button2">Blue</button>
      <button type="button" class="btn button3">Red</button>
      <button type="button" class="btn button4">Gray</button>
      <button type="button" class="btn button5">Black</button>
      
      <button type="button" class="btn button1">Green</button>
      <button type="button" class="btn button2">Blue</button>
      <button type="button" class="btn button3">Red</button>
      <button type="button" class="btn button4">Gray</button>
      <button type="button" class="btn button5">Black</button>
      

      按钮大小

      我们可以使用 font-size 属性来设置按钮大小:

      <style>
          .font1 {font-size: 10px;}
          .font2 {font-size: 12px;}
          .font3 {font-size: 16px;}
          .font4 {font-size: 20px;}
          .font5 {font-size: 24px;}
      </style>
      <div>
          <button type="button" class="btn button1 font1">10px</button>
          <button type="button" class="btn button2 font2">12px</button>
          <button type="button" class="btn button3 font3">16px</button>
          <button type="button" class="btn button4 font4">20px</button>
          <button type="button" class="btn button5 font5">24px</button>
      </div>
      

      圆角按钮

      我们可以使用 border-radius 属性来设置圆角按钮:

      <style>
          .round1 {border-radius: 2px;}
          .round2 {border-radius: 4px;}
          .round3 {border-radius: 8px;}
          .round4 {border-radius: 12px;}
          .round5 {border-radius: 50%;}
      </style>
      <div>
          <button type="button" class="btn button1 round1">2px</button>
          <button type="button" class="btn button2 round2">4px</button>
          <button type="button" class="btn button3 round3">8px</button>
          <button type="button" class="btn button4 round4">12px</button>
          <button type="button" class="btn button5 round5">50%</button>
      </div>
      

      渲染效果如下


      按钮边框颜色

      我们可以使用 border 属性设置按钮边框颜色:

      .button1 {
          background-color: white;
          color: black;
          border: 2px solid #4CAF50; /* Green */
      }
      

      演示效果

      代码如下

      <style>
      .border1 {border-radius:3px;background-color:white;color:black;border:2px solid #4CAF50;}
      .border2 {border-radius:3px;background-color:white;color:black;border:2px solid #008CBA;}
      .border3 {border-radius:3px;background-color:white;color:black;border:2px solid #f44336;}
      .border4 {border-radius:3px;background-color:white;color:black;border:2px solid #e7e7e7;}
      .border5 {border-radius:3px;background-color:white;color:black;border:2px solid #555555;}
      </style>
      <div>
          <button type="button" class="btn border1">绿</button>
          <button type="button" class="btn border2">蓝</button>
          <button type="button" class="btn border3">红</button>
          <button type="button" class="btn border4">灰</button>
          <button type="button" class="btn border5">黑</button>
      </div>
      

      鼠标悬停按钮


      代码如下

      <style>
      .border11:hover {background-color:#4CAF50;color:white;}
      .border22:hover {background-color:#008CBA;color:white;}
      .border33:hover {background-color:#f44336;color:white;}
      .border44:hover {background-color:#e7e7e7;}
      .border55:hover {background-color:#555555;color:white;}
      
      .border111{border-radius:3px;background-color:#4CAF50;color:white;}
      .border222{border-radius:3px;background-color:#008CBA;color:white;}
      .border333 {border-radius:3px;background-color:#f44336;color:white;}
      .border444 {border-radius:3px;background-color:#e7e7e7;}
      .border555 {border-radius:3px;background-color:#555555;color:white;}
      
      .border111:hover {background-color:white;color:black;border:2px solid #4CAF50;}
      .border222:hover {background-color:white;color:black;border:2px solid #008CBA;}
      .border333:hover {background-color:white;color:black;border:2px solid #f44336;}
      .border444:hover {background-color:white;color:black;border:2px solid #e7e7e7;}
      .border555:hover {background-color:white;color:black;border:2px solid #555555;}
      
      </style>
      <div>
          <button type="button" class="btn border1 border111">绿</button>
          <button type="button" class="btn border2 border222">蓝</button>
          <button type="button" class="btn border3 border333">红</button>
          <button type="button" class="btn border4 border444">灰</button>
          <button type="button" class="btn border5 border555">黑</button>
      </div>
      <div>
          <button type="button" class="btn border1 border11">绿</button>
          <button type="button" class="btn border2 border22">蓝</button>
          <button type="button" class="btn border3 border33">红</button>
          <button type="button" class="btn border4 border44">灰</button>
          <button type="button" class="btn border5 border55">黑</button>
      </div>
      

      我们可以使用 :hover 选择器来修改鼠标悬停在按钮上的样式。

      提示: 我们可以使用 transition-duration 属性来设置 “hover” 效果的速度:

      .button {
          -webkit-transition-duration: 0.4s; /* Safari */
          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);
      }
      

      禁用按钮

      代码如下

      <style>
          .disabled{cursor: not-allowed;opacity: 0.6;}
      </style>
      <div>
          <button type="button" class="btn">正常按钮</button>
          <button type="button" class="btn disabled">禁用按钮</button>
      </div>
      

      我们可以使用 opacity 属性为按钮添加透明度 (看起来类似 “disabled” 属性效果)。

      提示: 我们可以添加 cursor 属性并设置为 “not-allowed” 来设置一个禁用的图片:

      .disabled {
          opacity: 0.6;
          cursor: not-allowed;
      }
      

      按钮宽度



      代码如下

      <style>
          .width1 {width: 250px}
          .width2 {width: 50%}
          .width3 {width: 100%}
      </style>
      <div>
          <button type="button" class="btn button1 width1">250px</button>
          <br>
          <button type="button" class="btn button2 width2">50%</button>
          <br>
          <button type="button" class="btn button3 width3">100%</button>
      </div>
      

      默认情况下,按钮的大小有按钮上的文本内容决定( 根据文本内容匹配长度 )。 我们可以使用 width 属性来设置按钮的宽度:

      提示: 如果要设置固定宽度可以使用像素 (px) 为单位,如果要设置响应式的按钮可以设置为百分比。

      .button1 {width: 250px;}
      .button2 {width: 50%;}
      .button3 {width: 100%;}
      

      按钮组



      效果如下

      <style>
          .float {
              margin: 0 !important;
              float: left;
              transition: none;
          }
      
          .float:hover {
              background-color: #3e8e41;
          }
      </style>
      <div>
          <button type="button" class="btn button1 float">Button</button>
          <button type="button" class="btn button2 float">Button</button>
          <button type="button" class="btn button3 float">Button</button>
          <button type="button" class="btn button4 float">Button</button>
      </div>
      

      移除外边距并添加 float:left 来设置按钮组:

      .button {
          float: left;
      }
      

      带边框按钮组


      代码如下

      <button 
          type="button" 
          class="btn btn1 float" 
          style="
          border: 1px solid green; 
          --darkreader-inline-border-top:#00e500; 
          --darkreader-inline-border-right:#00e500; 
          --darkreader-inline-border-bottom:#00e500; 
          --darkreader-inline-border-left:#00e500;" 
          data-darkreader-inline-border-top="" 
          data-darkreader-inline-border-right="" 
          data-darkreader-inline-border-bottom="" 
          data-darkreader-inline-border-left=""
      >
          Button
      </button>
      

      我们可以使用 border 属性来设置带边框的按钮组:

      .button {
          float: left;
          border: 1px solid green
      }
      

      按钮动画

      鼠标移动到按钮上后添加箭头标记:

      代码如下

      <style>
          .button {
              display: inline-block;
              border-radius: 4px;
              background-color: #f4511e;
              border: none;
              color: #FFFFFF;
              text-align: center;
              font-size: 28px;
              padding: 20px;
              width: 180px;
              transition: all 0.5s;
              cursor: pointer;
              margin: 5px;
              vertical-align:middle;
          }
          .button span {
              cursor: pointer;
              display: inline-block;
              position: relative;
              transition: 0.5s;
          }
      
          .button span :after {
              content: '»';
              position: absolute;
              opacity: 0;
              top: 0;
              right: -20px;
              transition: 0.5s;
          }
      
          .button:hover span {
              padding-right: 25px;
          }
      
          .button:hover span :after {
              opacity: 1;
              right: 0;
          }
      </style>
      <div >
          <button class="button">
              <span>Hover </span>
          </button>
      </div>
      
      

      点击时添加 “波纹” 效果:

      代码如下

      <style>
      .button123 {
          position: relative;
          background-color: #4CAF50;
          border: none;
          font-size: 28px;
          color: #FFFFFF;
          padding: 20px;
          width: 200px;
          text-align: center;
          -webkit-transition-duration: 0.4s; /* Safari */
          transition-duration: 0.4s;
          text-decoration: none;
          overflow: hidden;
          cursor: pointer;
      }
      
      .button123:after {
          content: "";
          background: #90EE90;
          display: block;
          position: absolute;
          padding-top: 300%;
          padding-left: 350%;
          margin-left: -20px!important;
          margin-top: -120%;
          opacity: 0;
          transition: all 0.8s
      }
      
      .button123:active:after {
          padding: 0;
          margin: 0;
          opacity: 1;
          transition: 0s
      }
      </style>
      <button class="button123">Click Me</button>
      

      点击时添加 “压下” 效果:

      代码如下

      <style>
          .button777 {
          display: inline-block;
          padding: 15px 25px;
          font-size: 24px;
          cursor: pointer;
          text-align: center;   
          text-decoration: none;
          outline: none;
          color: #fff;
          background-color: #4CAF50;
          border: none;
          border-radius: 15px;
          box-shadow: 0 9px #999;
          }
      
          .button777:hover {background-color: #3e8e41}
      
          .button777:active {
          background-color: #3e8e41;
          box-shadow: 0 5px #666;
          transform: translateY(4px);
          }
      </style>
      <div>
          <button class="button777">Click Me</button>
      </div>
      
      
      目录
      目录