I'm having trouble getting a toggle switch from the w3c tutorial to line up with some text beside it.
this is the tutorial https://www.w3schools.com/howto/howto_css_switch.asp
and this is my code:
<div class="sliderWrapper">
<div><?php echo __('Postal Address');?> </div>
<label class="switch">
<input type="checkbox" name="data[SplashPage][firstname]">
<span class="slider"></span>
</label>
</div>
<div class="sliderWrapper">
<div><?php echo __('Postal Address');?> </div>
<label class="switch">
<input type="checkbox" name="data[SplashPage][lastname]">
<span class="slider"></span>
</label>
</div>
.sliderWrapper{display: inline-block;margin:24px 24px 24px 24px;}
.sliderWrapper div{display: inline-block;line-height:60px;}
.switch {
position: relative;
display: inline-block;
padding:0px;
width: 54px;
height: 28px;
}
/* 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: #d7d7d7;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 1px;
bottom: 1px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider {background-color: #1fb5ad;}
input:focus + .slider {box-shadow: 0 0 1px #1fb5ad;}
input:checked + .slider:before {-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);transform: translateX(26px);}
Can anyone help me out with making the text be vertically aligned with the switch - been banging my head on the table of hours now :-(
jsfiddle: https://jsfiddle.net/vfjgtaoh/
Add the line:
vertical-align: middle;
to your switch class, this will vertically align the contents
Fiddle: https://jsfiddle.net/tzv75zvk/
Add Vertical-align:middle; property to label tag and remove line-height for the div
.sliderWrapper div{
display: inline-block;
}
.switch {
position: relative;
display: inline-block;
padding:0px;
vertical-align:middle;
width: 54px;
height: 28px;
}
Link for reference
Related
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
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 the following radio button, I need the bigger circle to be 38px
input[type=radio] {
visibility: hidden;
}
.label {
font-weight: normal;
color: #333;
}
.label::before {
content: '';
position: absolute;
left: 0;
width: 38px;
height: 38px;
border: 1px solid #727272;
border-radius: 50%;
}
input[type=radio]:checked+label:after {
content: '';
position: absolute;
width: 38px;
height: 38px;
left: 0;
background: #0065bd;
border: none;
transform: scale(0.5);
border-radius: 50%;
}
<div class="container">
<input type="radio" id="radio1" value="on">
<label for="radio1" class="label">Yes</label>
</div>
Here is a fiddle, how can I align the label so it is aligned to the centered and pushed to the right of the circle?
Add .container{ line-height:38px} to have it centered (it seems that it was to the right already)
https://jsfiddle.net/8gubpzhq/
to move it to the right add this to the
.label {
font-weight: normal;
color: #333;
padding-left:5px;//add this line
}
https://jsfiddle.net/vszuu535/
You can add line-height:40px; to your .label to center it vertically. To move it over to the right more you can add padding-left:20px; (You can change the line-height and padding-left to fit your needs).
input[type=radio] {
visibility: hidden;
}
.label {
font-weight: normal;
color: #333;
line-height:40px;
padding-left:20px;
}
.label::before {
content: '';
position: absolute;
left: 0;
width: 38px;
height: 38px;
border: 1px solid #727272;
border-radius: 50%;
}
input[type=radio]:checked + label:after {
content: '';
position: absolute;
width: 38px;
height: 38px;
left: 0;
background: #0065bd;
border: none;
transform: scale(0.5);
border-radius: 50%;
}
<div class="container">
<input type="radio" id="radio1" value="on">
<label for="radio1" class="label">Yes</label>
</div>
Perhaps your code is over-complicating matters; as you want the input to be bigger, maybe you should focus the sizing etc on the input rather than the label?
See the snippet (the radio turns blue now since edit, adapted from this codepen. It's grey before click, blue after, centered, and indented from the edge).
Just a note: If you are going to use a default value (and only have one option) maybe a custom checkbox would be a more suitable choice? (Radios button are usually used in instances where the user would have 2 or more choices, but can only select one).. just a thought.
input[type="radio"] {
display: none;
width: 38px;
height: 38px;
border: 1px solid #727272;
}
input[type="radio"]+label span {
display: inline-block;
width: 38px;
height: 38px;
margin: 9px;
vertical-align: middle;
cursor: pointer;
border-radius: 50%;
background-color: grey;
}
input[type="radio"]:checked+label span {
content="";
background: #0065bd;
}
input[type="radio"]+label span,
input[type="radio"]:checked+label span {
-webkit-transition: background-color 0.4s linear;
-o-transition: background-color 0.4s linear;
-moz-transition: background-color 0.4s linear;
transition: background-color 0.4s linear;
}
<div class="container">
<input type="radio" id="radio1" name="radio">
<label for="radio1" class="label"><span></span>Yes</label>
<input type="radio" id="radio2" name="radio">
<label for="radio2" class="label"><span></span>No</label>
</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
I am looking to find an example of how to make a label / placeholder transition move up and out of placeholder position into a label position and vice versa..
Example: https://www.xero.com/us/signup/
General sibling selectors & :focus does the trick in a very simple way ;)
input{
position:absolute;
top:20px;
}
input ~ span{
transition:top .7s ease;
position:absolute;
top:20px;
}
input:focus ~ span{
top:0px;
}
<label>
<input>
<span>Text</span>
</label>
here is an example with multiple fields
https://jsfiddle.net/shLe3107/1/
hope this is enough else just ask
I found a good Codepen showing an example of how to do it in CSS.
HTML:
<div class="row">
<input id="name" type="text" name="name">
<label for="name">Full Name</label>
</div>
CSS:
.row {
position: relative;
margin-top: 35px;
}
input {
display: block;
padding: 8px 12px;
width: 100%;
max-width: 300px;
border-radius: 5px;
border: 0;
}
label {
position: absolute;
font-weight: 600;
color: #777777;
top: 50%;
left: 12px;
transform: translateY(-50%);
-ms-transform: translateY(-50%);
-webkit-transform: translateY(-50%);
cursor: text;
user-select: none;
transition: 0.15s ease-in-out;
}
input[data-empty="false"] + label,
input:valid + label,
input:focus + label {
top: -10px;
left: 0px;
font-size: 10px;
color: #ffffff;
}
Example:
https://codepen.io/sivan/pen/alKwf