I have a switch toggle button for an HTML page
<div>
<label class="switch" style="margin-left:14em;">
<h5>Profile</h5>
<input id="profile-toggle" checked type="checkbox">
<span class="slider round"></span>
</label>
</div>
It shows on the page like this:
I am trying to bring the label to the left side, similar to this:
Does anyone have an idea on how can I do this?
EDIT Toggle 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: #e61610;
-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: #29b6f6;
}
input:focus + .slider {
box-shadow: 0 0 1px #29b6f6;
}
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%;
}
PS
The toggle switch works fine, my problem is to bring the label to the left side, in front of the button.
Add this bunch of css code and you are all good.
.switch h5 {
position: absolute;
left: -14rem;
top: 0;
margin: 0.75rem;
}
Try this
/* The switch - the box around the slider */
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 28px;
margin-left:14em;
}
/* 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: green;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 1px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked+.slider {
background-color: green;
}
input:focus+.slider {
box-shadow: 0 0 1px #29b6f6;
}
input:checked+.slider:before {
-webkit-transform: translateX(2px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
/* Rounded sliders */
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
.switch h5 {
position: relative;
left: -200px;
top: -50%;
width: 175px;
}
<div>
<label class="switch">
<h5>Do you want to check</h5>
<input id="profile-toggle" checked type="checkbox">
<span class="slider round"></span>
</label>
</div>
<div>
<label class="switch">
<h5>Check Grammar and Spelling on Stackoverflow.com</h5>
<input id="profile-toggle" checked type="checkbox">
<span class="slider round"></span>
</label>
</div>
Related
Here is what I have been trying.
What should go into the javascript function myFunc() to get 'Switch-2' to enable [with gray background turning blue] when I click on 'Switch-1' and vice-versa. I'm fairly new to CSS or jQuery. I believe the solution should be using one of these or both. Would be of great help if someone can share the code to achieve this. Thanks in advance.
function myFunc() {
var checkBox1 = document.getElementById("SW1");
var checkBox2 = document.getElementById("SW2");
if (checkBox1.checked == true){
alert("SW2:"+checkBox2.checked);
//How to Enable SW2 with blue background
} else {
alert("SW2:"+checkBox2.checked);
//How to Disable SW2 with gray background
}
}
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-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: #2196F3;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
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%;
}
<html>
<body>
<h2>Toggle Switch</h2>
<label>Switch 1: </label>
<label class="switch">
<input type="checkbox" id="SW1" onclick="myFunc()">
<span class="slider"></span>
</label><br><br>
<label>Switch 2: </label>
<label class="switch">
<input type="checkbox" id="SW2">
<span class="slider round"></span>
</label>
</body>
</html>
Is this what you want to do? Try running the snippet.
function switchClicked(currentSwitchState, switchToToggle) {
var switchElement = document.getElementById(switchToToggle);
switchElement.checked = currentSwitchState.checked;
}
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-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: #2196F3;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
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%;
}
<html>
<body>
<h2>Toggle Switch</h2>
<label>Switch 1: </label>
<label class="switch">
<input id="s1" type="checkbox" onclick="switchClicked(this, 's2');">
<span class="slider"></span>
</label><br><br>
<label>Switch 2: </label>
<label class="switch">
<input id="s2" type="checkbox" onclick="switchClicked(this, 's1');">
<span class="slider round"></span>
</label>
</body>
</html>
I have a toggle switch below and I'm trying to position it somewhere else on my page but for some reason it doesn't go past this tiny bubble of space. I added the 'top' and left' elements under .switch{ but as I increase the numbers, the button stays put. Does anyone know how to fix this? There are position elements in the other classes and whenever I change the numbers, the toggle switch and text gets all messed up. I want to be able to move the button in its entirety.
html:
<label class="switch">
<input type="checkbox" id="togBtn">
<div class="slider round">
<!--ADDED HTML -->
<span class="on">ON</span>
<span class="off">OFF</span>
<!--END-->
</div>
</label>
css:
.switch {
position: absolute;
display: inline-block;
width: 90px;
height: 34px;
top: 500;
left: 600;
}
.switch input {display:none;}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ca2222;
-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: #2ab934;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked + .slider:before {
-webkit-transform: translateX(55px);
-ms-transform: translateX(55px);
transform: translateX(55px);
}
.on
{
display: none;
}
.on, .off
{
color: white;
position: absolute;
transform: translate(-50%,-50%);
top: 50%;
left: 50%;
font-size: 10px;
font-family: Verdana, sans-serif;
}
input:checked+ .slider .on
{display: block;}
input:checked + .slider .off
{display: none;}
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;}
top & left value need unit eg. 500px, 50rem or 10% but why you added position :absolute in .switch class, if position required for your case then ok, but using absolute positioning the switch is not proper solution
This question already has answers here:
How to center an element horizontally and vertically
(27 answers)
Closed 2 years ago.
I have some toggles with spans next to them (the spans with "True" and "False" in them). I want the spans to be centered in the vertical space that the toggles takes up. How can I best achieve this?
.question__label-group {
margin-bottom: 2rem;
}
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: 0.4s;
transition: 0.4s;
}
.slider:before {
position: absolute;
content: '';
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: 0.4s;
transition: 0.4s;
}
input:checked + .slider {
background-color: #2196f3;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196f3;
}
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%;
}
<div class="question__label-group">
<label class="switch">
<input type="radio" name="1" id="1" />
<span class="slider"></span>
</label>
<span>False</span>
</div>
<div class="question__label-group">
<label class="switch">
<input type="radio" name="1" id="1" />
<span class="slider"></span>
</label>
<span>True</span>
</div>
The easiest way to do this would be to give the .question__label-group class the following styles: display: flex and align-items: center.
Updated your fiddle for you. You may also want to give your slider or span a margin left/right.
.question__label-group {
margin-bottom: 2rem;
display: flex;
align-items: center;
}
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: 0.4s;
transition: 0.4s;
}
.slider:before {
position: absolute;
content: '';
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: 0.4s;
transition: 0.4s;
}
input:checked + .slider {
background-color: #2196f3;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196f3;
}
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%;
}
<div class="question__label-group">
<label class="switch">
<input type="radio" name="1" id="1" />
<span class="slider"></span>
</label>
<span>False</span>
</div>
<div class="question__label-group">
<label class="switch">
<input type="radio" name="1" id="1" />
<span class="slider"></span>
</label>
<span>True</span>
</div>
update css:
.question__label-group {
display: flex;
align-items: center;
margin-bottom: 2rem;
}
Okay, I'm totally unaware how I can do this. If the title wasn't clear enough, let me better describe what I want to accomplish.
Right now it's showing metric and the input checked, once you click the toggle button, the white circle will move from right to left, I'd like to hide the text as the circle is hovering on top of it while moving from right to left instead of how it's being hidden right now.
And the same goes for imperial.
I'm trying to grasp how to do this, but I really can't wrap my mind around it.
.text { position: relative; top: 8px; padding: 0 7px; font-size: 14px; }
input + .slider .off { display:none; }
input:checked + .slider .off { display:block; }
input + .slider .on { display:block; }
input:checked + .slider .on { display:none; }
.on { float: right; }
.switch { position: relative; display: inline-block; width: 100px; height: 34px; }
.switch input {display:none;}
.slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; -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: #eb974e; }
input:focus + .slider { box-shadow: 0 0 1px #eb974e; }
input:checked + .slider:before {-webkit-transform: translateX(64px); -ms-transform: translateX(64px); transform: translateX(64px); }
.slider.round { border-radius: 34px; }
.slider.round:before { border-radius: 50%; }
<label class="switch">
<input type="checkbox" checked>
<span class="slider round">
<span class="text off">Metric</span>
<span class="text on">Imperial</span>
</span>
</label>
Change the text to Visibility: hidden; or Opacity: 0; and make it
position: absolute; so it doesn't interfere with your other text. do the same for the other one and you should be able to style the fade.
Using Display: none; does'nt allow you to transition it, it will always just pop out of existence.
I have this toggle switch which I want to bring into my site.
https://jsfiddle.net/njxfnfoa/
<div class="switch-container">
<label class="switch">
<input type="checkbox">
<div class="slider round"></div>
</label>
</div>
And the CSS is long, so I'll just keep that in the JSFiddle.
However as you can see it looks quite jagged. The PDF design I'm working to looks like this.
How can I better replicate this smooth border?
use border: 1px solid rgba(211,211,211, .8); so you have control over the Border Opacity (0.8 here)
.switch-container{
position:relative;
}
.switch {
position: relative;
display: inline-block;
width: 65px;
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: white;
-webkit-transition: .4s;
transition: .4s;
border: 1px solid rgba(211,211,211, .8);
}
.slider:before {
position: absolute;
content: "";
height: 32px;
width: 32px;
left: 0px;
background-color: #73c82b;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider {
background-color: white;
}
input:focus + .slider {
box-shadow: 0 0 1px white;
}
input:checked + .slider:before {
-webkit-transform: translateX(31px);
-ms-transform: translateX(31px);
transform: translateX(31px);
}
/* Rounded sliders */
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
<div class="switch-container">
<label class="switch">
<input type="checkbox">
<div class="slider round"></div>
</label>
</div>
You just need to fine tune the css in
.slider:before
a little bit: js fiddle