Wain2021
Wain2021

Reputation: 349

CSS Colored checkbox label text not inline stretch on the right of the checkbox

I have some css for a custom checkbox.

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

The issue is that the text is all wrapping down in a width of 13 pixels and I need it on the right of the checkbox to stretch.

How can I fix this code?

Upvotes: 1

Views: 55

Answers (1)

Timothy Alexis Vass
Timothy Alexis Vass

Reputation: 2705

Your code is completely incomprehensible, what is it that you're trying to achieve?

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>

Upvotes: 1

Related Questions