I want the text Immunization coverage with a purple background to be on the left, while the text HIT and the switch are aligned to the right.
Also I would like the switch to be on the left of HIT.
How can I do?
#legendRectContainer {
border: 1px solid black;
}
/* The switch - the box around the slider */
.switch {
position: relative;
display: inline-block;
width: 26px;
height: 15px;
background-color: lime;
margin-top: 3.5px;
}
/* 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: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 11px;
width: 11px;
left: 2px;
bottom: 2px;
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(11px);
-ms-transform: translateX(11px);
transform: translateX(11px);
}
/* Rounded sliders */
.slider.round {
border-radius: 15px;
}
.slider.round:before {
border-radius: 50%;
}
#legendRectRow {
background-color: yellow;
display: flex;
align-items: center;
}
#titleLegendRectRow {
float: left;
}
#switchTitleLegendRectRow {
background-color: orange;
float: right;
}
#switchLegendRectRow {
text-align: right;
float: right;
}
#switchAndHit {
border: 1px solid green;
}
#svgLegendRectRect text {
font-size: 7pt;
font-weight: 100;
}
.legendLinAxis path, .legendLinAxis line {
fill: none;
stroke: none;
shape-rendering: crispEdges;
}
.legendLinG .tick line {
stroke: black;
stroke-width: 1px;
}
.chartTitle {
font-weight: 500;
margin: 0;
background-color: DeepPink;
padding: 5px;
}
<div class='columnFooter' id='legendRectContainer'>
<div id='legendRectRow'>
<div class='colTitleRowLegendRect' id='titleLegendRectRow'>
<p class='chartTitle'>Immunization coverage</p>
</div>
<div class='colTitleRowLegendRect' id='switchTitleLegendRectRow'>HIT</div>
<div class='colTitleRowLegendRect' id='switchLegendRectRow'>
<label class="switch">
<input type="checkbox" id='legendRectSwitchButton' autocomplete='off'>
<span class="slider round"></span>
</label>
</div>
</div>
<div id='legend-rect'></div>
</div>
The problem is you should not use floats along with flexbox:
#legendRectRow {
background-color: yellow;
display: block;
overflow: hidden;
}
#legendRectContainer {
border: 1px solid black;
}
/* The switch - the box around the slider */
.switch {
position: relative;
display: inline-block;
width: 26px;
height: 15px;
background-color: lime;
margin-top: 3.5px;
}
/* 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: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 11px;
width: 11px;
left: 2px;
bottom: 2px;
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(11px);
-ms-transform: translateX(11px);
transform: translateX(11px);
}
/* Rounded sliders */
.slider.round {
border-radius: 15px;
}
.slider.round:before {
border-radius: 50%;
}
#legendRectRow {
background-color: yellow;
display: block;
overflow: hidden;
}
#titleLegendRectRow {
float: left;
}
#switchTitleLegendRectRow {
background-color: orange;
float: right;
padding: 5px;
}
#switchLegendRectRow {
text-align: right;
float: right;
}
#switchAndHit {
border: 1px solid green;
}
#svgLegendRectRect text {
font-size: 7pt;
font-weight: 100;
}
.legendLinAxis path,
.legendLinAxis line {
fill: none;
stroke: none;
shape-rendering: crispEdges;
}
.legendLinG .tick line {
stroke: black;
stroke-width: 1px;
}
.chartTitle {
font-weight: 500;
margin: 0;
background-color: DeepPink;
padding: 5px;
}
<div class='columnFooter' id='legendRectContainer'>
<div id='legendRectRow'>
<div class='colTitleRowLegendRect' id='titleLegendRectRow'>
<p class='chartTitle'>Immunization coverage</p>
</div>
<div class='colTitleRowLegendRect' id='switchTitleLegendRectRow'>HIT</div>
<div class='colTitleRowLegendRect' id='switchLegendRectRow'>
<label class="switch">
<input type="checkbox" id='legendRectSwitchButton' autocomplete='off'>
<span class="slider round"></span>
</label>
</div>
</div>
<div id='legend-rect'></div>
</div>
i just remove display:flex and just add overflow:auto to #legendRectRow id
#legendRectContainer {
border: 1px solid black;
}
/* The switch - the box around the slider */
.switch {
position: relative;
display: inline-block;
width: 26px;
height: 15px;
background-color: lime;
margin-top: 3.5px;
}
/* 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: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 11px;
width: 11px;
left: 2px;
bottom: 2px;
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(11px);
-ms-transform: translateX(11px);
transform: translateX(11px);
}
/* Rounded sliders */
.slider.round {
border-radius: 15px;
}
.slider.round:before {
border-radius: 50%;
}
#legendRectRow {
background-color: yellow;
overflow:auto;
align-items: center;
}
#titleLegendRectRow {
float: left;
}
#switchTitleLegendRectRow {
background-color: orange;
float: right;
}
#switchLegendRectRow {
text-align: right;
float: right;
}
#switchAndHit {
border: 1px solid green;
}
#svgLegendRectRect text {
font-size: 7pt;
font-weight: 100;
}
.legendLinAxis path, .legendLinAxis line {
fill: none;
stroke: none;
shape-rendering: crispEdges;
}
.legendLinG .tick line {
stroke: black;
stroke-width: 1px;
}
.chartTitle {
font-weight: 500;
margin: 0;
background-color: DeepPink;
padding: 5px;
}
<div class='columnFooter' id='legendRectContainer'>
<div id='legendRectRow'>
<div class='colTitleRowLegendRect' id='titleLegendRectRow'>
<p class='chartTitle'>Immunization coverage</p>
</div>
<div class='colTitleRowLegendRect' id='switchTitleLegendRectRow'>HIT</div>
<div class='colTitleRowLegendRect' id='switchLegendRectRow'>
<label class="switch">
<input type="checkbox" id='legendRectSwitchButton' autocomplete='off'>
<span class="slider round"></span>
</label>
</div>
</div>
<div id='legend-rect'></div>
</div>
tell me if any other question
Related
My problem is, they overlap each other: https://i.stack.imgur.com/ltZ8w.png
I need to do like this: https://i.stack.imgur.com/roBun.png
What can I do? I just need to line text with my input checkbox, as I showed in my picture, any suggestions?
.round {
position: relative;
margin-left: 50px;
}
.round label {
background-color: #fff;
border: 1px solid #ccc;
border-radius: 50%;
cursor: pointer;
height: 28px;
left: 0;
position: absolute;
top: 0;
width: 28px;
}
.round label:after {
border: 2px solid #fff;
border-top: none;
border-right: none;
content: "";
height: 6px;
left: 7px;
opacity: 0;
position: absolute;
top: 8px;
transform: rotate(-45deg);
width: 12px;
}
.round input[type="checkbox"] {
visibility: hidden;
}
.round input[type="checkbox"]:checked+label {
background-color: #66bb6a;
border-color: #66bb6a;
text-decoration: line-through;
color: #3CC070;
}
.tasks input[type="checkbox"]:checked+label {
border-color: #66bb6a;
text-decoration: line-through;
color: #3CC070;
}
.round input[type="checkbox"]:checked+label:after {
opacity: 1;
}
<div>
<div> <input type="checkbox" class="input" checked>
<label>Make some cash</label>
</div>
<div class="round">
<input type="checkbox" id="checkbox" />
<label for="checkbox" class="text"> Make</label>
</div>
</div>
you can add pseudo Before for label and add text inside it.
remove label content <label for="checkbox" class="text"></label>
label.text:before {
content: "click";
display: inline-block;
left: 37px;
position: absolute;
top: 4px;
}
.round {
position: relative;
margin-left: 50px;
}
.round label {
background-color: #fff;
border: 1px solid #ccc;
border-radius: 50%;
cursor: pointer;
height: 28px;
left: 0;
position: absolute;
top: 0;
width: 28px;
}
label.text:before {
content: "click";
display: inline-block;
left: 37px;
position: absolute;
top: 4px;
}
.round label:after {
border: 2px solid #fff;
border-top: none;
border-right: none;
content: "";
height: 6px;
left: 7px;
opacity: 0;
position: absolute;
top: 8px;
transform: rotate(-45deg);
width: 12px;
}
.round input[type="checkbox"] {
visibility: hidden;
}
.round input[type="checkbox"]:checked+label {
background-color: #66bb6a;
border-color: #66bb6a;
text-decoration: line-through;
color: #3CC070;
}
.tasks input[type="checkbox"]:checked+label {
border-color: #66bb6a;
text-decoration: line-through;
color: #3CC070;
}
.round input[type="checkbox"]:checked+label:after {
opacity: 1;
}
<div>
<div> <input type="checkbox" class="input" checked>
<label>Make some cash</label>
</div>
<div class="round">
<input type="checkbox" id="checkbox" />
<label for="checkbox" class="text"></label>
</div>
</div>
I am designing an app in Unqork. The issue I have is how to shape the radio button to look like the regular Learn More button. I only intend to style the radio button instead of making it into a multiple choice.
The style of the regular button is
.abc-quote-option-cta {
bottom: 38px;
left: 0;
right: 0;
padding-top: 24px;
position: absolute; }
.abc-quote-option-cta:before {
background-color: #979797;
content: '';
display: block;
height: 1px;
left: 62px;
opacity: .36;
position: absolute;
right: 56px;
top: 0;
}
.abc-quote-option-cta .btn.btn-primary {
border-radius: 6px;
border: solid 2px #01a7e1;
background-color: #FFFFFF;
border-radius: 6px;
color: #01a7e1;
display: block;
font-size: 1.8rem;
line-height: 1.44;
margin: 0 auto;
padding: 10px;
text-align: center;
transition: background-color .25s ease, width .13s ease-in, color .18s ease-out ;
width: 188px;
}
.abc-quote-option-cta .btn.btn-primary:hover,
.abc-quote-option-cta .btn.btn-primary:focus {
background-color: #0099cc;
color: #FFFFFF;
width: 251px;
}
Your help is very much appreciated. Thanks!
I hope this snippet will help you.
form {
max-width: 250px;
position: relative;
margin: 50px auto 0;
font-size: 15px;
}
.radiobtn {
position: relative;
display: block;
}
.radiobtn label {
display: block;
background: rgba(0, 0, 0, 0.2);
color: #000;
border-radius: 5px;
padding: 10px 20px;
border: 2px solid grey;
margin-bottom: 5px;
cursor: pointer;
}
.radiobtn label:after, .radiobtn label:before {
content: "";
position: absolute;
right: 11px;
top: 11px;
width: 20px;
height: 20px;
border-radius: 3px;
background:grey;
}
.radiobtn label:before {
background: transparent;
transition: 0.1s width cubic-bezier(0.075, 0.82, 0.165, 1) 0s, 0.3s height cubic-bezier(0.075, 0.82, 0.165, 2) 0.1s;
z-index: 2;
overflow: hidden;
background-repeat: no-repeat;
background-size: 13px;
background-position: center;
width: 0;
height: 0;
background-image: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxNS4zIDEzLjIiPiAgPHBhdGggZmlsbD0iI2ZmZiIgZD0iTTE0LjcuOGwtLjQtLjRhMS43IDEuNyAwIDAgMC0yLjMuMUw1LjIgOC4yIDMgNi40YTEuNyAxLjcgMCAwIDAtMi4zLjFMLjQgN2ExLjcgMS43IDAgMCAwIC4xIDIuM2wzLjggMy41YTEuNyAxLjcgMCAwIDAgMi40LS4xTDE1IDMuMWExLjcgMS43IDAgMCAwLS4yLTIuM3oiIGRhdGEtbmFtZT0iUGZhZCA0Ii8+PC9zdmc+);
}
input[type="radio"] {
display: none;
position: absolute;
width: 100%;
appearance: none;
}
input[type="radio"]:checked + label {
background:grey;
animation-name: blink;
animation-duration: 1s;
border-color: $accentcolor;
}
input[type="radio"]:checked + label:after {
background: $accentcolor;
}
input[type="radio"]:checked + label:before {
width: 20px;
height: 20px;
}
<form>
<div class="radiobtn">
<input type="radio" id="huey"
name="drone" value="huey" checked />
<label for="huey">Learn More</label>
</div>
<div class="radiobtn">
<input type="radio" id="dewey"
name="drone" value="dewey" />
<label for="dewey">Learn More</label>
</div>
</form>
Custom radio button without check mark.
form {
max-width: 200px;
position: relative;
margin: 50px auto 0;
font-size: 15px;
}
.radiobtn {
position: relative;
display: block;
text-align:center;
}
.radiobtn label {
display: block;
background:none;
color:#01a7e1;
border-radius: 5px;
padding: 10px 20px;
border: 2px solid #01a7e1;
margin-bottom: 5px;
cursor: pointer;
font-family:Arial, Helvetica, sans-serif;
}
input[type="radio"] {
display: none;
position: absolute;
width: 100%;
appearance: none;
}
input[type="radio"]:checked + label {
background:#01a7e1;
color:#fff;
}
<form>
<div class="radiobtn">
<input type="radio" id="huey"
name="drone" value="huey" checked />
<label for="huey">Learn More</label>
</div>
<div class="radiobtn">
<input type="radio" id="dewey"
name="drone" value="dewey" />
<label for="dewey">Learn More</label>
</div>
</form>
I hope this will help you.
form {
max-width: 250px;
position: relative;
margin: 50px auto 0;
font-size: 15px;
}
.radiobtn {
position: relative;
display: block;
}
.radiobtn label {
display: block;
background: rgba(0, 0, 0, 0.2);
color: #000;
border-radius: 5px;
padding: 10px 20px;
border: 2px solid grey;
margin-bottom: 5px;
cursor: pointer;
}
.radiobtn label:after,
.radiobtn label:before {
content: "";
position: absolute;
right: 11px;
top: 11px;
width: 20px;
height: 20px;
border-radius: 3px;
background: grey;
}
.radiobtn label:before {
background: transparent;
transition: 0.1s width cubic-bezier(0.075, 0.82, 0.165, 1) 0s, 0.3s height cubic-bezier(0.075, 0.82, 0.165, 2) 0.1s;
z-index: 2;
overflow: hidden;
background-repeat: no-repeat;
background-size: 13px;
background-position: center;
width: 0;
height: 0;
background-image: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxNS4zIDEzLjIiPiAgPHBhdGggZmlsbD0iI2ZmZiIgZD0iTTE0LjcuOGwtLjQtLjRhMS43IDEuNyAwIDAgMC0yLjMuMUw1LjIgOC4yIDMgNi40YTEuNyAxLjcgMCAwIDAtMi4zLjFMLjQgN2ExLjcgMS43IDAgMCAwIC4xIDIuM2wzLjggMy41YTEuNyAxLjcgMCAwIDAgMi40LS4xTDE1IDMuMWExLjcgMS43IDAgMCAwLS4yLTIuM3oiIGRhdGEtbmFtZT0iUGZhZCA0Ii8+PC9zdmc+);
}
input[type="radio"] {
display: none;
position: absolute;
width: 100%;
appearance: none;
}
input[type="radio"]:checked+label {
background: grey;
animation-name: blink;
animation-duration: 1s;
border-color: $accentcolor;
}
input[type="radio"]:checked+label:after {
background: $accentcolor;
}
input[type="radio"]:checked+label:before {
width: 20px;
height: 20px;
}
<form>
<div class="radiobtn">
<input type="radio" id="huey" name="drone" value="huey" checked />
<label for="huey">Learn More</label>
</div>
<div class="radiobtn">
<input type="radio" id="dewey" name="drone" value="dewey" />
<label for="dewey">Learn More</label>
</div>
</form>
Below code, the transition is 3 milliseconds and the issue is not really noticeable.
body {
background-color: Royalblue; /*#f0f0f0;*/
}
label {
font-family: 'Montserrat', sans-serif;
font-size: 14px;
z-index: -1;
border: 0;
color: white;
position: relative;
}
.head {
color: white;
margin-left: 44%;
font-family: monospace;
font-size: 25px;
}
.username {
height:40px;
}
.password {
height:40px;
margin-top: 30%;
}
form {
position: relative; /* Parent relative */
margin-top: 10%;
margin-left: 41%;
/*border: 1.5px solid black;*/
width: 15%;
}
input {
position: absolute;
font-family: 'Montserrat', sans-serif;
font-size: 15px;
background: transparent;
border: 0; /* BORDER yes/no */
border-bottom: 2px solid black;
background: transparent;
outline: 0;
height: 25px;
width: 180px;
z-index: 1;
margin-top: 5px;
}
label::after {
content: '';
position: absolute;
top: 1px;
left: 0;
width: 180px;
height: 23px;
border-radius: 2px;
transition: transform .3s;
}
label::after{
z-index: -1;
background: beige; /*#a86bf;*/
transform: scale3d(1, 0, 1);
transform-origin: 0% 0%;
}
input:focus {
border-radius: 2px;
}
input:focus + label::after,
input:valid + label::after {
transform: scale3d(1, -1.3, 1);
transition-timing-function: linear;
top: -3px;
}
span {
position: relative;
margin-top: -30px;
display: block;
padding: .6em ;
padding-left: 0px;
transition: top .5s ease, font-size .5s ease;
top: 0;
}
input:focus + label > span,
input:valid + label > span {
top: -23px;
font-size: 11px;
padding-bottom: 15px;
}
/*input:focus {
outline: 1;
}*/
/*.content {
margin-top: 10%;
margin-left: 41%;
}*/
/* font-family: monospace;*/
/*transform: translate3d(0, -80%, 0); */
/* transition-timing-function: linear;*/
<p class="head">Sign In</p>
<form>
<div class="content">
<div class="username">
<input type="text" name="name" class="user-input" id="user" required /> <!--input field-->
<label class="user-label" for="user"><span>Username</span></label>
</div>
<!-- <div class="password">
<input type="text" name="name" class="pass-input" id="pass" required />
<label class="pass-label" for="pass"><span>Password</span></label>
</div> -->
</div>
</form>
But changing transform time from label:after { transition: transform .3s;} to 2 seconds then it scales like this.
Why is there some exceeding scale at the bottom when unfocusing? I tried to tweak the top property in input:focus + label::after & label::afterin attempt of fixing it but nothing's working.
Try this and let me know if this solves your issue
body {
background-color: Royalblue; /*#f0f0f0;*/
}
label {
font-family: 'Montserrat', sans-serif;
font-size: 14px;
z-index: -1;
border: 0;
color: white;
position: relative;
}
.head {
color: white;
margin-left: 44%;
font-family: monospace;
font-size: 25px;
}
.username {
height:40px;
}
.password {
height:40px;
margin-top: 30%;
}
form {
position: relative; /* Parent relative */
margin-top: 10%;
margin-left: 41%;
/*border: 1.5px solid black;*/
width: 15%;
}
input {
position: absolute;
font-family: 'Montserrat', sans-serif;
font-size: 15px;
background: transparent;
border: 0; /* BORDER yes/no */
border-bottom: 2px solid black;
background: transparent;
outline: 0;
height: 25px;
width: 180px;
z-index: 1;
margin-top: 5px;
}
label::after {
content: '';
position: absolute;
top: 1px;
left: 0;
width: 180px;
height: 23px;
border-radius: 2px;
transition: all 2s linear;
}
label::after {
z-index: -1;
background: beige; /*#a86bf;*/
transform: scale3d(1, 0, 1);
transform-origin: 0% 0%;
}
input:focus {
border-radius: 2px;
}
input:focus + label::after,
input:valid + label::after {
transform: scale3d(1, -1.3, 1);
transition-timing-function: linear;
top: -3px;
}
span {
position: relative;
margin-top: -30px;
display: block;
padding: .6em ;
padding-left: 0px;
transition: all 2s linear;
font-size: 12px;
top: 0;
}
input:focus + label > span,
input:valid + label > span {
top: -23px;
font-size: 11px;
padding-bottom: 15px;
}
/*input:focus {
outline: 1;
}*/
/*.content {
margin-top: 10%;
margin-left: 41%;
}*/
/* font-family: monospace;*/
/*transform: translate3d(0, -80%, 0); */
/* transition-timing-function: linear;*/
<p class="head">Sign In</p>
<form>
<div class="content">
<div class="username">
<input type="text" name="name" class="user-input" id="user" required /> <!--input field-->
<label class="user-label" for="user"><span>Username</span></label>
</div>
<!-- <div class="password">
<input type="text" name="name" class="pass-input" id="pass" required />
<label class="pass-label" for="pass"><span>Password</span></label>
</div> -->
</div>
</form>
Here is what I changed :
input {
height: 40px;
margin-top: 0;
box-sizing: border-box;
}
Example
body {
background-color: Royalblue;
/*#f0f0f0;*/
}
label {
font-family: 'Montserrat', sans-serif;
font-size: 14px;
z-index: -1;
border: 0;
color: white;
position: relative;
}
.head {
color: white;
margin-left: 44%;
font-family: monospace;
font-size: 25px;
}
.username {
height: 40px;
}
.password {
height: 40px;
margin-top: 30%;
}
form {
position: relative;
margin-top: 10%;
margin-left: 41%;
width: 15%;
}
input {
position: absolute;
font-family: 'Montserrat', sans-serif;
font-size: 15px;
background: transparent;
border: 0;
/* BORDER yes/no */
border-bottom: 2px solid black;
background: transparent;
outline: 0;
height: 25px;
width: 180px;
z-index: 1;
//margin-top: 5px;
/* new */
height: 40px;
margin-top: 0;
box-sizing: border-box;
}
label::after {
content: '';
position: absolute;
top: 1px;
left: 0;
width: 180px;
height: 23px;
border-radius: 2px;
transition: transform .3s;
}
label::after {
z-index: -1;
background: beige;
/*#a86bf;*/
transform: scale3d(1, 0, 1);
transform-origin: 0% 0%;
}
input:focus {
border-radius: 2px;
}
input:focus+label::after,
input:valid+label::after {
transform: scale3d(1, -1.3, 1);
transition-timing-function: linear;
top: -3px;
}
span {
position: relative;
margin-top: -30px;
display: block;
padding: .6em;
padding-left: 0px;
transition: top .5s ease, font-size .5s ease;
top: 0;
}
input:focus+label>span,
input:valid+label>span {
top: -23px;
font-size: 11px;
padding-bottom: 15px;
}
<p class="head">Sign In</p>
<form>
<div class="content">
<div class="username">
<input type="text" name="name" class="user-input" id="user" required />
<!--input field-->
<label class="user-label" for="user"><span>Username</span></label>
</div>
<!-- <div class="password">
<input type="text" name="name" class="pass-input" id="pass" required />
<label class="pass-label" for="pass"><span>Password</span></label>
</div> -->
</div>
</form>
I am trying to make a switch button by using HTML elements. But I can't do animation for this. Here I have tried to use transition CSS3 property. The transition will work for some elements only and its doesn't work as expected for switch-group:before element. Is there any way to improve more animation to get an attractive one?. I have tried many links but this doesn't help with my code. I have attached code below,
input[type=checkbox] {
display: none;
}
.switch-wrapper {
position: relative;
width: 70px;
border: 1px solid;
cursor: pointer;
height: 22px;
overflow: hidden;
}
.switch-on,
.switch-off {
display: block;
width: 50%;
float: left;
height: 100%;
transition: 0.5s;
}
.switch-on {
background-color: red;
margin-left: -100%;
text-indent: -18px;
line-height: 21px;
text-align: center;
}
.switch-off {
text-indent: 18px;
line-height: 21px;
text-align: center;
background-color: aliceblue;
}
.switch-group:before {
display: block;
content: "";
position: absolute;
height: 18px;
width: 18px;
margin: 2px;
background-color: #1b191e;
top: 0px;
}
.switch-group {
display: block;
position: relative;
height: 23px;
width: 200%;
user-select: none;
}
input[type=checkbox]:checked+.switch-group .switch-on {
margin-left: 0;
}
input[type=checkbox]:checked+.switch-group:before {
right: 50%;
}
<div class="switch-wrapper">
<input type="checkbox" id="checked" />
<label class="switch-group" for="checked">
<span class="switch-on">ON</span>
<span class="switch-off">OFF</span>
</label>
</div>
The transition will not work in your :before pseudo element because you are changing the :before right value from auto(by default) to 0...transition does not works on auto values...
So try to use left:0 at start and then change it value on click(when input checked) using calc() and also use transtion:0.5s in :before to make it animate
input[type=checkbox] {
display: none;
}
.switch-wrapper {
position: relative;
width: 70px;
border: 1px solid;
cursor: pointer;
height: 22px;
overflow: hidden;
}
.switch-on,
.switch-off {
display: block;
width: 50%;
float: left;
height: 100%;
transition: 0.5s;
}
.switch-on {
background-color: red;
margin-left: -50%;
text-indent: -18px;
line-height: 21px;
text-align: center;
}
.switch-off {
text-indent: 18px;
line-height: 21px;
text-align: center;
background-color: aliceblue;
}
.switch-group:before {
display: block;
content: "";
position: absolute;
height: 18px;
width: 18px;
margin: 2px;
background-color: #1b191e;
top: 0px;
left: 0;
transition: 0.5s;
}
.switch-group {
display: block;
position: relative;
height: 23px;
width: 200%;
user-select: none;
}
input[type=checkbox]:checked+.switch-group .switch-on {
margin-left: 0;
}
input[type=checkbox]:checked+.switch-group:before {
left: calc(50% - 22px);
}
<div class="switch-wrapper">
<input type="checkbox" id="checked" />
<label class="switch-group" for="checked">
<span class="switch-on">ON</span>
<span class="switch-off">OFF</span>
</label>
</div>
Or if you want your code to work in more intuitive way, try to use opacity instead of margin.
I have changed some of your css a little bit
input[type=checkbox] {
display: none;
}
.switch-wrapper {
position: relative;
width: 70px;
border: 1px solid;
cursor: pointer;
height: 22px;
overflow: hidden;
}
.switch-on,
.switch-off {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
transition: 0.5s;
padding: 2px 6px;
}
.switch-on {
background-color: red;
line-height: 21px;
opacity: 0;
}
.switch-off {
line-height: 21px;
background-color: aliceblue;
opacity: 1;
text-align: right;
}
.switch-group:before {
display: block;
content: "";
position: absolute;
height: 18px;
width: 18px;
margin: 2px;
background-color: #1b191e;
top: 0px;
left: 0;
transition: 0.5s;
z-index: 99;
}
.switch-group {
display: block;
position: relative;
height: 23px;
user-select: none;
}
input[type=checkbox]:checked+.switch-group .switch-on {
opacity: 1;
}
input[type=checkbox]:checked+.switch-group .switch-off {
opacity: 0;
}
input[type=checkbox]:checked+.switch-group:before {
left: calc(100% - 22px);
}
<div class="switch-wrapper">
<input type="checkbox" id="checked" />
<label class="switch-group" for="checked">
<span class="switch-on">ON</span>
<span class="switch-off">OFF</span>
</label>
</div>
Try this toggler
.onoffswitch {
position: relative; width: 48px;
-webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
}
.onoffswitch-checkbox {
display: none;
}
.onoffswitch-label {
display: block; overflow: hidden; cursor: pointer;
height: 24px; padding: 0; line-height: 24px;
border: 2px solid #F1F1F5; border-radius: 24px;
background-color: #F1F1F5;
transition: background-color 0.3s ease-in;
}
.onoffswitch-label:before {
content: "";
display: block; width: 24px; margin: 0px;
background: #FFFFFF;
position: absolute; top: 0; bottom: 0;
right: 22px;
border: 2px solid #F1F1F5; border-radius: 24px;
transition: all 0.3s ease-in 0s;
}
.onoffswitch-checkbox:checked + .onoffswitch-label {
background-color: #2DC76D;
}
.onoffswitch-checkbox:checked + .onoffswitch-label, .onoffswitch-checkbox:checked + .onoffswitch-label:before {
border-color: #2DC76D;
}
.onoffswitch-checkbox:checked + .onoffswitch-label:before {
right: 0px;
}
.onoffswitch-checkbox:checked + .onoffswitch-label span.switch-off {
opacity: 0;
}
.onoffswitch-checkbox:checked + .onoffswitch-label span.switch-on{
opacity: 1;
}
span.switch-off {
opacity: 1;
float: right;
font-size: 12px;
position: absolute;
right: 0;
top: 50%;
transform: translateY(-50%);
}
span.switch-on {
position: absolute;
font-size: 12px;
top: 50%;
transform: translateY(-50%);
opacity: 0;
}
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch">
<label class="onoffswitch-label" for="myonoffswitch">
<span class="switch-on">ON</span>
<span class="switch-off">OFF</span></label>
</div>
You Can Try This.. Or visit This link For Details
.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: 10px;
background-color: #34A7C1; color: #FFFFFF;
}
.onoffswitch-inner:after {
content: "OFF";
padding-right: 10px;
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;
}
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked>
<label class="onoffswitch-label" for="myonoffswitch">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div>
I have made a custom on/off checkbox and I am having some trouble figuring out how to center it next to some text. Right now, I am attempting to use inline block to try this. Here is my code :
.for-you-switch .switch {
position: relative;
display: inline-block;
width: 65px;
height: 30px;
}
.for-you-switch input {
display: none;
}
.for-you-switch input:checked+.slider {
background-color: red;
}
.for-you-switch input:checked+.slider:after {
transform: translateX(35px);
}
.for-you-switch input:checked+.slider:before {
content: "on";
right: 36px;
color: #fff;
}
.for-you-switch input:focus+.slider {
box-shadow: 0 0 1px #2196F3;
}
.for-you-switch .slider {
position: absolute;
cursor: pointer;
border-radius: 34px;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
transition: .4s;
}
.for-you-switch .slider:after {
position: absolute;
content: "";
border-radius: 50%;
height: 26px;
width: 26px;
left: 2px;
bottom: 2px;
background-color: #fff;
transition: .2s;
}
.for-you-switch .slider:before {
position: absolute;
content: "off";
right: 12px;
top: 6px;
transition: .4s;
}
<div class="for-you-switch">
<span>Products for you.</span>
<label class="switch">
<input type="checkbox" />
<div class="slider round"></div>
</label>
</div>
Here is a fiddle with the code to play with - https://jsfiddle.net/qywkdv31/2/
As you can see the button is kind of floating at the top of the wrapping div, I would like to have it centered to the right of the text. Unsure how to accomplish this. Thanks!
You just need to add vertical-align: middle; to .for-you-switch .switch.
.for-you-switch .switch {
position: relative;
display: inline-block;
width: 65px;
height: 30px;
vertical-align: middle;
}
.for-you-switch input {
display: none;
}
.for-you-switch input:checked+.slider {
background-color: red;
}
.for-you-switch input:checked+.slider:after {
transform: translateX(35px);
}
.for-you-switch input:checked+.slider:before {
content: "on";
right: 36px;
color: #fff;
}
.for-you-switch input:focus+.slider {
box-shadow: 0 0 1px #2196F3;
}
.for-you-switch .slider {
position: absolute;
cursor: pointer;
border-radius: 34px;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
transition: .4s;
}
.for-you-switch .slider:after {
position: absolute;
content: "";
border-radius: 50%;
height: 26px;
width: 26px;
left: 2px;
bottom: 2px;
background-color: #fff;
transition: .2s;
}
.for-you-switch .slider:before {
position: absolute;
content: "off";
right: 12px;
top: 6px;
transition: .4s;
}
<div class="for-you-switch">
<span>Products for you.</span>
<label class="switch">
<input type="checkbox" />
<div class="slider round"></div>
</label>
</div>
Try,
.for-you-switch .switch{
display: inline-block;
vertical-align: middle;
}
.for-you-switch span{
display: inline-block;
vertical-align: middle;
}
Hope this help.
.switch {
position: relative;
display: inline-block;
vertical-align:middle;
width: 65px;
height: 30px; }