Search code examples
cssmaterialize

Inner switch text MaterializeCSS


I have a simple Materialize switch element and I want to add some text inside.

MaterializeCSS example switch:

Current switch

This is the code:

<div class="switch">
<label>
  Off
  <input type="checkbox">
  <span class="lever"></span>
  On
</label>

Desired switch:

https://codepen.io/chiandet/pen/raNKbE

Its enough by just getting some text inside the switch and making it square; is it possible?

Thanks a lot!


Solution

  • https://jsfiddle.net/55L52yww/190/

    HTML

    <div class="switch">
    <label>
    <input type="checkbox" class="upsellActiveToggle" checked="checked"><span class="lever">
    <span class="off">Off</span>
    <span class="on">On</span>
    </span>
    </label>
    </div>
    

    CSS:

    .switch label input[type=checkbox]:checked+.lever {
        background-color: #c3ccd9;
        padding: 3px 0 0 10px;
    }
    .switch label .lever {
        content: "";
        display: inline-block;
        position: relative;
        width: 60px;
        height: 21px;
        background-color: rgba(0,0,0,0.38);
        border-radius: 0;
        margin-right: 10px;
        transition: background 0.3s ease;
        vertical-align: middle;
        margin: 0 16px;
    }
    .switch label .lever:before, .switch label .lever:after {
        content: "";
        position: absolute;
        display: inline-block;
        width: 22px;
        height: 22px;
        border-radius: 0;
        left: 0;
        top: -1px;
        transition: left 0.3s ease, background .3s ease, box-shadow 0.1s ease, transform .1s ease;
    }
    .switch label input[type=checkbox]:checked+.lever:before, .switch label input[type=checkbox]:checked+.lever:after {
        left: 37px;
    }
    .switch label input[type=checkbox]+.lever span.off, .switch label input[type=checkbox]+.lever span.off {
        display: none;
    }
    .switch label input[type=checkbox]+.lever span.on, .switch label input[type=checkbox]+.lever span.on {
        display: inline-block;
        padding: 3px 0 0 15px;
    }
    
    .switch label input[type=checkbox]:checked+.lever span.on, .switch label input[type=checkbox]:checked+.lever span.on {
        display: none;
    }
    .switch label input[type=checkbox]:checked+.lever span.off, .switch label input[type=checkbox]:checked+.lever span.off {
        display: inline-block;
    }
    .switch label input[type=checkbox]+.lever {
        background-color: #111;
        padding: 3px 0 0 15px;
    }
    

    You probably need to do some more styling to have what you actually need