HTML,CSS复选框标签位置应位于复选框

时间:2016-11-24 09:59:36

标签: javascript jquery html css checkbox

美好的一天,我不擅长CSS,而且我很难将复选框标签放在复选框本身的顶部。你能帮我么?我希望它的标签也是中心格式的文本,因为我还想使用jquery动态更改文本。如果我选中它,“修复”文本将变为“固定”,此文本应保持在复选框顶部的居中位置。这是我的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>

6 个答案:

答案 0 :(得分:3)

这会使标签居中于&#34;复选框&#34; (它实际上不再是input type="checkbox",因为您在标签上使用伪元素来直观地替换它。)

&#13;
&#13;
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);
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

只需对齐一些对齐移除填充并调整位置

&#13;
&#13;
def by_three?(num)
  num % 3 == 0
end
&#13;
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);
}
&#13;
&#13;
&#13;

答案 2 :(得分:1)

假设您确实希望将<label>元素放在复选框的顶部(例如您的问题状态),而您却不想像标题的其他答案那样垂直对齐标签的文字:只需设置您的将top属性设置为100%的伪元素(将它们直接放在<label>元素下方),然后将<label>元素设置为底部{{1} }向下推动任何其他元素(为伪元素提供空间),然后将margin设置为<label>

display
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);
}

答案 3 :(得分:1)

我添加了一个margin-top: 50px来强行打开盒子并给标签留出一个空间。这是完整的CSS,我不会编辑你的原始代码我只添加了一些行。我在我添加的专栏中写了评论。

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);
}

答案 4 :(得分:0)

为什么不尝试简单的事情.......把你的lableon放在上面并添加一个&#34; br&#34;

 <!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>

答案 5 :(得分:-1)

只需添加:

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