【问题标题】:Custom checkboxes move slightly when selected选中时自定义复选框会略微移动
【发布时间】:2017-09-03 19:37:07
【问题描述】:

I modified the checkboxes in my project to look more aligned with the rest of the design but I have a problem: whenever selected the label of the checkbox moves slightly.

input[type="checkbox"] {
  display: none !important;
}

input[type="checkbox"]+label:before {
  position: relative;
  content: " ";
  display: inline-block;
  width: 22px;
  height: 22px;
  border: 1px solid grey;
  top: 4px;
  margin: 0 10px 0 0;
}

input[type="checkbox"]:checked+label:before {
  position: relative;
  content: "✔";
  display: inline-block;
  width: 22px;
  height: 22px;
  border: 1px solid grey;
  top: 4px;
  margin: 0 10px 0 0;
}
<input type="checkbox" />
<label>Click to accept</label>

结果如下:

如果我选择它会发生以下情况:

我做错了什么?

【问题讨论】:

    标签: html css checkbox


    【解决方案1】:

    要修复“移动”,您需要:

    • label::before 中设置vertical-align: some value 我选择了bottom

    并且要对齐“✔”(如果没有 - sn-p 不是),您需要:

    • :checked+label::before 中添加text-align:centerline-height:22px(与height 相同)

    我也删除了重复的属性,没有必要

    注意:您在label 中缺少for 属性以匹配输入中的id,否则您无法点击伪checkbox,只能在label 中点击

    input[type="checkbox"] {
      display: none !important;
    }
    
    input[type="checkbox"]+label::before {
      position: relative;
      content: " ";
      display: inline-block;
      vertical-align: bottom;
      width: 22px;
      height: 22px;
      border: 1px solid grey;
      top: 4px;
      margin: 0 10px 0 0;
    }
    
    input[type="checkbox"]:checked+label::before {
      content: "✔";
      text-align: center;
      line-height: 22px;
    }
    <input id="input" type="checkbox" />
    <label for="input">Click to accept</label>

    【讨论】:

      【解决方案2】:

      你可以在这里看到另一种方式。

      参见下面的工作示例:

      /*--CSS--*/
       
      
         
      
       input[type="checkbox"] {
          -webkit-appearance: none;
          background-color: transparent;
          -webkit-rtl-ordering: logical;
          user-select: text;
          cursor: pointer;
          padding: 1px;
          border-width: 0;
          border-style: inset;
          border-color: initial;
          border-image: initial;
          float: left;
          margin: 0 25px 0 0;
          position: relative;
      }
      input[type="checkbox"]:after {
          -webkit-transition: all 0.3s ease-in-out;
          -moz-transition: all 0.3s ease-in-out;
          transition: all 0.3s ease-in-out;
          content: "";
          position: absolute;
          left: 0;
          z-index: 1;
          width: 16px;
          height: 16px;
          border: 1px solid #c0c0c0;
          top:0;
      }
      	input[type="checkbox"]:before {
          -webkit-transition: all 0.3s ease-in-out;
          -moz-transition: all 0.3s ease-in-out;
          transition: all 0.3s ease-in-out;
          content: "";
          position: absolute;
          left: 0;
          z-index: 1;
          width: 16px;
          height: 16px;
          border: 1px solid #c0c0c0;
          top:0;
          opactity:0;
      }
          input[type="checkbox"]:checked:before {
          -webkit-transform: rotate(-45deg);
          -moz-transform: rotate(-45deg);
          -ms-transform: rotate(-45deg);
          -o-transform: rotate(-45deg);
          transform: rotate(-45deg);
          height: 5px;
          border-color: #009688;
          border-top-style: none;
          border-right-style: none;
          outline: none;
          opacity: 1;
          width: 12px;
          top: 3px;
          left: 3px;
      }
      input[type="checkbox"]:focus{outline:0;}
      <!--HTML-->
      	 <input type="checkbox" />
      	 <label>Click to accept</label>

      【讨论】:

        【解决方案3】:

        我为标签使用了一个弹性框来垂直对齐项目。选中复选框时添加text-align:center,确保标记水平居中。

        label {
          display: flex;
          align-items: center;
        }
        
        input[type="checkbox"] {
          display: none;
        }
        
        input[type="checkbox"]+label:before {
          content: " ";
          display: inline-block;
          width: 22px;
          height: 22px;
          border: 1px solid grey;
          margin-right: 0.5em;
        }
        
        input[type="checkbox"]:checked+label:before {
          content: "✔";
          display: inline-block;
          width: 22px;
          height: 22px;
          border: 1px solid grey;
          margin-right: 0.5em;
          text-align: center;
        }
        <input type="checkbox" id="accept" />
        <label for="accept">Click to accept</label>

        【讨论】:

          【解决方案4】:

          你可以试试这个,效果很好

          input[type="checkbox"] {
                position: absolute;
              width: 100%;
              height: 100%;
              opacity: 0;
            }
          
            input[type="checkbox"] + label:before {
                  position: relative;
              content: " ";
              display: inline-block;
              width: 22px;
              height: 22px;
              border: 1px solid grey;
              top: 4px;
              margin: -8px 10px 0 0;
              vertical-align: middle;
            }
          
            input[type="checkbox"]:checked + label:before {
               content: "✔";
              text-align: center;
              line-height: 22px;
            }
           <input type="checkbox"/>
           <label>Click to accept</label>

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-07-06
            • 2022-11-15
            • 2011-09-23
            • 1970-01-01
            相关资源
            最近更新 更多