How do I create a switch toggle input where a "Y" and "N" appears 'above' the input?
In the snippet below, I have a problem where the z-index of the "Y" and "N" covers the input so it is only toggle-able if you click around the z-indexed spans.
Additionally, I would like the letters to change color when the checkbox is toggled, but that is a secondary issue, I think.
body, html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}
.switch__input {
position: absolute;
top: 0;
left: 0;
width: 90px;
height: 50px;
opacity: 0;
z-index: 0;
}
.switch__label:before {
content: '';
text-align: center;
position: absolute;
color: red;
top: 5px;
left: 0;
width: 90px;
height: 35px;
background-color: rgba(0, 0, 0, 0.26);
border-radius: 100px;
z-index: 0;
-webkit-transition: background-color 0.28s cubic-bezier(0.4, 0, 0.2, 1);
transition: background-color 0.28s cubic-bezier(0.4, 0, 0.2, 1);
}
.switch__label:after {
content: '';
color: white;
position: absolute;
text-align: center;
font-size: 24px;
padding: 4.4px 0;
top: -2px;
left: 0;
width: 50px;
height: 50px;
background-color: #2d95e5;
border-radius: 100px;
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 1px 5px 0 rgba(0, 0, 0, 0.12);
z-index: 0;
-webkit-transition: all 0.28s cubic-bezier(0.4, 0, 0.2, 1);
transition: all 0.28s cubic-bezier(0.4, 0, 0.2, 1);
-webkit-transition-property: left, background-color;
transition-property: left, background-color;
}
.switch__input:checked + .switch__label:before {
background-color: rgba(225, 225, 225, 0.5);
}
.switch__input:checked + .switch__label:after {
left: 40px;
content: '';
color: white;
background-color: #BFBFBF;
}
.yesnocontainer {
font-family: sans-serif;
display: flex;
width: 70px;
justify-content: space-evenly;
align-content: center;
}
.yes, .no {
font-size: 24px;
}
.yes {
position: relative;
color: black !important;
z-index: 999 !important;
}
.no {
position: relative;
color: black !important;
z-index: 999 !important;
}
<div class="switch">
<input type="checkbox" id="switch1" class="switch__input" checked>
<label for="switch1" class="switch__label"></label>
<span class="yesnocontainer">
<span class="yes">Y</span>
<span class="no">N</span>
</span>
</div>
.switch-toggle {
float: left;
background: gray;
}
.switch-toggle input {
position: absolute;
opacity: 0;
}
.switch-toggle input + label {
padding: 7px;
float:left;
color: #fff;
cursor: pointer;
}
.switch-toggle input:checked + .red {
background: red;
}
.switch-toggle input:checked + .green {
background: green;
}
<div class="switch-toggle switch-3 switch-candy">
<input id="on" name="state-d" class="green" type="radio" />
<label for="on" class="green" onclick="">ON</label>
<input id="off" name="state-d" class="red" type="radio" />
<label for="off" class="red" onclick="">OFF</label>
</div>
.switch__label must be visibled on screen, try this.
body, html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}
/* ADD .switch__label */
.switch__input,.switch__label {
position: absolute;
top: 0;
left: 0;
width: 90px;
height: 50px;
opacity: 0;
z-index: 0;
}
/*ADD*/
.switch__label{
z-index:2;
opacity:1;
}
.switch__label:before {
content: '';
text-align: center;
position: absolute;
color: red;
top: 5px;
left: 0;
width: 90px;
height: 35px;
background-color: rgba(0, 0, 0, 0.26);
border-radius: 100px;
z-index: 0;
-webkit-transition: background-color 0.28s cubic-bezier(0.4, 0, 0.2, 1);
transition: background-color 0.28s cubic-bezier(0.4, 0, 0.2, 1);
}
.switch__label:after {
content: '';
color: white;
position: absolute;
text-align: center;
font-size: 24px;
padding: 4.4px 0;
top: -2px;
left: 0;
width: 50px;
height: 50px;
background-color: #2d95e5;
border-radius: 100px;
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 1px 5px 0 rgba(0, 0, 0, 0.12);
z-index: 0;
-webkit-transition: all 0.28s cubic-bezier(0.4, 0, 0.2, 1);
transition: all 0.28s cubic-bezier(0.4, 0, 0.2, 1);
-webkit-transition-property: left, background-color;
transition-property: left, background-color;
}
.switch__input:checked + .switch__label:before {
background-color: rgba(225, 225, 225, 0.5);
}
.switch__input:checked + .switch__label:after {
left: 40px;
content: '';
color: white;
background-color: #BFBFBF;
}
.yesnocontainer {
font-family: sans-serif;
display: flex;
width: 70px;
justify-content: space-evenly;
align-content: center;
}
.yes, .no {
font-size: 24px;
}
.yes {
position: relative;
color: black !important;
z-index: 1;
}
/* ADD */
.switch__input:checked + .switch__label + .yesnocontainer > .no {
z-index:3;
}
.no {
position: relative;
color: black !important;
z-index: 1;
}
/* ADD */
.switch__input:not(:checked) + .switch__label + .yesnocontainer > .yes {
z-index:3;
}
<div class="switch">
<input type="checkbox" id="switch1" class="switch__input" checked>
<label for="switch1" class="switch__label"></label>
<span class="yesnocontainer">
<span class="yes">Y</span>
<span class="no">N</span>
</span>
</div>
Related
I would like to make the whole radio button clickable.
For example, when I click on the first one, it will toggle to 'OFF', and when I click again, it will switch back to 'ON', just like how normal toggle switch does.
Somehow it only will switch when I click on that specific radio button. You can see from my fiddle.
Fiddle
I appreciate your help. Thank you!
.switch {
background: white;
border-radius: 3px;
}
.switch-label {
position: relative;
z-index: 2;
float: left;
width: 58px;
line-height: 26px;
font-size: 11px;
color: rgba(255, 255, 255, 0.35);
text-align: center;
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.45);
cursor: pointer;
}
.switch-label:active {
font-weight: bold;
}
.switch-label-No {
padding-left: 2px;
}
.switch-label-Yes {
padding-right: 2px;
}
.switch-input {
display: none;
}
.switch-input:checked + .switch-label {
font-weight: bold;
color: rgba(0, 0, 0, 0.65);
}
.switch-input:checked + .switch-label-Yes ~ .switch-selection {
left: 60px;
background-color:red;
/* Note: left: 50%; doesn't transition in WebKit */
}
.switch-selection {
position: absolute;
z-index: 1;
left: 2px;
display: block;
width: 58px;
height: 22px;
border-radius: 3px;
background-color: #65bd63;
-webkit-box-shadow: inset 0 1px rgba(255, 255, 255, 0.5), 0 0 2px rgba(0, 0, 0, 0.2);
box-shadow: inset 0 1px rgba(255, 255, 255, 0.5), 0 0 2px rgba(0, 0, 0, 0.2);
-webkit-transition: left 0.15s ease-out;
-moz-transition: left 0.15s ease-out;
-ms-transition: left 0.15s ease-out;
-o-transition: left 0.15s ease-out;
transition: left 0.15s ease-out;
}
<div class="switch">
<input type="radio" class="switch-input" name="view" value="1" id="week" checked>
<label for="week" class="switch-label switch-label-No">ON</label>
<input type="radio" class="switch-input" name="view" value="month" id="month">
<label for="month" class="switch-label switch-label-Yes">OFF</label>
<span class="switch-selection"></span>
</div>
Hope this will help you:
HTML:
<label class="switch">
<input type="checkbox">
<span class="slider"></span>
</label>
CSS:
.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);
}
I have made this button by editing "Simple Hover Effect by Vincent Durand". But the problem is that some css in my blog is overlapping. So it is not aligning to middle correctly. I cant find which one it may be. I tried using !important tag in some places. But I guess it didn't work out. What I need to know where should I use !important in this code to align the button to middle? Or will I need a new css element to do that?
<!-- Awesome button css Start -->
.btn-margin {
margin-top: 1.6rem;
box-sizing: inherit;
text-align: center;
}
.btn {
-webkit-tap-high!importantr: transparent;
border-radius: 2px;
box-shadow: rgba(0, 0, 0, 0.14) 0 3px 3px 0, rgba(0, 0, 0, 0.12) 0 1px 7px 0, rgba(0, 0, 0, 0.2) 0 3px 1px -1px;
box-sizing: inherit;
color: white !important;
cursor: pointer;
display: inline-block;
height: auto;
letter-spacing: 0.5px;
line-height: 42px;
pointer-events: all;
position: relative;
text-decoration-line: none;
vertical-align: middle;
font-size: 1.6em;
padding: 0 2em;
transition: 800ms ease all;
}
.btn-green {
background-color: #1AAB8A;
}
.btn-green:hover {
background-color: #fff;
color: #1AAB8A !important;
}
.btn:before,.btn:after {
content: '';
position: absolute;
top: 0;
right: 0;
height: 2px;
width: 0;
transition: 400ms ease all;
}
.btn:after {
right: inherit;
top: inherit;
left: 0;
bottom: 0;
}
.btn-green:before,.btn-green:after {
background: #1AAB8A;
}
.btn:hover:before,.btn:hover:after {
width: 100%;
transition: 800ms ease all;
}
<!-- Awesome button css End -->
<div class="btn-margin">
<a class="btn btn-green" href="#">
Click Here To See Answers
</a>
</div>
Add *{box-sizing:border-box;} to ur css
<!-- Awesome button css Start -->
*{box-sizing:border-box;}
.btn-margin {
margin-top: 1.6rem;
box-sizing: inherit;
text-align: center;
}
.btn {
-webkit-tap-high!importantr: transparent;
border-radius: 2px;
box-shadow: rgba(0, 0, 0, 0.14) 0 3px 3px 0, rgba(0, 0, 0, 0.12) 0 1px 7px 0, rgba(0, 0, 0, 0.2) 0 3px 1px -1px;
box-sizing: inherit;
color: white !important;
cursor: pointer;
display: inline-block;
height: auto;
letter-spacing: 0.5px;
line-height: 42px;
pointer-events: all;
position: relative;
text-decoration-line: none;
vertical-align: middle;
font-size: 1.6em;
padding: 0 2em;
transition: 800ms ease all;
}
.btn-green {
background-color: #1AAB8A;
}
.btn-green:hover {
background-color: #fff;
color: #1AAB8A !important;
}
.btn:before,.btn:after {
content: '';
position: absolute;
top: 0;
right: 0;
height: 2px;
width: 0;
transition: 400ms ease all;
}
.btn:after {
right: inherit;
top: inherit;
left: 0;
bottom: 0;
}
.btn-green:before,.btn-green:after {
background: #1AAB8A;
}
.btn:hover:before,.btn:hover:after {
width: 100%;
transition: 800ms ease all;
}
<!-- Awesome button css End -->
<div class="btn-margin">
<a class="btn btn-green" href="#">
Click Here To See Answers
</a>
</div>
Use this CSS to center align the button
.btn-green {
background-color: #1AAB8A;
position: relative;
left: 50%;
transform: translateX(-50%);
}
<!-- Awesome button css Start -->.btn-margin {
margin-top: 1.6rem;
box-sizing: inherit;
position: relative;
left: 50%;
transform: translateX(-50%);
}
.btn {
-webkit-tap-high!importantr: transparent;
border-radius: 2px;
box-shadow: rgba(0, 0, 0, 0.14) 0 3px 3px 0, rgba(0, 0, 0, 0.12) 0 1px 7px 0, rgba(0, 0, 0, 0.2) 0 3px 1px -1px;
box-sizing: inherit;
color: white !important;
cursor: pointer;
display: inline-block;
height: auto;
letter-spacing: 0.5px;
line-height: 42px;
pointer-events: all;
position: relative;
text-decoration-line: none;
vertical-align: middle;
font-size: 1.6em;
padding: 0 2em;
transition: 800ms ease all;
}
.btn-green {
background-color: #1AAB8A;
position: relative;
left: 50%;
transform: translateX(-50%);
}
.btn-green:hover {
background-color: #fff;
color: #1AAB8A !important;
}
.btn:before,
.btn:after {
content: '';
position: absolute;
top: 0;
right: 0;
height: 2px;
width: 0;
transition: 400ms ease all;
}
.btn:after {
right: inherit;
top: inherit;
left: 0;
bottom: 0;
}
.btn-green:before,
.btn-green:after {
background: #1AAB8A;
}
.btn:hover:before,
.btn:hover:after {
width: 100%;
transition: 800ms ease all;
}
<!-- Awesome button css End -->.btn-green {}
<div class="btn-margin">
<a class="btn btn-green" href="https://myneobuxportal.blogspot.com/p/answer-gamer-quiz-v2.html">
Click Here To See Answers
</a>
</div>
add one parent div and set this div text-align: center;
<!-- Awesome button css Start -->
.demo{
text-align: center;
}
.btn-margin {
margin-top: 1.6rem;
box-sizing: inherit;
text-align: center;
}
.btn {
-webkit-tap-high!importantr: transparent;
border-radius: 2px;
box-shadow: rgba(0, 0, 0, 0.14) 0 3px 3px 0, rgba(0, 0, 0, 0.12) 0 1px 7px 0, rgba(0, 0, 0, 0.2) 0 3px 1px -1px;
box-sizing: inherit;
color: white !important;
cursor: pointer;
display: inline-block;
height: auto;
letter-spacing: 0.5px;
line-height: 42px;
pointer-events: all;
position: relative;
text-decoration-line: none;
vertical-align: middle;
font-size: 1.6em;
padding: 0 2em;
transition: 800ms ease all;
}
.btn-green {
background-color: #1AAB8A;
}
.btn-green:hover {
background-color: #fff;
color: #1AAB8A !important;
}
.btn:before,.btn:after {
content: '';
position: absolute;
top: 0;
right: 0;
height: 2px;
width: 0;
transition: 400ms ease all;
}
.btn:after {
right: inherit;
top: inherit;
left: 0;
bottom: 0;
}
.btn-green:before,.btn-green:after {
background: #1AAB8A;
}
.btn:hover:before,.btn:hover:after {
width: 100%;
transition: 800ms ease all;
}
<!-- Awesome button css End -->
<div class="Demo">
<div class="btn-margin">
<a class="btn btn-green" href="#">
Click Here To See Answers
</a>
</div>
</div>
In the end the problem was with the comment :(
I used html comment in css. That's why this happened. Thanks #CharuMaheshwari for mentioning that out. Also another thanks for other 3 answers. All of them worked.
With #CharuMaheshwari help this is the code I decided to use.
/* Awesome button css Start */
.btn-margin {
margin-top: 1.6rem;
box-sizing: inherit;
text-align: center;
}
.btn {
-webkit-tap-high!importantr: transparent;
border-radius: 2px;
box-shadow: rgba(0, 0, 0, 0.14) 0 3px 3px 0, rgba(0, 0, 0, 0.12) 0 1px 7px 0, rgba(0, 0, 0, 0.2) 0 3px 1px -1px;
box-sizing: inherit;
color: white !important;
cursor: pointer;
display: inline-block;
height: auto;
letter-spacing: 0.5px;
line-height: 42px;
pointer-events: all;
position: relative;
text-decoration-line: none;
vertical-align: middle;
font-size: 1.6em;
padding: 0 2em;
transition: 800ms ease all;
}
.btn-green {
background-color: #1AAB8A;
}
.btn-green:hover {
background-color: #fff;
color: #1AAB8A !important;
}
.btn:before,.btn:after {
content: '';
position: absolute;
top: 0;
right: 0;
height: 2px;
width: 0;
transition: 400ms ease all;
}
.btn:after {
right: inherit;
top: inherit;
left: 0;
bottom: 0;
}
.btn-green:before,.btn-green:after {
background: #1AAB8A;
}
.btn:hover:before,.btn:hover:after {
width: 100%;
transition: 800ms ease all;
}
/* Awesome button css End */
<div class="btn-margin">
<a class="btn btn-green" href="#">
Click Here To See Answers
</a>
</div>
Based on some code I found, I was able to make a kind of select menu with CSS only.
But now I have a problem with overlaying divs. It means, all divs below that "select menu" are moving down on hover or click that menu.
I've tried to play with z-index and positioning without success. I believe its quite simple but I'm seeing this code for many hours that I'm going crazy.
body {
width: 100%;
min-height: 500px;
margin: 3em auto;
font-family: 'Roboto', sans-serif;
letter-spacing: 1px;
}
h1 {
text-align: center;
color: #fff;
font-weight: 300;
font-size: 50px;
font-family: ProximaNovaSoftW03-Semibold;
letter-spacing: 0px;
text-shadow: 1px 1px rgba(0, 0, 0, 0.02), 2px 2px rgba(0, 0, 0, 0.02), 3px 3px rgba(0, 0, 0, 0.02), 4px 4px rgba(0, 0, 0, 0.02), 5px 5px rgba(0, 0, 0, 0.02), 6px 6px rgba(0, 0, 0, 0.02), 7px 7px rgba(0, 0, 0, 0.02);
}
h1 i {
position: relative;
font-size: 70px;
}
p {
text-align: center;
color: #000;
font-size: 14px;
margin-bottom: 2em;
line-height: 30px;
}
p img {
position: relative;
top: 8px;
right: 10px;
}
.paragrafo {
width: 100%;
background-color: #e1e1e1;
position: fixed;
}
.reserve-select {
position: relative;
overflow: hidden;
display: block;
margin: auto;
width: 330px;
height: 100%;
border-bottom: 0px;
border-radius: 3px;
font-size: 12px;
box-shadow: 0px 1em 1em -1.5em rgba(0, 0, 0, 0.5);
border: 1px solid rgba(0, 0, 0, 0.05);
}
.reserve-select:not(:hover)>i.toggle.icon-arrow-down {
display: block !important;
}
.reserve-select:not(:hover)>i.toggle.icon-arrow-up {
display: none !important;
}
.reserve-select:not(:hover) label.option>input:not(:checked)~span.title {
display: none !important;
}
.reserve-select>i.toggle {
position: absolute;
z-index: 4;
right: 1.5em;
top: 1.6em;
color: #ccc;
}
.reserve-select .title,
.reserve-select .placeholder {
position: relative;
display: block;
width: 100%;
height: 100%;
padding: 1.5em 2em;
background: white;
border-top: 1px solid rgba(0, 0, 0, 0.05);
cursor: pointer;
}
.reserve-select>input {
position: absolute;
left: 0px;
top: 0px;
z-index: 1;
width: 100%;
height: 100%;
display: block;
opacity: 0;
cursor: pointer;
}
.reserve-select>input:checked~i.toggle.icon-arrow-down {
display: none;
}
.reserve-select>input:checked~i.toggle.icon-arrow-up {
display: block;
}
.reserve-select>input:checked div.options label.option .title {
display: none !important;
}
.reserve-select>input:not(:checked) {
z-index: 4;
}
.reserve-select>input:not(:checked)~label.option>span.title {
display: none;
}
.reserve-select>input:not(:checked)~i.toggle.icon-arrow-up {
display: none;
}
.reserve-select>input:not(:checked)~i.toggle.icon-arrow-down {
display: block;
}
.reserve-select>input:disabled {
cursor: no-drop;
}
.reserve-select>span.placeholder {
position: relative;
z-index: 0;
display: inline-block;
width: 100%;
color: #999;
border-top: 0px;
}
.reserve-select label.option {
display: block;
overflow: hidden;
z-index: 1;
width: 100%;
transition: all 1s ease-out;
}
.reserve-select label.option span.title {
position: relative;
z-index: 2;
transition: background .3s ease-out;
}
.reserve-select label.option span.title i.icon {
padding-right: 8px;
color: #92a8d1;
}
.reserve-select label.option span.title:hover {
color: white;
background: #3498db;
box-shadow: inset 0px 1px 0px rgba(0, 0, 0, 0.1);
}
.reserve-select label.option input {
display: none;
}
.reserve-select label.option input:checked~span.title {
position: absolute;
display: block;
z-index: 3;
top: 0px;
font-size: 12px;
background: #fff;
border-top: 0px;
box-shadow: none;
color: inherit;
width: 100%;
}
.reserve-select label.option input:disabled~span.title {
background: #f9f9f9 !important;
color: #aaa;
}
.reserve-select label.option input:disabled~span.title:hover {
color: #aaa;
background: none;
cursor: no-drop;
}
<div class="reserve-select">
<input type="radio" name="option">
<span class="placeholder">Choose...</span>
<label class="option">
<input type="radio" name="option">
<span class="title">Speedometer</span>
</label>
<label class="option">
<input type="radio" name="option">
<span class="title">Fire</span>
</label>
<label class="option">
<input type="radio" name="option">
<span class="title">Badge</span>
</label>
</div>
<div class="paragrafo">
<p>Teste</p>
</div>
LINK: https://jsfiddle.net/o2gxgz9r/13580/
try this.Wrap options in a div and give position absolute for that div
body {
width: 100%;
min-height: 500px;
margin: 3em auto;
background: white;
/* IE6-9 fallback on horizontal gradient */
font-family: 'Roboto', sans-serif;
letter-spacing: 1px;
}
h1 {
text-align: center;
color: #fff;
font-weight: 300;
font-size: 50px;
font-family: ProximaNovaSoftW03-Semibold;
letter-spacing: 0px;
text-shadow: 1px 1px rgba(0, 0, 0, 0.02), 2px 2px rgba(0, 0, 0, 0.02), 3px 3px rgba(0, 0, 0, 0.02), 4px 4px rgba(0, 0, 0, 0.02), 5px 5px rgba(0, 0, 0, 0.02), 6px 6px rgba(0, 0, 0, 0.02), 7px 7px rgba(0, 0, 0, 0.02);
}
h1 i {
position: relative;
font-size: 70px;
}
p {
text-align: center;
color: #000;
font-size: 14px;
margin-bottom: 2em;
line-height: 30px;
}
p img {
position: relative;
top: 8px;
right: 10px;
}
.paragrafo {
width:100%;
background-color: #e1e1e1;
z-index: 1;
}
.reserve-select {
position: relative;
//overflow: hidden;
display: block;
margin: auto;
width: 330px;
height: 100%;
border-bottom: 0px;
border-radius: 3px;
font-size: 12px;
box-shadow: 0px 1em 1em -1.5em rgba(0, 0, 0, 0.5);
border: 1px solid rgba(0, 0, 0, 0.05);
}
.reserve-select:not(:hover) > i.toggle.icon-arrow-down {
display: block !important;
}
.reserve-select:not(:hover) > i.toggle.icon-arrow-up {
display: none !important;
}
.reserve-select:not(:hover) label.option > input:not(:checked) ~ span.title {
display: none !important;
}
.reserve-select > i.toggle {
position: absolute;
z-index: 4;
right: 1.5em;
top: 1.6em;
color: #ccc;
}
.reserve-select .title,
.reserve-select .placeholder {
position: relative;
display: block;
width: 100%;
height: 100%;
padding: 1.5em 2em;
background: white;
border-top: 1px solid rgba(0, 0, 0, 0.05);
cursor: pointer;
}
.reserve-select > input {
position: absolute;
left: 0px;
top: 0px;
z-index: 1;
width: 100%;
height: 100%;
display: block;
opacity: 0;
cursor: pointer;
}
.reserve-select > input:checked ~ i.toggle.icon-arrow-down {
display: none;
}
.reserve-select > input:checked ~ i.toggle.icon-arrow-up {
display: block;
}
.reserve-select > input:checked div.options label.option .title {
display: none !important;
}
.reserve-select > input:not(:checked) {
z-index: 4;
}
.reserve-select > input:not(:checked) ~ label.option > span.title {
display: none;
}
.reserve-select > input:not(:checked) ~ i.toggle.icon-arrow-up {
display: none;
}
.reserve-select > input:not(:checked) ~ i.toggle.icon-arrow-down {
display: block;
}
.reserve-select > input:disabled {
cursor: no-drop;
}
.reserve-select > span.placeholder {
position: relative;
z-index: 0;
display: inline-block;
width: 100%;
color: #999;
border-top: 0px;
}
.reserve-select label.option {
display: block;
overflow: hidden;
z-index: 1;
width: 100%;
transition: all 1s ease-out;
}
.reserve-select label.option span.title {
position: relative;
z-index: 2;
transition: background .3s ease-out;
}
.reserve-select label.option span.title i.icon {
padding-right: 8px;
color: #92a8d1;
}
.reserve-select label.option span.title:hover {
color: white;
background: #3498db;
box-shadow: inset 0px 1px 0px rgba(0, 0, 0, 0.1);
}
.reserve-select label.option input {
display: none;
}
.reserve-select label.option input:checked ~ span.title {
position: absolute;
display: block;
z-index: 3;
top: 0px;
font-size: 12px;
background: #fff;
border-top: 0px;
box-shadow: none;
color: inherit;
width: 100%;
}
.reserve-select label.option input:disabled ~ span.title {
background: #f9f9f9 !important;
color: #aaa;
}
.reserve-select label.option input:disabled ~ span.title:hover {
color: #aaa;
background: none;
cursor: no-drop;
}
.options{
position:absolute;
top:50px;
width:100%;
}
<body>
<div class="reserve-select">
<input type="radio" name="option">
<span class="placeholder">Choose...</span>
<div class="options">
<label class="option">
<input type="radio" name="option">
<span class="title">Speedometer</span>
</label>
<label class="option">
<input type="radio" name="option">
<span class="title">Fire</span>
</label>
<label class="option">
<input type="radio" name="option">
<span class="title">Badge</span>
</label>
</div>
</div>
<div class="paragrafo">
<p>Teste</p>
</div>
I have the following code:
$(document).ready(function() {
$('.input').append('<div class="expander"></div>');
});
.input {
position: relative;
height: 5.85714rem
}
.input input {
outline: 0;
width: 100%;
border: none;
background: 0 0;
border-bottom: .07143rem solid rgba(0, 0, 0, .42);
font-size: 1.14286rem;
padding-bottom: .57143rem;
position: absolute;
bottom: 1.42857rem;
}
.input input:hover+label {
color: rgba(0, 0, 0, .54)
}
.input input:active,
.input input:active:hover,
.input input:focus,
.input input:focus:hover,
.input input:hover {
border-bottom: .14286rem solid rgba(0, 0, 0, .87);
padding-bottom: .5rem
}
.input input:active+label,
.input input:focus+label {
color: #304ffe;
font-size: .85714rem;
bottom: 3.85714rem
}
.input input:active+.expander,
.input input:focus+.expander {
width: 100%;
left: 0;
height: .14286rem
}
.input label {
color: rgba(0, 0, 0, .54);
font-size: 1.14286rem;
position: absolute;
left: 0;
bottom: 2.07143rem;
font-weight: 400;
}
.input .expander {
width: 0;
background: #304ffe;
position: absolute;
height: .07143rem;
left: 50%;
bottom: 1.42857rem;
-webkit-transition: all cubic-bezier(.4, 0, .2, 1) 3s;
transition: all cubic-bezier(.4, 0, .2, 1) 3s
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="floating-input input">
<input type="text" name="name" id="name">
<label for="name">Name</label>
</div>
In the above example, clicking on the input moves the label, but the border color doesn't change. I'm using jQuery 3.2.1.
.input {
position: relative;
height: 5.85714rem
}
.input input {
outline: 0;
width: 100%;
border: none;
background: 0 0;
border-bottom: .07143rem solid rgba(0, 0, 0, .42);
font-size: 1.14286rem;
padding-bottom: .57143rem;
position: absolute;
bottom: 1.42857rem;
}
.input input:hover+label {
color: rgba(0, 0, 0, .54)
}
.input input:active,
.input input:active:hover,
.input input:focus,
.input input:focus:hover,
.input input:hover {
border-bottom: .14286rem solid rgba(0, 0, 0, .87);
padding-bottom: .5rem
}
.input input:active+label,
.input input:focus+label {
color: #304ffe;
font-size: .85714rem;
bottom: 3.85714rem
}
.input input:active+.expander,
.input input:focus+.expander {
width: 100%;
left: 0;
height: .14286rem
}
.input label {
color: rgba(0, 0, 0, .54);
font-size: 1.14286rem;
position: absolute;
left: 0;
bottom: 2.07143rem;
font-weight: 400;
}
.input .expander {
width: 0;
background: #304ffe;
position: absolute;
height: .07143rem;
left: 50%;
bottom: 1.42857rem;
-webkit-transition: all cubic-bezier(.4, 0, .2, 1) 1s;
transition: all cubic-bezier(.4, 0, .2, 1) 1s
}
<div class="floating-input input">
<input type="text" name="name" id="name">
<div class="expander"></div>
<label for="name">Name</label>
</div>
Another example is where the label and .expander rules are basically switched in order. The label is stuck but the border changes. This time I added the <div class="expander"></div> manually but it should be automatic.
The expected result would be that the label is moved above the input and the border changes its color. So the expected result would basically be the 2 examples shown merged together.
What causes either effect to not work and how do I fix it?
You're using a CSS selector (the +) which depends on element order.
input:hover+.expander matches a .expander immediately after an input:hover, but not the other way around.
Instead of + it should be ~. With ~ the elements don't need to be directly behind the input.
$(document).ready(function() {
$('.input').append('<div class="expander"></div>');
});
.input {
position: relative;
height: 5.85714rem
}
.input input {
outline: 0;
width: 100%;
border: none;
background: 0 0;
border-bottom: .07143rem solid rgba(0, 0, 0, .42);
font-size: 1.14286rem;
padding-bottom: .57143rem;
position: absolute;
bottom: 1.42857rem;
}
.input input:hover~label {
color: rgba(0, 0, 0, .54)
}
.input input:active,
.input input:active:hover,
.input input:focus,
.input input:focus:hover,
.input input:hover {
border-bottom: .14286rem solid rgba(0, 0, 0, .87);
padding-bottom: .5rem
}
.input input:active~label,
.input input:focus~label {
color: #304ffe;
font-size: .85714rem;
bottom: 3.85714rem
}
.input input:active~.expander,
.input input:focus~.expander {
width: 100%;
left: 0;
height: .14286rem
}
.input label {
color: rgba(0, 0, 0, .54);
font-size: 1.14286rem;
position: absolute;
left: 0;
bottom: 2.07143rem;
font-weight: 400;
}
.input .expander {
width: 0;
background: #304ffe;
position: absolute;
height: .07143rem;
left: 50%;
bottom: 1.42857rem;
-webkit-transition: all cubic-bezier(.4, 0, .2, 1) 3s;
transition: all cubic-bezier(.4, 0, .2, 1) 3s
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="floating-input input">
<input type="text" name="name" id="name">
<label for="name">Name</label>
</div>
I recently saw a video and created this toggle button code
HTML:
<div class="display">
<label class="label toggle">
<input type="checkbox" class="toggle_input" />
<div class="toggle-control"></div>
</label>
</div>
CSS:
html {
font-size: 16px;
box-sizing: border-box;
}
body {
font-size: 1em;
font-family: arial, sans-serif;
}
.display {
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
.toggle .toggle-control {
-webkit-transition: 0.3s cubic-bezier(0.98, 0.99, 1, 0.99);
transition: 0.3s cubic-bezier(0.98, 0.99, 1, 0.99);
width: 4em;
height: 2em;
display: block;
border: 2px solid #8E8E93;
border-radius: 2em;
background-color: rgba(0, 0, 0, 0.06);
position: relative;
}
.toggle .toggle-control:after {
-webkit-transition: 0.3s cubic-bezier(0.98, 0.99, 1, 0.99);
transition: 0.3s cubic-bezier(0.98, 0.99, 1, 0.99);
content: "";
width: 2em;
height: 2em;
display: block;
background-color: #fff;
border-radius: 50%;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 3px 2px rgba(0, 0, 0, 0.4);
position: absolute;
top: 0;
left: 0;
}
.toggle input {
display: none;
}
.toggle input:checked + .toggle-control {
border-color: #4cd964;
background-color: #4cd964;
}
.toggle input:checked + .toggle-control:after {
left: 2em;
}
Now the i want to add a text left aligned to the toggle button but i am not able to do it.
Expected OUTPUT:
(Some Text) (Toggle Button)
Please help
Change the display:block of the div (which causes it to be displayed on a line of its own) to display:inline-block.
And I added vertical-align:middle in this example, but it's up to you how to want the controls to line out!
html {
font-size: 16px;
box-sizing: border-box;
}
body {
font-size: 1em;
font-family: 'Arial', sans-serif;
}
.display {
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
.toggle .toggle-control {
-webkit-transition: 0.3s cubic-bezier(0.98, 0.99, 1, 0.99);
transition: 0.3s cubic-bezier(0.98, 0.99, 1, 0.99);
width: 4em;
height: 2em;
display: inline-block; /* changed */
border: 2px solid #8E8E93;
border-radius: 2em;
background-color: rgba(0, 0, 0, 0.06);
position: relative;
vertical-align:middle; /* new; can be removed if desired */
}
.toggle .toggle-control:after {
-webkit-transition: 0.3s cubic-bezier(0.98, 0.99, 1, 0.99);
transition: 0.3s cubic-bezier(0.98, 0.99, 1, 0.99);
content: "";
width: 2em;
height: 2em;
background-color: #fff;
border-radius: 50%;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 3px 2px rgba(0, 0, 0, 0.4);
position: absolute;
top: 0;
left: 0;
}
.toggle input {
display: none;
}
.toggle input:checked + .toggle-control {
border-color: #4cd964;
background-color: #4cd964;
}
.toggle input:checked + .toggle-control:after {
left: 2em;
}
<div class="display">
<label class="label toggle">
Some text
<input type="checkbox" class="toggle_input" />
<div class="toggle-control"></div>
</label>
</div>