CSS 彩色复选框标签文本未在复选框右侧内联拉伸

时间:2021-07-15 14:16:18

标签: css

我有一些用于自定义复选框的 css。

.test label {
  position: absolute;
  width: 13px;
  height: 13px;
  background-color: #ff0000;
  color: #ffffff;
  -webkit-transition: background-color 1s ease-out 1s;
  -moz-transition: background-color 1s ease-out 1s;
  -o-transition: background-color 1s ease-out 1s;
  transition: background-color 1s ease-out 1s;
}

.test input[type=checkbox]:checked + label {
  background-color:#ff0000;
  -webkit-transition: background-color 1s ease-out 1s;
  -moz-transition: background-color 1s ease-out 1s;
  -o-transition: background-color 1s ease-out 1s;
  transition: background-color 1s ease-out 1s;
}

.test label:after {
  position: absolute;
  bottom: 5px;
  width: 12px;
  height: 5px;
  opacity: 0;
  content: '';
  background: transparent;
  border: 2px solid #ffffff;
  border-top: none;
  border-right: none;
  -webkit-transform: rotate(-50deg);
  -moz-transform: rotate(-50deg);
  -ms-transform: rotate(-50deg);
  -o-transform: rotate(-50deg);
  transform: rotate(-50deg);
  filter: alpha(opacity=0);
}
.test input[type=checkbox] {
  visibility: hidden;
}
.test input[type=checkbox]:checked + label:after {
  opacity: 1;
  filter: alpha(opacity=100);    
}
<div class="test">
  <input type="checkbox" value="none" id="test1" name="check">
  <label for="test1">This text should be inline from left to right or the right of the checkbox</label>
</div>

问题是文本全部以 13 像素的宽度环绕,我需要它在复选框的右侧进行拉伸。

如何修复此代码?

1 个答案:

答案 0 :(得分:1)

你的代码完全无法理解,你想实现什么?

input[type="checkbox"]:checked+label {
  background-color: #0a0;
}

label {
  display: block;
  position: relative;
  padding-left: 30px;
  margin-bottom: 12px;
  line-height: 26px;
  cursor: pointer;
  font-size: 20px;
}

/* Hiding the checkbox */
input[type=checkbox] {
  visibility: hidden;
}

/* Show a custom checkbox instead */
.custom {
  position: absolute;
  top: 0;
  left: 0;
  height: 25px;
  width: 25px;
  background-color: black;
  -webkit-transition: background-color .2s ease-out .2s;
  -moz-transition: background-color .2s ease-out .2s;
  -o-transition: background-color .2s ease-out .2s;
  transition: background-color .2s ease-out .2s;
}

label:hover input~.custom {
  background-color: gray;
}

label input:active~.custom {
  background-color: white;
}

label input:checked~.custom {
  background-color: orange;
}


/* Checkmark to be shown in checkbox */
.custom:after {
  content: "";
  position: absolute;
  display: none;
}

/* Display checkmark when checked */
label input:checked~.custom:after {
  display: block;
}

label .custom:after {
  left: 8px;
  bottom: 5px;
  width: 6px;
  height: 12px;
  border: solid white;
  border-width: 0 4px 4px 0;
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
}
<label for="check1">
  Check this box
  <input id="check1" type="checkbox">
  <span class="custom"></span>
</label>

<label for="check2">
  Or this one
  <input id="check2" type="checkbox">
  <span class="custom"></span>
</label>

<label for="check3">
  Or all three
  <input id="check3" type="checkbox">
  <span class="custom"></span>
</label>

相关问题