【问题标题】:How to customize the checkbox appearance with CSS如何使用 CSS 自定义复选框外观
【发布时间】:2020-12-21 01:08:04
【问题描述】:

我想对其进行自定义,以便复选框颜色在单击时会发生变化。请参考link and image 并建议如何实现这一点,因为我不知道该怎么做。

下面是复选框的 HTML 代码:

<div id="container">
    <div id="btnOuterDIV">
        <div id="btnChkbox">
            <input type="checkbox" id="btnCB" />
        </div>
    </div>
</div>

复选框的 CSS,它应该看起来像图片中的那个:

#container {
    padding:50px;
}
#btnOuterDIV {
    height:30px;
    width:80px;
    border:1px solid #ccc;
    border-radius:5px;
}
#btnOuterDIV:hover {
    border-color:#888;
}
#btnChkbox {
    height:15px;
    width:15px;
    padding-top:5px;
    padding-left:5px;
}

【问题讨论】:

    标签: html twitter-bootstrap css checkbox


    【解决方案1】:

    你必须创建一个&lt;label&gt; 并设置它的样式。

    <input type="checkbox" id="cb">
    <label for="cb" class="checkbox"></label>
    

    在CSS中,有:checked,你可以这样使用:

    #cb:checked + .checkbox {
      /* style here */
    }
    

    Example here

    【讨论】:

      【解决方案2】:

      [type="checkbox"]:not(:checked),
      [type="checkbox"]:checked {
        position: absolute;
        left: -9999px;
      }
      [type="checkbox"]:not(:checked) + label,
      [type="checkbox"]:checked + label {
        position: relative;
        padding-left: 25px;
        cursor: pointer;
      }
      
      /* checkbox aspect */
      [type="checkbox"]:not(:checked) + label:before,
      [type="checkbox"]:checked + label:before {
        content: '';
        position: absolute;
        left:0; top: 2px;
        width: 17px; height: 17px;
        border: 1px solid #aaa;
        background: #f8f8f8;
        border-radius: 3px;
        box-shadow: inset 0 1px 3px rgba(0,0,0,.3)
      }
      /* checked mark aspect */
      [type="checkbox"]:not(:checked) + label:after,
      [type="checkbox"]:checked + label:after {
        content: '✔';
        position: absolute;
        top: 3px; left: 4px;
        font-size: 18px;
        line-height: 0.8;
        color: #09ad7e;
        transition: all .2s;
      }
      [type="checkbox"]:not(:checked) + label:after {
        opacity: 0;
        transform: scale(0);
      }
      body {
        font-family: "Open sans", "Segoe UI", "Segoe WP", Helvetica, Arial, sans-serif;
        color: #777;
      }
      h1 {
        margin-bottom: 5px;
        font-weight: normal;
        text-align: center;
      }
      form {
        width: 80px;
        margin: 0 auto;
      }
      <h1>Custom checkboxes with CSS</h1>
      
      
      <form action="#">
        <p>
          <input type="checkbox" id="test1" />
          <label for="test1">Red</label>
        </p>
        
      </form>

      【讨论】:

      • 非常感谢 Mahesh,它工作得非常好,但是如果我改变背景颜色和箭头颜色,同样不能正常工作,如果可能的话,你能告诉我如何让它工作吗. jsfiddle.net/vivekhegde99/hfezs8r1/2
      【解决方案3】:
          <h1>Custom checkboxes with CSS</h1>
      
      
          <form action="#">
            <p>
              <input type="checkbox" id="test1" />
              <label for="test1">Red</label>
            </p>
      
          </form>
      
      
      
      ------CSS--------
      
      
      [type="checkbox"]:not(:checked),
      [type="checkbox"]:checked {
        position: absolute;
        left: -9999px;
      }
      
      [type="checkbox"]:not(:checked) + label,
      [type="checkbox"]:checked + label {
        position: relative;
        padding-left: 25px;
        cursor: pointer;
      }
      
      
      /* checkbox aspect */
      
      [type="checkbox"]:not(:checked) + label:before,
      [type="checkbox"]:checked + label:before {
        content: '';
        position: absolute;
        left: 0;
        top: 2px;
        width: 17px;
        height: 17px;
        border: 1px solid #aaa;
        background: #f8f8f8;
        border-radius: 3px;
        box-shadow: inset 0 1px 3px rgba(0, 0, 0, .3)
      }
      
      
      /* checked mark aspect */
      
      [type="checkbox"]:not(:checked) + label:after,
      [type="checkbox"]:checked + label:after {
        content: '✔';
        position: absolute;
        top: 3px;
        left: 4px;
        font-size: 18px;
        line-height: 0.8;
        color: white;
        background-color:#FF3336;
        transition: all .2s;
      }
      
      [type="checkbox"]:not(:checked) + label:after {
        opacity: 0;
        transform: scale(0);
      }
      
      body {
        font-family: "Open sans", "Segoe UI", "Segoe WP", Helvetica, Arial, sans-serif;
        color: #777;
        background-color:#4CFF33;
      }
      
      h1 {
        margin-bottom: 5px;
        font-weight: normal;
        text-align: center;
      }
      
      form {
        width: 80px;
        margin: 0 auto;
      }
      

      【讨论】:

      • 我想我已经更改了背景颜色和箭头颜色。它的工作。希望您对更改感到满意。
      • 非常感谢 Mahesh,很抱歉回复晚了,它工作正常!!
      【解决方案4】:
      <!DOCTYPE html>
      <html>
      <head>
      <title>Custom checkbox</title>
      </head>
      <style>
      input[type="checkbox"] {
        visibility: hidden;
      }
      label {
        cursor: pointer;
      }
      input[type="checkbox"] + label:before {
        border: 1px solid #ff0000;
        content: "\00a0";
        display: inline-block;
        font: 16px/1em sans-serif;
        height: 16px;
        margin: 0 .25em 0 0;
        padding: 0;
        vertical-align: top;
        width: 16px;
      }
      input[type="checkbox"]:checked + label:before {
        background: #00ff00;
        color: #ff0000;
        content: "\2713";
        text-align: center;
      }
      </style>
      <body>
          <p>
            <input type="checkbox" id="checkbox1" name="CheckOption">
            <label for="checkbox1">Check1</label>
          </p>
          <p>
            <input type="checkbox" id="checkbox2" name="CheckOption">
            <label for="checkbox2">Check2</label>
          </p>
          <p>
            <input type="checkbox" id="checkbox3" name="CheckOption">
            <label for="checkbox3">Check3</label>
          </p>
      
      </body>
      </html>
      

      【讨论】:

        【解决方案5】:
        <!DOCTYPE html>
        <html>
        <head>
        <title>Page Title</title>
        </head>
        <style>
        .checkbox {
            display: inline-flex;
            cursor: pointer;
            position: relative;
        }
        
        .checkbox > span {
            color: #34495E;
            padding: 0.5rem 0.25rem;
        }
        
        .checkbox > input {
            height: 25px;
            width: 25px;
            -webkit-appearance: none;
            -moz-appearance: none;
            -o-appearance: none;
            appearance: none;
            border: 1px solid #34495E;
            border-radius: 4px;
            outline: none;
            transition-duration: 0.3s;
            background-color: #41B883;
            cursor: pointer;
          }
        
        .checkbox > input:checked {
            border: 1px solid #41B883;
            background-color: #34495E;
        }
        
        .checkbox > input:checked + span::before {
            content: '\2713';
            display: block;
            text-align: center;
            color: #41B883;
            position: absolute;
            left: 0.7rem;
            top: 0.2rem;
        }
        
        .checkbox > input:active {
            border: 2px solid #34495E;
        }
        </style>
        <body>
          <label class="checkbox">
              <input type="checkbox" />
              <span>Check Me</span>
          </label>
        </body>
        </html>
        

        【讨论】:

          猜你喜欢
          • 2021-11-23
          • 1970-01-01
          • 2011-09-18
          • 1970-01-01
          • 2016-04-21
          • 1970-01-01
          • 2016-10-16
          • 2019-10-02
          • 1970-01-01
          相关资源
          最近更新 更多