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;
}
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'm trying to have the checkboxes appear below their slanted "span" label. However, the transform applied to the "span" is also applying to the custom checkbox created with the ":before" selector. How do you prevent the 45deg rotation to the custom checkbox, and have it appear directly below it's label?
.slanted_chkbx {
margin-top: 25px;
}
.slanted_check {
margin-left: 5px;
margin-right: 15px;
display: inline-block;
position: relative;
}
.slanted_check span {
position: absolute;
top: -20px;
left: .7em;
transform-origin: bottom left;
transform: rotate(-45deg);
}
.custom_check {
display: none;
}
.custom_check + span:before {
content: '';
display: block;
cursor: pointer;
width: 10px;
height: 10px;
left: 0;
top: 0;
position: absolute;
border: 2px solid #111111;
-webkit-transition: all .2s;
transition: all .2s;
}
.custom_check:checked + span:before {
width: 5px;
top: -2px;
left: 2px;
border-radius: 0;
opacity: 1;
border-top-color: transparent;
border-left-color: transparent;
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
}
<div class="slanted_chkbx">
<label class="slanted_check">
<input type="checkbox" class="custom_check" name="option_one" value="option_one">
<span>One</span>
</label>
<label class="slanted_check">
<input type="checkbox" class="custom_check" name="option_two" value="option_two">
<span>Two</span>
</label>
<label class="slanted_check">
<input type="checkbox" class="custom_check" name="option_three" value="option_three">
<span>Three</span>
</label>
</div>
You need to place the reverse transform on the base state of the pseudo-element, not on the hover state.
.slanted_chkbx {
margin-top: 25px;
}
.slanted_check {
margin-left: 15px;
margin-right: 15px;
display: inline-block;
position: relative;
}
.slanted_check span {
margin-right: -25px;
display: inline-block;
transform-origin: bottom left;
transform: rotate(-45deg);
}
.custom_check {
display: none;
}
.custom_check+span:before {
content: '';
display: block;
cursor: pointer;
width: 10px;
height: 10px;
left: -15px;
bottom: -10px;
position: absolute;
border: 2px solid #111111;
-webkit-transition: all .2s;
transition: all .2s;
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
}
.custom_check:checked+span:before {
width: 5px;
border-radius: 0;
opacity: 1;
border-top-color: transparent;
border-left-color: transparent;
}
<div class="slanted_chkbx">
<label class="slanted_check">
<input type="checkbox" class="custom_check" name="option_one" value="option_one">
<span>One</span>
</label>
<label class="slanted_check">
<input type="checkbox" class="custom_check" name="option_two" value="option_two">
<span>Two</span>
</label>
<label class="slanted_check">
<input type="checkbox" class="custom_check" name="option_three" value="option_three">
<span>Three</span>
</label>
</div>
I'm not entirely sure what effect you are going for in the checked state so I have not adjusted that.
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>
Is there a way to have the .switch__indicator cover each label without having to set a fixed width for the .switch container?
I'm using translate3d but this requires me to set the width of the container and I want to keep it dynamic.
.wrap {
display: flex;
}
.switch {
position: relative;
width: auto;
padding: 0 1rem;
}
.switch:before {
content: ' ';
position: absolute;
left: 0;
z-index: -1;
width: 100%;
height: 3rem;
border: 2px solid #eee;
border-radius: 30px;
}
.switch__label {
display: inline-block;
width: 2rem;
border: 3px solid;
padding: 1rem;
text-align: center;
cursor: pointer;
transition: color 200ms ease-out;
}
.switch__indicator {
width: 4rem;
height: 4rem;
position: absolute;
top: -.5rem;
left: 0;
background: green;
border-radius: 50%;
transition: transform 600ms cubic-bezier(0.02, 0.94, 0.09, 0.97), background 300ms cubic-bezier(0.17, 0.67, 0.14, 1.03);
transform: translate3d(1rem, 0, 0);
}
.switch input#one:checked~.switch__indicator {
transform: translate3d(1.2rem, 0, 0);
}
.switch input#two:checked~.switch__indicator {
transform: translate3d(5.5rem, 0, 0);
}
.switch input#three:checked~.switch__indicator {
transform: translate3d(9.6rem, 0, 0);
}
.switch input#four:checked~.switch__indicator {
transform: translate3d(14rem, 0, 0);
}
.switch input[type="radio"]:not(:checked),
.switch input[type="radio"]:checked {
display: none;
}
<div class="wrap">
<div class="switch">
<input id="one" type="radio" name='thing' value='a' checked="checked" />
<label for="one" class="switch__label">One</label>
<input id="two" type="radio" name='thing' value='b' />
<label for="two" class="switch__label">Two</label>
<input id="three" type="radio" name='thing' value='c' />
<label for="three" class="switch__label">Three</label>
<input id="four" type="radio" name='thing' value='d' />
<label for="four" class="switch__label">Four</label>
<div class="switch__indicator"></div>
</div>
</div>
Its a bit difficult to calculate the exact space between labels as label are inline elements which have a space around them to aligned, so better to use flexbox here to remove the space and then use margin to give space between them.
Now you will need to calculate the exact translateX value which will be sum of border value, width of label and the margin value between them.
Also you don't need of translate3d here just use translateX and use calc() to calculate the exact amount of space.
.wrap {
display: flex;
}
.switch {
position: relative;
width: auto;
padding: 0 1rem;
display: flex;
}
.switch:before {
content: ' ';
position: absolute;
left: 0;
z-index: -1;
width: 100%;
height: 3rem;
border: 2px solid #eee;
border-radius: 30px;
}
.switch__label {
display: inline-block;
width: 2rem;
border: 3px solid;
padding: 1rem;
text-align: center;
cursor: pointer;
transition: color 200ms ease-out;
margin-right: 4px;
}
.switch__indicator {
width: 4rem;
height: 4rem;
position: absolute;
bottom: 0;
left: 0;
background: green;
border-radius: 50%;
transition: transform 600ms cubic-bezier(0.02, 0.94, 0.09, 0.97), background 300ms cubic-bezier(0.17, 0.67, 0.14, 1.03);
transform: translate3d(1rem, 0, 0);
}
.switch__label:last-of-type {
margin-right: 0;
}
.switch input#one:checked~.switch__indicator {
transform: translateX(calc(1rem + 3px));
}
.switch input#two:checked~.switch__indicator {
transform: translateX(calc(5rem + 13px));
}
.switch input#three:checked~.switch__indicator {
transform: translateX(calc(9rem + 23px));
}
.switch input#four:checked~.switch__indicator {
transform: translateX(calc(13rem + 33px));
}
.switch input[type="radio"]:not(:checked),
.switch input[type="radio"]:checked {
display: none;
}
<div class="wrap">
<div class="switch">
<input id="one" type="radio" name='thing' value='a' checked="checked" />
<label for="one" class="switch__label">One</label>
<input id="two" type="radio" name='thing' value='b' />
<label for="two" class="switch__label">Two</label>
<input id="three" type="radio" name='thing' value='c' />
<label for="three" class="switch__label">Three</label>
<input id="four" type="radio" name='thing' value='d' />
<label for="four" class="switch__label">Four</label>
<div class="switch__indicator"></div>
</div>
</div>
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