【问题标题】:HTML,CSS checkbox label position should be on top of checkboxHTML,CSS 复选框标签位置应在复选框之上
【发布时间】:2017-04-08 13:37:47
【问题描述】:

美好的一天,我不擅长 CSS,我很难将复选框标签放置在复选框本身的顶部。你能帮我么?我希望它的标签也是中心格式的文本,因为我还想使用 jquery 动态更改文本。如果我选中它,“Fix”文本将变为“Fixed”,并且该文本应保持在复选框顶部的居中位置。这是我的 JSFIDDLE >>> https://jsfiddle.net/koykoys/d2cb96xo/ .

**注意:请不要修改“html”部分,只修改“css”部分。

谢谢。

input[type="checkbox"]:not(:checked),
input[type="checkbox"]:checked {
  position: absolute;
  left: -9999px;
}
input[type="checkbox"]:not(:checked) + label,
input[type="checkbox"]:checked + label{
  position: relative;
  padding-left: 50px;
  cursor: pointer;
}


/* checkbox aspect */
input[type="checkbox"]:not(:checked) + label:before,
input[type="checkbox"]:checked + label:before {
  content: '';
  position: absolute;
  left:0; top: 2px;
  width: 40px; height: 40px;
  background: #f8f8f8;
  border-radius: 3px;
  border: 2px solid #aaa;
  -webkit-box-shadow: groove 0 0 13px rgba(0, 0, 0, 0.5);
  box-shadow: groove 0 0 13px rgba(0, 0, 0, 0.5);
}
/* checked mark aspect */
input[type="checkbox"]:not(:checked) + label:after,
input[type="checkbox"]:checked + label:after {
  content: '✔';
  position: absolute;
  top: 0; left: 5px;
  font-size: 34px;
  color: green;
  transition: all .2s;
  -webkit-transition: all .2s;
  -moz-transition: all .2s;
  -ms-transition: all .2s;
  -o-transition: all .2s;
}
/* checked mark aspect changes */
input[type="checkbox"]:not(:checked) + label:after {
  opacity: 0;
  transform: scale(0);
}
input[type="checkbox"]:checked + label:after {
  opacity: 1;
  transform: scale(1);
}
<div class="col-md-12">
   <input id="checkid"  type="checkbox">
   <label for="checkid">Fixed</label>
</div>

【问题讨论】:

    标签: javascript jquery html css checkbox


    【解决方案1】:

    这将使标签在“复选框”上方居中(实际上不再是input type="checkbox",因为您在标签上使用伪元素来直观地替换它)。

    input[type="checkbox"]:not(:checked),
    input[type="checkbox"]:checked {
      position: absolute;
      left: -9999px;
    }
    input[type="checkbox"]:not(:checked) + label,
    input[type="checkbox"]:checked + label{
      position: relative;
      cursor: pointer;
      display: inline-block;
      padding-bottom: 50px;
    }
    
    
    /* checkbox aspect */
    input[type="checkbox"]:not(:checked) + label:before,
    input[type="checkbox"]:checked + label:before {
      content: '';
      position: absolute;
      left:50%; top: 20px;
      width: 40px; height: 40px;
      background: #f8f8f8;
      border-radius: 3px;
      border: 2px solid #aaa;
      -webkit-box-shadow: groove 0 0 13px rgba(0, 0, 0, 0.5);
      box-shadow: groove 0 0 13px rgba(0, 0, 0, 0.5);
      transform: translateX(-50%);
    }
    /* checked mark aspect */
    input[type="checkbox"]:not(:checked) + label:after,
    input[type="checkbox"]:checked + label:after {
      content: '✔';
      position: absolute;
      top: 20px; left: 0;
      right: 0;
      font-size: 34px;
      color: green;
      transition: all .2s;
      -webkit-transition: all .2s;
      -moz-transition: all .2s;
      -ms-transition: all .2s;
      -o-transition: all .2s;
      text-align: center;
    }
    /* checked mark aspect changes */
    input[type="checkbox"]:not(:checked) + label:after {
      opacity: 0;
      transform: scale(0);
    }
    input[type="checkbox"]:checked + label:after {
      opacity: 1;
      transform: scale(1);
    }
    <div class="col-md-12">
       <input id="checkid"  type="checkbox">
       <label for="checkid">Fixed</label>
    </div>
    
    <div class="col-md-12">
       <input id="checkid2"  type="checkbox">
       <label for="checkid2">Label can now vary in length</label>
    </div>

    【讨论】:

    • 谢谢@connexo,这就是我要找的:),我希望在css方面我能像你一样有才华。 :)
    【解决方案2】:

    简单地做一些对齐删除填充和调整位置

    input[type="checkbox"]:not(:checked), input[type="checkbox"]:checked {
    	position: absolute;
    	left: -9999px;
    }
    input[type="checkbox"]:not(:checked) + label, input[type="checkbox"]:checked + label {
    	position: relative;
    	cursor: pointer;
    }
    /* checkbox aspect */
    input[type="checkbox"]:not(:checked) + label:before, input[type="checkbox"]:checked + label:before {
    	content: '';
    	position: absolute;
    	left: 0;
    	top: 22px;
    	width: 40px;
    	height: 40px;
    	background: #f8f8f8;
    	border-radius: 3px;
    	border: 2px solid #aaa;
    	-webkit-box-shadow: groove 0 0 13px rgba(0, 0, 0, 0.5);
    	box-shadow: groove 0 0 13px rgba(0, 0, 0, 0.5);
    }
    /* checked mark aspect */
    input[type="checkbox"]:not(:checked) + label:after, input[type="checkbox"]:checked + label:after {
    	content: '✔';
    	position: absolute;
    	top: 16px;
    	left: 10px;
    	font-size: 34px;
    	color: green;
    	transition: all .2s;
    	-webkit-transition: all .2s;
    	-moz-transition: all .2s;
    	-ms-transition: all .2s;
    	-o-transition: all .2s;
    }
    /* checked mark aspect changes */
    input[type="checkbox"]:not(:checked) + label:after {
    	opacity: 0;
    	transform: scale(0);
    }
    input[type="checkbox"]:checked + label:after {
    	opacity: 1;
    	transform: scale(1);
    }
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    
    </head>
    
    <body>
    <div class="col-md-12">
      <input id="checkid"  type="checkbox">
      <label for="checkid">Fixed</label>
    </div>
    </body>
    </html>

    【讨论】:

    • OP 声明了关于复选框的标签文本居中的要求。
    • 谢谢,这真是一个不错的解决方案:)
    【解决方案3】:

    假设您实际上希望您的 &lt;label&gt; 元素位于您的复选框之上(如您的问题状态),并且您不想像这里的其他答案那样垂直对齐标签的文本:只需设置您的伪将top 属性设置为100% 的元素(这会将它们直接放在&lt;label&gt; 元素下方),然后给&lt;label&gt; 元素一些底部margin 将任何进一步的元素向下推一点(给伪元素的空间),然后将&lt;label&gt; 设置为display 作为block

    input[type="checkbox"]:not(:checked),
    input[type="checkbox"]:checked {
      position: absolute;
      left: -9999px;
    }
    input[type="checkbox"]:not(:checked) + label,
    input[type="checkbox"]:checked + label{
      position: relative;
      margin-bottom: 50px;
      padding-bottom: 4px;
      cursor: pointer;
      display: block;
    }
    
    
    /* checkbox aspect */
    input[type="checkbox"]:not(:checked) + label:before,
    input[type="checkbox"]:checked + label:before {
      content: '';
      position: absolute;
      left:0; top: 100%;
      width: 40px; height: 40px;
      background: #f8f8f8;
      border-radius: 3px;
      border: 2px solid #aaa;
      -webkit-box-shadow: groove 0 0 13px rgba(0, 0, 0, 0.5);
      box-shadow: groove 0 0 13px rgba(0, 0, 0, 0.5);
    }
    /* checked mark aspect */
    input[type="checkbox"]:not(:checked) + label:after,
    input[type="checkbox"]:checked + label:after {
      content: '✔';
      position: absolute;
      top: 100%; left: 5px;
      font-size: 34px;
      color: green;
      transition: all .2s;
      -webkit-transition: all .2s;
      -moz-transition: all .2s;
      -ms-transition: all .2s;
      -o-transition: all .2s;
    }
    /* checked mark aspect changes */
    input[type="checkbox"]:not(:checked) + label:after {
      opacity: 0;
      transform: scale(0);
    }
    input[type="checkbox"]:checked + label:after {
      opacity: 1;
      transform: scale(1);
    }
    <div class="col-md-12">
       <input id="checkid"  type="checkbox">
       <label for="checkid">Fixed</label>
    </div>

    【讨论】:

    • OP 声明了关于复选框的标签文本居中的要求。
    • @connexo 他们确实做到了,这就是为什么我已经投票赞成你的答案。
    【解决方案4】:

    我添加了一个margin-top: 50px 来强制框向下并为标签留出空间。这是完整的 CSS,我不编辑您的原始代码,我只添加了一些行。我在添加的行中写了 cmets。

    input[type="checkbox"]:not(:checked),
    input[type="checkbox"]:checked {
      position: absolute;
      left: -9999px;
      /* added */
      opacity: 0;
    }
    input[type="checkbox"]:not(:checked) + label,
    input[type="checkbox"]:checked + label{
      position: relative;
      padding-left: 50px;
      cursor: pointer;
      /* added */
      text-align: center;
      padding-left: 4px;
    }
    
    
    /* checkbox aspect */
    input[type="checkbox"]:not(:checked) + label:before,
    input[type="checkbox"]:checked + label:before {
      content: '';
      position: absolute;
      left:0; top: 2px;
      width: 40px; height: 40px;
      background: #f8f8f8;
      border-radius: 3px;
      border: 2px solid #aaa;
      -webkit-box-shadow: groove 0 0 13px rgba(0, 0, 0, 0.5);
      box-shadow: groove 0 0 13px rgba(0, 0, 0, 0.5);
      /* added */
      margin-top: 20px;
    }
    /* checked mark aspect */
    input[type="checkbox"]:not(:checked) + label:after,
    input[type="checkbox"]:checked + label:after {
      content: '✔';
      position: absolute;
      top: 0; left: 5px;
      font-size: 34px;
      color: green;
      transition: all .2s;
      -webkit-transition: all .2s;
      -moz-transition: all .2s;
      -ms-transition: all .2s;
      -o-transition: all .2s;
      /* added */
      margin-top: 20px;
    }
    /* checked mark aspect changes */
    input[type="checkbox"]:not(:checked) + label:after {
      opacity: 0;
      transform: scale(0);
    }
    input[type="checkbox"]:checked + label:after {
      opacity: 1;
      transform: scale(1);
    }
    

    【讨论】:

    • 非常感谢您的帮助,非常感谢
    【解决方案5】:

    你为什么不尝试一些简单的东西............把你的标签放在上面并添加一个“br”

     <!DOCTYPE html>
        <html>
        <body>
    
         <form action="demo_form.asp" method="get"><lable>asdf</lable><br>
         <input type="checkbox" name="vehicle" value="Bike"><br>
    
         <input type="submit" value="Submit">
        </form>
    
        </body>
        </html>
    

    【讨论】:

    • 您可能没有注意到,您看到的复选框不是input,而是标签上的::before伪元素。
    【解决方案6】:

    只需添加:

    input[type="checkbox"]:not(:checked) + label,
    input[type="checkbox"]:checked + label {
        padding-top: 11px;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-06-07
      • 2012-10-14
      • 1970-01-01
      • 1970-01-01
      • 2011-12-22
      • 1970-01-01
      • 2020-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多