在同一行显示标签和滑动复选框

时间:2015-08-03 07:52:15

标签: html css checkbox

在我的HTML页面中,我尝试在同一行上对齐标签和滑动复选框。我的代码如下(滑动复选框代码来自另一篇文章)。

.ondisplay {
  display: inline-block;
}
/* SLIDE THREE */

.slideThree {
  width: 80px;
  height: 26px;
  background: #333;
  margin: 10px auto;
  -webkit-border-radius: 50px;
  -moz-border-radius: 50px;
  border-radius: 50px;
  position: relative;
  -webkit-box-shadow: inset 0px 1px 1px rgba(0, 0, 0, 0.5), 0px 1px 0px rgba(255, 255, 255, 0.2);
  -moz-box-shadow: inset 0px 1px 1px rgba(0, 0, 0, 0.5), 0px 1px 0px rgba(255, 255, 255, 0.2);
  box-shadow: inset 0px 1px 1px rgba(0, 0, 0, 0.5), 0px 1px 0px rgba(255, 255, 255, 0.2);
}
.slideThree:after {
  content: 'OFF';
  font: 12px/26px Arial, sans-serif;
  color: #000;
  position: absolute;
  right: 10px;
  z-index: 0;
  font-weight: bold;
  text-shadow: 1px 1px 0px rgba(255, 255, 255, .15);
}
.slideThree:before {
  content: 'ON';
  font: 12px/26px Arial, sans-serif;
  color: #00bf00;
  position: absolute;
  left: 10px;
  z-index: 0;
  font-weight: bold;
}
.slideThree label {
  display: block;
  width: 34px;
  height: 20px;
  -webkit-border-radius: 50px;
  -moz-border-radius: 50px;
  border-radius: 50px;
  -webkit-transition: all .4s ease;
  -moz-transition: all .4s ease;
  -o-transition: all .4s ease;
  -ms-transition: all .4s ease;
  transition: all .4s ease;
  cursor: pointer;
  position: absolute;
  top: 3px;
  left: 3px;
  z-index: 1;
  -webkit-box-shadow: 0px 2px 5px 0px rgba(0, 0, 0, 0.3);
  -moz-box-shadow: 0px 2px 5px 0px rgba(0, 0, 0, 0.3);
  box-shadow: 0px 2px 5px 0px rgba(0, 0, 0, 0.3);
  background: #fcfff4;
  background: -webkit-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
  background: -moz-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
  background: -o-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
  background: -ms-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
  background: linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
  filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#fcfff4', endColorstr='#b3bead', GradientType=0);
}
.slideThree input[type=checkbox]:checked + label {
  left: 43px;
}
input[type=checkbox] {
  visibility: hidden;
}
}
<div class="ondisplay">
  <div><strong>Test: </strong>
  </div>
  <div class="slideThree">
    <input type="checkbox" value="None" id="slideThree" name="check" checked/>
    <label for="slideThree"></label>
  </div>
</div>

通过这些,标签“Test”和滑块出现在另一个左对齐的下方。我需要它们出现在同一条线上,首先是标签然后是滑块。我试过了display: inline,但它没有用。我仍然是CSS的新手。有人可以帮忙吗?

编辑:这是我提到的小提琴 - http://jsfiddle.net/CXn3v/

2 个答案:

答案 0 :(得分:1)

添加以下CSS规则。

.ondisplay > div {
   display: inline-block;
}

.ondisplay {
  display: inline-block;
}

.ondisplay > div {
  display: inline-block;
}
/* SLIDE THREE */

.slideThree {
  width: 80px;
  height: 26px;
  background: #333;
  margin: 10px auto;
  -webkit-border-radius: 50px;
  -moz-border-radius: 50px;
  border-radius: 50px;
  position: relative;
  -webkit-box-shadow: inset 0px 1px 1px rgba(0, 0, 0, 0.5), 0px 1px 0px rgba(255, 255, 255, 0.2);
  -moz-box-shadow: inset 0px 1px 1px rgba(0, 0, 0, 0.5), 0px 1px 0px rgba(255, 255, 255, 0.2);
  box-shadow: inset 0px 1px 1px rgba(0, 0, 0, 0.5), 0px 1px 0px rgba(255, 255, 255, 0.2);
}
.slideThree:after {
  content: 'OFF';
  font: 12px/26px Arial, sans-serif;
  color: #000;
  position: absolute;
  right: 10px;
  z-index: 0;
  font-weight: bold;
  text-shadow: 1px 1px 0px rgba(255, 255, 255, .15);
}
.slideThree:before {
  content: 'ON';
  font: 12px/26px Arial, sans-serif;
  color: #00bf00;
  position: absolute;
  left: 10px;
  z-index: 0;
  font-weight: bold;
}
.slideThree label {
  display: block;
  width: 34px;
  height: 20px;
  -webkit-border-radius: 50px;
  -moz-border-radius: 50px;
  border-radius: 50px;
  -webkit-transition: all .4s ease;
  -moz-transition: all .4s ease;
  -o-transition: all .4s ease;
  -ms-transition: all .4s ease;
  transition: all .4s ease;
  cursor: pointer;
  position: absolute;
  top: 3px;
  left: 3px;
  z-index: 1;
  -webkit-box-shadow: 0px 2px 5px 0px rgba(0, 0, 0, 0.3);
  -moz-box-shadow: 0px 2px 5px 0px rgba(0, 0, 0, 0.3);
  box-shadow: 0px 2px 5px 0px rgba(0, 0, 0, 0.3);
  background: #fcfff4;
  background: -webkit-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
  background: -moz-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
  background: -o-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
  background: -ms-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
  background: linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
  filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#fcfff4', endColorstr='#b3bead', GradientType=0);
}
.slideThree input[type=checkbox]:checked + label {
  left: 43px;
}
input[type=checkbox] {
  visibility: hidden;
}
}
<div class="ondisplay">
  <div><strong>Test: </strong>
  </div>
  <div class="slideThree">
    <input type="checkbox" value="None" id="slideThree" name="check" checked/>
    <label for="slideThree"></label>
  </div>
</div>

答案 1 :(得分:0)

尝试以下代码,我已将label类添加到<div class="label"><strong>Test: </strong> </div>,并为其分配display:inline-block属性,并为slideThree DIV分配。

请查看以下代码段:

.ondisplay {
  display: inline-block;
}
/* SLIDE THREE */
.label{ display: inline-block;}
.slideThree {

  width: 80px;
  height: 26px;
  background: #333;
  margin: 10px auto;
  -webkit-border-radius: 50px;
  -moz-border-radius: 50px;
  border-radius: 50px;
  position: relative;
  -webkit-box-shadow: inset 0px 1px 1px rgba(0, 0, 0, 0.5), 0px 1px 0px rgba(255, 255, 255, 0.2);
  -moz-box-shadow: inset 0px 1px 1px rgba(0, 0, 0, 0.5), 0px 1px 0px rgba(255, 255, 255, 0.2);
  box-shadow: inset 0px 1px 1px rgba(0, 0, 0, 0.5), 0px 1px 0px rgba(255, 255, 255, 0.2);
   display: inline-block;
}
.slideThree:after {
  content: 'OFF';
  font: 12px/26px Arial, sans-serif;
  color: #000;
  position: absolute;
  right: 10px;
  z-index: 0;
  font-weight: bold;
  text-shadow: 1px 1px 0px rgba(255, 255, 255, .15);
}
.slideThree:before {
  content: 'ON';
  font: 12px/26px Arial, sans-serif;
  color: #00bf00;
  position: absolute;
  left: 10px;
  z-index: 0;
  font-weight: bold;
}
.slideThree label {
  display: block;
  width: 34px;
  height: 20px;
  -webkit-border-radius: 50px;
  -moz-border-radius: 50px;
  border-radius: 50px;
  -webkit-transition: all .4s ease;
  -moz-transition: all .4s ease;
  -o-transition: all .4s ease;
  -ms-transition: all .4s ease;
  transition: all .4s ease;
  cursor: pointer;
  position: absolute;
  top: 3px;
  left: 3px;
  z-index: 1;
  -webkit-box-shadow: 0px 2px 5px 0px rgba(0, 0, 0, 0.3);
  -moz-box-shadow: 0px 2px 5px 0px rgba(0, 0, 0, 0.3);
  box-shadow: 0px 2px 5px 0px rgba(0, 0, 0, 0.3);
  background: #fcfff4;
  background: -webkit-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
  background: -moz-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
  background: -o-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
  background: -ms-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
  background: linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
  filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#fcfff4', endColorstr='#b3bead', GradientType=0);
}
.slideThree input[type=checkbox]:checked + label {
  left: 43px;
}
input[type=checkbox] {
  visibility: hidden;
}
}
<div class="ondisplay">
  <div class="label"><strong>Test: </strong>
  </div>
  <div class="slideThree">
    <input type="checkbox" value="None" id="slideThree" name="check" checked/>
    <label for="slideThree"></label>
  </div>
</div>