在同一行显示文本和自定义切换

时间:2016-07-13 17:35:21

标签: html css html5 css3 inline

我正在尝试开发自定义切换按钮并在其前面附加文本。我希望文本和切换位于同一行。但是,文本在一行中,而切换在另一行。

工作代码:https://jsfiddle.net/nvarun123/x6v1nevw/1/

当我添加样式display:inline-block;时,切换圈的形状会受到干扰。我试图在Angular2中将其作为一个组件进行开发。 Angular2中的woking代码:http://plnkr.co/edit/4ZnrnJbSyFAp5c6Z0V7T?p=preview

HTML code:

   <div style="display:inline;">  
    Toggle1:
    <div class="onoffswitch" style="margin-bottom:5px;">
        <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked >
        <label class="onoffswitch-label" for="myonoffswitch">
            <span class="onoffswitch-inner"></span>
            <span class="onoffswitch-switch"></span>
        </label>
    </div>
    </div>

    <div style="display:inline;">
    Toggle2:
    <div class="onoffswitch" style="margin-bottom:5px;">
    <input type="checkbox" name="onoffswitch1" class="onoffswitch-checkbox" id="myonoffswitch1" >
        <label class="onoffswitch-label" for="myonoffswitch1">
            <span class="onoffswitch-inner"></span>
            <span class="onoffswitch-switch"></span>
        </label>
    </div>
    </div>  

CSS代码:

.onoffswitch {
    position: relative; width: 50px;
    -webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
}
.onoffswitch-checkbox {
    display: none;
}
.onoffswitch-label {
    display: block; overflow: hidden; cursor: pointer;
    border-radius: 10px;
}
.onoffswitch-inner {
    display: block; width: 200%; margin-left: -100%;
    transition: margin 0.3s ease-in 0s;
}
.onoffswitch-inner:before, .onoffswitch-inner:after {
    display: block; float: left; width: 50%; height: 22px; padding: 0; line-height: 22px;
    font-size: 10px; color: white; font-family: sans-serif; font-weight: bold;
    box-sizing: border-box;
}
.onoffswitch-inner:before {
    content: "";//ON
    padding-left: 8px;
    background-color: lightgreen; 
    color: pink;
}
.onoffswitch-inner:after {
    content: "";//OFF
    padding-right: 6px;
    background-color: red; 
    color: #F9FAFB;
    text-align: right;
}
.onoffswitch-switch {
    display: block; width: 20px; margin: 0px;
    background: #F9FAFB;
    position: absolute; top: 0; bottom: 0;
    right: 30px;
     border-radius: 20px;
    transition: all 0.3s ease-in 0s; 
    box-shadow: 0.276px 0.961px 2px 0px rgba(0, 0, 0, 0.1);
}
.onoffswitch-checkbox:checked  + .onoffswitch-label .onoffswitch-inner {
    margin-left: 0;

}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
    right: 0px; 

}

1 个答案:

答案 0 :(得分:0)

你的onoffswitch类应用于div元素,默认情况下它的显示值为'block'。默认情况下,块元素将显示在各自的行中。

将此样式添加到onoffswitch类:

display: inline-block;