I've coded some toggle switches just using CSS (no JS). When previewing them via the Microsoft Explorer programme they come out as the default checkboxes.
I've applied these styles:
-webkit-appearance: none;
-moz-appearance: none;
-ms-appearance: none;
-o-appearance: none;
appearance: none;
to the CSS file, however I've been reading that Microsoft Edge doesn't "play by" the WebKit features.
Is there any way around this at all?
.toggle-switch{
-webkit-appearance: none;
-moz-appearance: none;
-ms-appearance: none;
-o-appearance: none;
appearance: none;
width: 4em;
height: 2em;
border-radius: 3em;
background-color: #DED;
transform: backgrond-color 0.09s ease-in-out;
position: relative;
cursor: pointer;
}
.toggle-switch:checked{
background: #0D0155;
cursor: pointer;
}
.toggle-switch::after{
content: "";
width: 2em;
height:2em;
background-color: white;
border-radius: 3em;
position: absolute;
transform: scale(0.5);
left: 0;
transition: left 0.5s ease-in-out;
cursor: pointer;
box-shadow: 0 0.1em rgba(0,0,0,0.5);
}
.toggle-switch:checked::after{
left: 2em;
cursor: pointer;
}
<input type="checkbox" class="toggle-switch" checked>
<input type="checkbox" class="toggle-switch">
What it should look like:
What is previews as:
A possible solution would be to style a label instead of styling the input and hide the input. Indeed, you can use the for attribute on the label to refer to the input, so that when you click on it, it toggles the checkbox. Then you won't have any more problems with checkboxes appearing because your input will be in display: none.
Here is an example:
.checkbox {
display: none;
}
.toggle-switch {
width: 4em;
height: 2em;
border-radius: 3em;
background-color: #DED;
transition: background-color 0.09s ease-in-out;
position: relative;
cursor: pointer;
display: inline-block;
}
.checkbox:checked + .toggle-switch {
background: #0D0155;
cursor: pointer;
}
.toggle-switch::after{
content: "";
width: 2em;
height:2em;
background-color: white;
border-radius: 3em;
position: absolute;
transform: scale(0.5);
left: 0;
transition: left 0.5s ease-in-out;
cursor: pointer;
box-shadow: 0 0.1em rgba(0,0,0,0.5);
}
.checkbox:checked + .toggle-switch::after{
left: 2em;
cursor: pointer;
}
<input type="checkbox" class="checkbox" id="checkbox-id" checked>
<label class="toggle-switch" for="checkbox-id"></label>
Related
I try to make an thumb with content inside but it's not working and i dont know why it's not working . Any one know to add content to thumb in chrome ?
here is my css
input[type='range']::-webkit-slider-thumb::after {
content: 'test';
color: red;
}
input[type='range'] {
-webkit-appearance: none;
width: 100%;
height: 40px;
background: var(--theme-special-dark);
outline: none;
-webkit-transition: 0.2s;
transition: opacity 0.2s;
position: relative;
}
input[type='range']::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 40px;
height: 38px;
background: var(--theme-text-black);
cursor: pointer;
position: relative;
}
input[type='range']::before {
content: 'HT In Bay';
color: black;
position: absolute;
z-index: 1;
font-size: 20px;
font-weight: 400;
left: 140px;
top: 7px;
}
I am trying to add a checkbox based toggle button on my webpage that is compatible with Opera mini extreme data-savings mode. Right now the button does not change its state after it is clicked.
Source of code: https://proto.io/freebies/onoff/
Note that this source when opened on Opera mini extreme mode is working fine.
The button:
<div class="onoffswitch" style="display:inline-block;align-items: center;">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch">
<label class="onoffswitch-label" for="myonoffswitch">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
this is the class definition
.onoffswitch {
position: relative; width: 90px;
-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: 30px; padding: 0; line-height: 30px;
font-size: 14px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold;
box-sizing: border-box;
}
.onoffswitch-inner:before {
content: "ON";
padding-left: 11px;
background-color: #34A7C1; color: #FFFFFF;
}
.onoffswitch-inner:after {
content: "OFF";
padding-right: 11px;
background-color: #EEEEEE; color: #999999;
text-align: right;
}
.onoffswitch-switch {
display: block; width: 18px; margin: 6px;
background: #FFFFFF;
position: absolute; top: 0; bottom: 0;
right: 56px;
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;
}
I thought that maybe the transition property from the class onoffswitch-switch style class might not be compatible with OBML but even removing it didn't help. Can you please tell me what changes need to be made so this starts working, or any other form of toggle button that works for Opera Mini Extreme mode.
<input type='checkbox' name='chkbox'/>
I want to change the checkbox height and width and also background color if possible
You can look at this piece of code for starters.
It uses the before element to create a custom shape and hide the default checkbox for two states 'checked' and 'unchecked'
Also give a label for that corresponding checkbox so that clicking this custom shape will actually trigger the hidden default checkbox.
body {
font-family: 'segoe ui';
background-color: white;
padding: 10px;
}
.space {
height: 10px;
}
.checkbox * {
box-sizing: border-box;
position: relative;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.checkbox {
display: inline-block;
}
.checkbox>input {
display: none;
}
.checkbox>label {
vertical-align: top;
font-size: 18px;
padding-left: 30px;
}
.checkbox>[type="checkbox"]+label:before {
color: #777;
content: '';
position: absolute;
left: 0px;
display: inline-block;
min-height: 20px;
height: 20px;
width: 20px;
border: 2px solid #777;
font-size: 15px;
vertical-align: top;
text-align: center;
transition: all 0.2s ease-in;
content: '';
}
.checkbox.radio-square>[type="checkbox"]+label:before {
border-radius: 0px;
}
.checkbox.radio-rounded>[type="checkbox"]+label:before {
border-radius: 25%;
}
.checkbox.radio-blue>[type="checkbox"]+label:before {
border: 2px solid #ccc;
}
.checkbox>[type="checkbox"]+label:hover:before {
border-color: lightgreen;
}
.checkbox>[type="checkbox"]:checked+label:before {
width: 8px;
height: 16px;
border-top: transparent;
border-left: transparent;
border-color: green;
border-width: 4px;
transform: rotate(45deg);
top: -4px;
left: 4px;
}
<div class="checkbox">
<input id="chkTest" type="checkbox" />
<label for="chkTest">Task one</label>
<div class="space">
</div>
<input id="chkTest1" type="checkbox" />
<label for="chkTest1">Task two</label>
</div>
I'm working on modifying an existing project which has some custom CSS styling for checkboxes. Currently the checkbox label is sitting to the right of the checkbox and I would like to move the checkbox to sit above the label. How can I achieve this using the following code?
<input type="checkbox" id="box-<%= tmp %>">
<label for="box-<%= tmp %>"><%= question.choiceAnswer[tmp] %></label>
input[type="checkbox"] { display: none; }
input[type="checkbox"] + label {
display: block;
position: relative;
padding-left: 35px;
margin-bottom: 20px;
text-align: center;
font-weight: 500;
font-size: 0.85em;
color: #232A33;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
}
input[type="checkbox"] + label:last-child { margin-bottom: 0; }
input[type="checkbox"] + label:before {
content: '';
display: block;
width: 20px;
height: 20px;
border: 1px solid #232A33;
position: absolute;
left: 0;
top: 0;
opacity: .6;
-webkit-transition: all .12s, border-color .08s;
transition: all .12s, border-color .08s;
}
input[type="checkbox"]:checked + label:before {
width: 10px;
top: -5px;
left: 5px;
border-radius: 0;
opacity: 1;
border-top-color: transparent;
border-left-color: transparent;
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
}
From my understanding, the typical checkbox is no hidden and new checkbox is added through CSS, but I'm not sure how to move the label to be below the checkbox since the seem to be a single piece. Can I achieve this by modifying the above code or am I better off starting from scratch?
Thanks in advance!
Remove the left padding for the label and the absolute positioning for the pseudo element :before
input[type="checkbox"] { display: none; }
input[type="checkbox"] + label {
position: relative;
margin-bottom: 20px;
text-align: center;
font-weight: 500;
font-size: 0.85em;
color: #232A33;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
}
input[type="checkbox"] + label:last-child { margin-bottom: 0; }
input[type="checkbox"] + label:before {
content: '';
display: block;
width: 20px;
height: 20px;
border: 1px solid #232A33;
opacity: .6;
-webkit-transition: all .12s, border-color .08s;
transition: all .12s, border-color .08s;
}
input[type="checkbox"]:checked + label:before {
width: 10px;
top: -5px;
left: 5px;
border-radius: 0;
opacity: 1;
border-top-color: transparent;
border-left-color: transparent;
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
}
<input type="checkbox" id="box">
<label for="box">Label</label>
How do I align a label and a custom checkbox? The code snippet is below. I want to make sure that the label and switch are vertically aligned and I should also have the ability to use any text in the label. I cannot use the position:absolute method to align the label since the text might change and I dont want any overlap.
.switch {
position: relative;
display: inline-block;
}
.switch input[type="checkbox"] {
-webkit-appearance: none;
-moz-appearance: none;
-ms-appearance: none;
-o-appearance: none;
appearance: none;
cursor: pointer;
outline: none;
height: 25px;
width: 50px;
border-radius: 25px;
background-color: #999;
}
.switch input[type="checkbox"]:checked {
background-color: green;
}
.switch input[type="checkbox"]:after {
position: absolute;
content: ' ';
height: 16.66667px;
width: 16.66667px;
top: 6.5px;
right: 33px;
border-radius: 11.11111px;
background: #fff;
}
.switch input[type="checkbox"]:checked::after {
right: 6px;
}
<div class="switch">
<label for="checkbox1">Test Test Test Test</label>
<input type="checkbox" id="checkbox1">
</div>
You should be able to use the vertical-align property on the input (ex: vertical-align: middle;):
.switch {
position: relative;
display: inline-block;
}
.switch input[type="checkbox"] {
-webkit-appearance: none;
-moz-appearance: none;
-ms-appearance: none;
-o-appearance: none;
appearance: none;
cursor: pointer;
outline: none;
height: 25px;
width: 50px;
border-radius: 25px;
background-color: #999;
vertical-align: middle;
}
.switch input[type="checkbox"]:checked {
background-color: green;
}
.switch input[type="checkbox"]:after {
position: absolute;
content: ' ';
height: 16.66667px;
width: 16.66667px;
top: 6.5px;
right: 33px;
border-radius: 11.11111px;
background: #fff;
}
.switch input[type="checkbox"]:checked::after {
right: 6px;
}
<div class="switch">
<label for="checkbox1">Test Test Test Test</label>
<input type="checkbox" id="checkbox1">
</div>