eckenman eckenman - 2 months ago 18
Sass (Sass) Question

Center text besides custom textbox

I have a custom styled checkbox, see this JSFiddle:

https://jsfiddle.net/y58zrd4m/

My HTML looks like this:

<label class="checkbox">
<input type="checkbox" />
<span>Lorem ipsum</span>
</label>


Now I want my text to be vertically centered in relation to the checkbox, how can I achieve this?

Answer

Some changes in your CSS will help you achieve that as:

Code Snippet

.checkbox {
  cursor: pointer;
  position: relative;
}
.checkbox input[type="checkbox"] {
  display: none;
}
.checkbox span {
  font-size: 20px;
  display: inline-block;
  padding: 10px 0 0 40px;
}
.checkbox span:before {
  content: "";
  display: inline-block;
  width: 25px;
  height: 25px;
  border: 2px solid rgba(0, 0, 0, 0.5);
  border-radius: 5px;
  margin: 0;
  position: absolute;
  left: 2px;
  top: -7px;
}
.checkbox span:after {
  content: "";
  display: none;
  position: absolute;
  left: 12px;
  top: -2px;
  width: 5px;
  height: 13px;
  border-bottom: 4px solid white;
  border-right: 4px solid white;
  border-radius: 1px;
  transform: rotate(30deg);
}
.checkbox input[type="checkbox"]:checked + span:before {
  border-color: #03a9f4;
  background-color: #03a9f4;
}
.checkbox input[type="checkbox"]:checked + span:after {
  display: inline-block;
}
<label class="checkbox">
  <input type="checkbox" />
  <span>Lorem ipsum</span>
</label>

Comments