Jim
Jim

Reputation: 4669

How to line up a label to the right of a styled checkbox

I'm struggling to align a label with my styled checkbox. I'm using this spiffy CSS toggle switch as a styled checkbox. I have the following HTML:

<label>
  <input id="test" class="cmn-toggle cmn-toggle-round" type="checkbox" />
  <label for="test"></label>
  testlabel
</label>

Along with that, I have the following CSS:

input.cmn-toggle-round + label {
  padding: 2px;
  width: 60px;
  height: 30px;
  background-color: #dddddd;
  -webkit-border-radius: 30px;
  -moz-border-radius: 30px;
  -ms-border-radius: 30px;
  -o-border-radius: 30px;
  border-radius: 30px;
}
input.cmn-toggle-round + label:before, input.cmn-toggle-round + label:after {
  display: block;
  position: absolute;
  top: 1px;
  left: 1px;
  bottom: 1px;
  content: "";
}
input.cmn-toggle-round + label:before {
  right: 1px;
  background-color: #f1f1f1;
  -webkit-border-radius: 30px;
  -moz-border-radius: 30px;
  -ms-border-radius: 30px;
  -o-border-radius: 30px;
  border-radius: 30px;
  -webkit-transition: background 0.4s;
  -moz-transition: background 0.4s;
  -o-transition: background 0.4s;
  transition: background 0.4s;
}
input.cmn-toggle-round + label:after {
  width: 29px;
  background-color: #fff;
  -webkit-border-radius: 100%;
  -moz-border-radius: 100%;
  -ms-border-radius: 100%;
  -o-border-radius: 100%;
  border-radius: 100%;
  -webkit-box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
  -moz-box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
  -webkit-transition: margin 0.4s;
  -moz-transition: margin 0.4s;
  -o-transition: margin 0.4s;
  transition: margin 0.4s;
}
input.cmn-toggle-round:checked + label:before {
  background-color: #8ce196;
}
input.cmn-toggle-round:checked + label:after {
  margin-left: 30px;
}

The result has the text from the label placed directly below the checkbox, rather than to the right of it. Here is a screenshot as an example: this is the output

My question is simple. Why is this happening? How can I line up the label to be on the right of the checkbox instead?

Upvotes: 0

Views: 87

Answers (1)

ala8727
ala8727

Reputation: 139

Your text label is being pushed beneath the toggle because it's set to display: block. You could either: 1. Change this to display: inline-block, which would bring the inline text up next to it on the right. 2. Leave display:block, but add float: left to your toggle, so that the text comes up next to it.

You'll just have to add margin/padding to the text as necessary; you'll probably want to wrap it in a or other element so you can style it more easily.

Upvotes: 1

Related Questions