简体   繁体   English

如何在html中为切换开关添加标题?

[英]How to add a title to a toggle switch in html?

I am trying to add a title to a toggle switch above it in html. 我试图在html上方的切换开关中添加标题。 However, it is getting hidden behind the switch itself. 但是,它被隐藏在开关本身的后面。 When I increase the font, I do see it. 当我增加字体时,我确实看到了。 But, I do not want to increase the font. 但是,我不想增加字体。 Following is the code snippet and its results: 以下是代码段及其结果:

HTML: HTML:

<label className="hrow switch form-group pull-right">
   <input type="checkbox"><span className="slider round"></span>Active</input>
</label>

CSS (for the switch): CSS(用于开关):

/* The switch - the box around the slider */ .switch {
    position: relative;
    display: inline-block;
    width: 60px;
    height: 34px;
}

/* Hide default HTML checkbox */
.switch input {
    display: none;
}

/* The slider */
.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: lightgray;
    -webkit-transition: .4s;
    transition: .4s;
}

    .slider:before {
        position: absolute;
        content: "";
        height: 26px;
        width: 26px;
        left: 4px;
        bottom: 4px;
        background-color: white;
        -webkit-transition: .4s;
        transition: .4s;
    }

input:checked + .slider {
    background-color: #85a3e0;
}

input:focus + .slider {
    box-shadow: 0 0 1px #85a3e0;
}

input:checked + .slider:before {
    -webkit-transform: translateX(26px);
    -ms-transform: translateX(26px);
    transform: translateX(26px);
}

/* Rounded sliders */
.slider.round {
    border-radius: 34px;
}

.slider.round:before {
    border-radius: 50%;
}

RESULTS: 结果:

results 结果

Try moving the label content outside of the input element and using the for attribute to ensure click pass through. 尝试将标签内容移到input元素之外,并使用for属性以确保点击通过。 This should be easier to style than what you were doing before. 这应该比以前做起来更容易样式化。

 label.switch input { display: block; } 
 <label class="switch" for="checky"> Active <input id="checky" type="checkbox"> </label> 

Note: You also have an error in your HTML. 注意:您的HTML也会出现错误。 className should be class (edit: Except if you are using React) className应该是class (edit:除非您正在使用React)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM