user4756836 user4756836 - 3 months ago 10
CSS Question

Aligning custom checkbox issue css

I am tying to align the text of the right of my checkbox. Currently the text is appearing in multiple lines.

I have tried changing the width and giving it a fixed width:

.checkbox span.custom {
margin-left: 34px;
display: inline-block;
width: 100px; // MY TRY
}


but it is changing the size of the checkbox not the text next to it.

CSS:

* {
box-sizing: border-box;
}

label {
display: inline-block;
}

.checkbox {
position: relative;
min-height: 24px;
font-size: 1.6rem;
}

.checkbox input {
-webkit-tap-highlight-color: transparent;
height: 10px;
margin: 6px;
opacity: 0;
outline: none;
position: absolute;
left: 1px;
top: 1px;
width: 10px;
}

.checkbox .custom {
background-color: #fff;
border: 1px solid #ccc;
border-radius: 3px;
display: inline-block;
height: 24px;
left: 0;
position: absolute;
top: 0;
width: 24px;
}

.checkbox span {
display: inline-block;
margin-left: 34px;
margin-top: 0;
position: relative;
top: 3px;
}

.checkbox input:checked:not(:disabled) + .custom {
background-color: #0574ac;
border-color: #0574ac;
}

.checkbox span {
margin-left: 0px;
}

.checkbox span.custom {
margin-left: 34px;
display: inline-block;
width: 100px; // MY TRY
}

.checkbox span.custom .radio span.custom {
margin-left: 34px;
margin-right: 34px;
display: flex;
}

.radio input:checked + .custom:after {
background-color: #0574ac;
border-radius: 100%;
border: 3px solid #fff;
content: "";
display: block;
height: 16px;
position: absolute;
width: 16px;
}


HTML:

<label for="checkbox1" class="checkbox">
<input id="checkbox1" type="checkbox" role="checkbox" /><span class="custom">Checkbox 1</span>
</label>


JSFiddle Demo

Answer

You should remove the position: absolute of the .checkbox .custom and also the width: 24px that you have there is causing you problems.

Here is a working version:

* {
  box-sizing: border-box;
}

label {
  display: inline-block;
}

.checkbox {
  position: relative;
  min-height: 27px;
  font-size: 1.6rem;
}

.checkbox input {
  -webkit-tap-highlight-color: transparent;
  margin: 6px;
  opacity: 0;
  outline: none;
  position: relative;
  left: 1px;
  top: 1px;
  line-height: 26px;
}
.checkbox input#checkbox2, .checkbox input#checkbox3 {
  opacity: 1;
}
.checkbox3 {
  display: flex;
}
.checkbox3 span {
  order: -1;
}
.checkbox .custom {
  background-color: #fff;
  border: 1px solid #ccc;
  border-radius: 3px;
  display: inline-block;
  height: 27px;
  left: 0;
  top: 0;
}

.checkbox span {
  display: inline-block;
  margin-left: 34px;
  margin-top: 0;
  position: relative;
  top: 3px;
}

.checkbox.checkbox3 span.custom {
  margin-left: 0;
}
.checkbox input:checked:not(:disabled) + .custom {
  background-color: #0574ac;
  border-color: #0574ac;
}

.checkbox span {
  margin-left: 0px;
}

.checkbox span.custom  {
  margin-left: 34px;
  display: inline-block;
}

.checkbox span.custom .radio span.custom {
  margin-left: 34px;
  margin-right: 34px;
  display: flex;
}

.radio input:checked + .custom:after {
  background-color: #0574ac;
  border-radius: 100%;
  border: 3px solid #fff;
  content: "";
  display: block;
  height: 16px;
  position: absolute;
  width: 16px;
}
<label for="checkbox1" class="checkbox">
  <input id="checkbox1" type="checkbox" role="checkbox" /><span class="custom">Checkbox 1</span>
</label>
<br />
<label for="checkbox2" class="checkbox">
  <input id="checkbox2" type="checkbox" role="checkbox" /><span class="custom">Checkbox 2</span>
</label>
<br />
<label for="checkbox3" class="checkbox checkbox3">
  <input id="checkbox3" type="checkbox" role="checkbox" /><span class="custom">Checkbox 3</span>
</label>

I'm not sure why you set the opacity: 0 for the checkbox itself so I added an example with the checkbox without the opacity: 0

Update

I notices in the comments that you want the label to be on the left and the checkbox on the right so I also added an example for that using the flexbox model.

Comments