【发布时间】:2015-08-03 16:14:37
【问题描述】:
在我的 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>
有了这些,标签“测试”和滑块将出现在另一个左对齐的下方。我需要它们出现在同一行,首先是标签,然后是滑块。我试过display: inline,但没有用。我对 CSS 还是很陌生。有人可以帮忙吗?
编辑:这是我提到的小提琴 - http://jsfiddle.net/CXn3v/
【问题讨论】:
-
标签
test是一个普通的div,显示为块状,会占据整行。我主要是好奇您为什么不使用 HTML 中已经存在的实际label元素。这将使文档至少在语义上更正确。 -
@GolezTrol 我已经试过了。该空标签用于滑块,而不是复选框本身。