使div与文本位于同一行,在哪里放置display:inline-block?

时间:2016-01-26 11:16:09

标签: javascript html css

上下文:

我使用开/关开关按钮,我希望帮助文本与开关位于同一行。

现在看起来像这样:

enter image description here

我希望它看起来像这样:

enter image description here

我用Google搜索我可以use this是我的div风格:

display:inline-block;

我的代码:

html {
    position: relative;
    min-height: 100%;
}

body {
    margin: 0 0 100px;
    /* bottom = footer height */
    padding: 25px;
}
    
footer {
    background-color: #5B8CA8;
    position: absolute;
    left: 0;
    bottom: 0;
    height: 110px;
    width: 100%;
    overflow:hidden;
}

div.upp {
    background-color: #5B8CA8;
    position: absolute;
    left: 0;
    top: 0;
    height: 39px;
    width: 100%;
    overflow:hidden;
}

.onoffswitch {
    position: relative;
    width: 55px; 
    -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: 2px solid #999999;
    border-radius: 20px;
}

.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: 23px;
    padding: 0; 
    line-height: 23px;
    font-size: 11px; 
    color: white; 
    font-family: Trebuchet, Arial, sans-serif; 
    font-weight: bold;
    box-sizing: border-box;
}

.onoffswitch-inner:before {
    content: "PÅ";
    padding-left: 7px;
    background-color: #5B8CA8;
    color: #FFFFFF;
}

.onoffswitch-inner:after {
    content: "AV";
    padding-right: 10px;
    background-color: #EEEEEE;
    color: #999999;
    text-align: right;
}

.onoffswitch-switch {
    display: block;
    width: 15px;
    margin: 4px;
    background: #FFFFFF;
    position: absolute;
    top: 0;
    bottom: 0;
    right: 28px;
    border: 2px solid #999999;
    border-radius: 20px;
    transition: all 0.3s ease-in 0s; 
}

.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
    margin-left: 0;
}

.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
    right: 0px; 
}
<p>
Help Text
</p>
<div class="onoffswitch">
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitchT1" unchecked onclick="javascript:toggle1();">
    <label class="onoffswitch-label" for="myonoffswitchT1">
        <span class="onoffswitch-inner"></span>
        <span class="onoffswitch-switch"></span>
    </label>
</div>

我的问题:

我应该将display:inline-block;放在我的div风格中?

2 个答案:

答案 0 :(得分:2)

p和div都应该采用内联块样式。

当然,在实际项目中我不会设置样式属性 - 而是使用css类。

<p style="display: inline-block">
Help Text
</p>
<div class="onoffswitch" style="display: inline-block">
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitchT1" unchecked onclick="javascript:toggle1();">
    <label class="onoffswitch-label" for="myonoffswitchT1">
        <span class="onoffswitch-inner"></span>
        <span class="onoffswitch-switch"></span>
    </label>
</div>

答案 1 :(得分:1)

同时使用display : inline-block;&amp; vertical-align : middle;元素和p类上的.onoffswitch。这应该产生预期的效果!

演示:

&#13;
&#13;
html {
    position: relative;
    min-height: 100%;
}

body {
    margin: 0 0 100px;
    /* bottom = footer height */
    padding: 25px;
}
    
p {
    display : inline-block;
    vertical-align : middle;
}
    
footer {
    background-color: #5B8CA8;
    position: absolute;
    left: 0;
    bottom: 0;
    height: 110px;
    width: 100%;
    overflow:hidden;
}

div.upp {
    background-color: #5B8CA8;
    position: absolute;
    left: 0;
    top: 0;
    height: 39px;
    width: 100%;
    overflow:hidden;
}

.onoffswitch {
    position: relative;
    width: 55px; 
    display : inline-block;
    vertical-align : middle;
    -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: 2px solid #999999;
    border-radius: 20px;
}

.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: 23px;
    padding: 0; 
    line-height: 23px;
    font-size: 11px; 
    color: white; 
    font-family: Trebuchet, Arial, sans-serif; 
    font-weight: bold;
    box-sizing: border-box;
}

.onoffswitch-inner:before {
    content: "PÅ";
    padding-left: 7px;
    background-color: #5B8CA8;
    color: #FFFFFF;
}

.onoffswitch-inner:after {
    content: "AV";
    padding-right: 10px;
    background-color: #EEEEEE;
    color: #999999;
    text-align: right;
}

.onoffswitch-switch {
    display: block;
    width: 15px;
    margin: 4px;
    background: #FFFFFF;
    position: absolute;
    top: 0;
    bottom: 0;
    right: 28px;
    border: 2px solid #999999;
    border-radius: 20px;
    transition: all 0.3s ease-in 0s; 
}

.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
    margin-left: 0;
}

.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
    right: 0px; 
}
&#13;
<p>
Help Text
</p>
<div class="onoffswitch">
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitchT1" unchecked onclick="javascript:toggle1();">
    <label class="onoffswitch-label" for="myonoffswitchT1">
        <span class="onoffswitch-inner"></span>
        <span class="onoffswitch-switch"></span>
    </label>
</div>
&#13;
&#13;
&#13;

尝试this Fiddle

注意:

设置display : inline-block;&amp ;;这是一个非常糟糕的想法。所有p元素都是vertical-align : middle;

为你的p元素添加一个类是多么好的想法,如下所示:

<p class="help">
Help Text
</p>

现在,您可以像这样设置该元素的样式:

p.help {
    display : inline-block;
    vertical-align : middle;
}

这可以防止这些样式应用于所有p元素!

另见this Fiddle