Textbox text hides for floating label Textbox Style - html

I want to convert the label into floating style for textboxes in my form. I have performed the action on Hover and focus of the input box. The problem here is when the label floats and moves up the text is getting hidden.
Here is my HTML and CSS that i have tried so far.
.float-label {
position: absolute;
left: 0;
top: 0;
padding: 10px 6px;
color: #767676;
pointer-events: none;
transition: 0.2s ease all;
}
.form-control:focus ~ .float-label,
.form-control:not(:focus):valid ~ .float-label {
top: -18px;
margin-left:5px;
font-size: 12px;
opacity: 1;
z-index: 1;
background-color: white;
}
<div class="form-group border-lable-flt">
<input type="email" class="form-control" id="work_email" required>
<label class="float-label" for="workEmail">Work email</label>
</div>

You can use :placeholder-shown pseudo-class. If you don't want to make color: transparent the placeholder text, you can perfect-pixel match the styles for label and placeholder.
.form-group {
position: relative;
padding: 20px;
}
.float-label {
position: absolute;
left: 25px;
top: 21px;
color: #767676;
pointer-events: none;
transition: 0.2s ease all;
}
input::placeholder {
color: transparent;
}
.form-control:focus ~ .float-label,
.form-control:not(:focus):valid ~ .float-label,
input:not(:placeholder-shown) ~ .float-label {
top: 5px;
font-size: 12px;
opacity: 1;
z-index: 1;
background-color: transparent;
}
<div class="form-group border-lable-flt">
<input type="email" class="form-control" id="work_email" required placeholder="Work email">
<label class="float-label" for="workEmail">Work email</label>
</div>

Try to do the following changes :
.form-group{
margin-top:20px;
position:relative;
}
.float-label {
position: absolute;
left: 0;
top: 0;
padding:0;
color: #767676;
pointer-events: none;
transition: 0.2s ease all;
}

use this code:
Html:
<div class="form-group border-lable-flt textinput">
<input type="email" id="email" class="form-control" />
<label for="email">Email address</label>
</div>
CSS:
.textinput {
height: 3em;
margin: 1em 0;
position: relative;
width: 100%;
}
.textinput input,
.textinput label {
cursor: text;
font-size: 1.5em;
padding: 0.6em 1% 0.15em 1%;
position: absolute;
transition: all 0.15s ease;
width: 98%;
}
.textinput input {
border: solid black 1px;
border-radius: 0.15em;
}
.textinput label {
color: #BCB9B8;
padding: 0.5em 1%;
}
.textinput input.filled ~ label,
.textinput input:focus ~ label {
font-size: 0.6em;
font-weight: 600;
position: absolute;
}
.textinput input.filled ~ label {
color: #007932;
}
.textinput input[type="text"].filled,
.textinput input[type="email"].filled:valid {
background: #B8DB9B;
}
.textinput input[type="text"].filled ~label:after,
.textinput input[type="email"].filled:valid ~label:after {
color: #007932;
content: "\f058";
display: inline-block;
font: normal normal normal 14px/1 FontAwesome;
font-size: 3em;
text-rendering: auto;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
position: absolute;
top: 0.3em;
right: 0.3em;
transform: translate(0, 0);
}

Related

How can I add a floating label to a input?

I want that when a user comes and tries to type text in "input" the label goes up, and if they leave that area blank it comes back to the original position. If user typed something over there it will stay up there.
I tried this code with input box with required it is working fine, but when I try to do this with input and add some content in input the label coming back down it need to be stay up there.
div{
margin-top:40px;
}
.btn-add input[type="text"]{
width: 90%;
padding: 10px 20px 0 20px;
border: none;
border-bottom:1px solid #999;
font-size: 140%;
color: #000;
}
.btn-add input:focus ~ .floating-label{
top: -20px;
bottom: 10px;
left: 10px;
font-size: 20px;
opacity: 1;
color: rgb(100, 6, 6);
}
.floating-label {
position: absolute;
pointer-events: none;
left: 20px;
top:10px;
transition: 0.2s ease all;
color: #999999;
font-size: 120%;
}
.form-float{
position: relative;
}
<div class="form-float btn-add">
<input type="text" class="inputText" placeholder="" >
<label class="floating-label" >Button text</label>
</div>
I remove input:not(:focus):valid~.floating-label so it can work with not required field
Try this code since because of placeholder happening.
.form-group {
position: relative;
margin-bottom: 1.5rem;
}
input + .form-control-placeholder {
position: absolute;
transition: all 200ms;
top: -20px;
bottom: 10px;
font-size: 20px;
opacity: 1;
color: rgb(100, 6, 6);
}
input:placeholder-shown + .form-control-placeholder {
position: absolute;
pointer-events: none;
left: 20px;
top:10px;
transition: 0.2s ease all;
color: #999999;
font-size: 120%;
}
.form-control:focus + .form-control-placeholder {
position: absolute;
transition: all 200ms;
top: -20px;
bottom: 10px;
font-size: 20px;
opacity: 1;
color: rgb(100, 6, 6);
}
input[type="text"]{
width: 90%;
padding: 10px 20px 0 20px;
border: none;
border-bottom:1px solid #999;
font-size: 140%;
color: #000;
}
label{
position: absolute;
pointer-events: none;
left: 20px;
top:10px;
transition: 0.2s ease all;
color: #999999;
font-size: 120%;
}
<br>
<br>
<div class="form-group">
<input type="text" class="form-control" placeholder=" " >
<label class="form-control-placeholder">Button text</label>
</div>
Add Space to you placeholder by default and after that use input:not(:placeholder-shown) property to check if input is empty or not. Here working example of your code
div{
margin-top:40px;
}
.btn-add input[type="text"]{
width: 90%;
padding: 10px 20px 0 20px;
border: none;
border-bottom:1px solid #999;
font-size: 140%;
color: #000;
}
.btn-add input:focus ~ .floating-label{
top: -20px;
bottom: 10px;
left: 10px;
font-size: 20px;
opacity: 1;
color: rgb(100, 6, 6);
}
.btn-add input:not(:placeholder-shown) ~ .floating-label{
top: -20px;
bottom: 10px;
left: 10px;
font-size: 20px;
opacity: 1;
color: rgb(100, 6, 6);
}
.floating-label {
position: absolute;
pointer-events: none;
left: 20px;
top:10px;
transition: 0.2s ease all;
color: #999999;
font-size: 120%;
}
.form-float{
position: relative;
}
<div class="form-float btn-add">
<input type="text" class="inputText" placeholder=" " >
<label class="floating-label" >Button text</label>
</div>
Since :placeholder-shown only works with placeholder text shown (no empty string) you could use visibility.
div {
margin-top:40px;
}
.btn-add input[type="text"] {
width: 90%;
padding: 10px 20px 0 20px;
border: none;
border-bottom:1px solid #999;
font-size: 140%;
color: #000;
}
.floating-label {
position: absolute;
pointer-events: none;
left: 20px;
top: -20px;
transition: 0.2s ease all;
color: #999999;
font-size: 120%;
}
.form-float input:placeholder-shown ~ label {
top: 0;
visibility: hidden;
}
.form-float {
position: relative;
}
<div class="form-float btn-add">
<input type="text" placeholder="Button Label" >
<label class="floating-label">Button Label</label>
</div>
These are two solutions. I think they need some adjustment because probably they are not correct.
Solution #1: Floating label on focus
.custom-form-input {
display: block;
width: 100%;
height:32px;
padding: 1rem .55rem .065rem .55rem;
font-weight: 400;
border: 1px solid #cad3d6;
transition: border-color .15s ease-in-out, box-shadow .15s ease-in-out;
background-color: #fafafa;
font-size: 0.79em;
color:#000000;
border-radius: 2px;
font-family: Arial, Helvetica, sans-serif;
}
.custom-form-input:focus,
.custom-form-input:active {
color: #212529;
background-color: #fff;
border-color: #585d61;
outline: 0;
}
.custom-form-floating {
position:relative;
}
.custom-form-floating-label {
position:absolute;
color: #9aa0a9;
font-size: 0.85em;
height:100%;
width:100%;
top: 0px;
display:flex;
align-items: center;
padding: 0 .55rem 0 .55rem;
transition: 0.2s ease all;
-moz-transition: 0.2s ease all;
-webkit-transition: 0.2s ease all;
user-select: none;
pointer-events: none
}
.custom-form-input:focus ~ label,
.custom-form-input:valid ~ label {
font-size: 0.65rem;
height:55%;
}
.wrapper {
width:150px;
}
<div class="custom-form-floating">
<input id="test" class="custom-form-input" aria-label="usuario o correo electrónico" required=true aria-required="true"/>
<label for="test" class="custom-form-floating-label">Example 1</label>
</div>
<div class="wrapper">
<div class="custom-form-floating">
<input id="test" class="custom-form-input" aria-label="usuario o correo electrónico" required=true aria-required="true"/>
<label for="test" class="custom-form-floating-label">Example 2</label>
</div>
</div>
Solution #2: Floating label when type a letter
.custom-form-input {
display: block;
width: 100%;
height: 5px;
font-weight: 400;
border: 1px solid #cad3d6;
transition: border-color .15s ease-in-out;
background-color: #fafafa;
font-size: 1.2em;
color: #000000;
padding: 1rem .50rem 1rem .50rem;
border-radius: 2px;
font-family: Arial, Helvetica, sans-serif;
}
.custom-form-input:focus,
.custom-form-input:active {
color: #212529;
background-color: #fff;
border-color: #585d61;
outline: 0;
}
.custom-form-floating {
position: relative;
}
.custom-form-floating-label {
position: absolute;
color: #9aa0a9;
font-size: 0.85em;
height: 100%;
width: 100%;
top: 0px;
display: flex;
align-items: center;
padding: 0 .55rem 0 .55rem;
transition: 0.2s ease all;
-moz-transition: 0.2s ease all;
-webkit-transition: 0.2s ease all;
user-select: none;
pointer-events: none
}
.custom-form-input:valid {
padding: 1.50rem .50rem .50rem .50rem;
font-size: 0.79em;
}
.custom-form-input:valid~label {
font-size: 0.65rem;
height: 55%;
}
.wrapper {
width:250px;
}
<div class="wrapper">
<div class="custom-form-floating">
<input id="test" class="custom-form-input"
aria-label="usuario o correo electrónico" required=true aria-required="true"/>
<label for="test" class="custom-form-floating-label">Correo electrónico o
usuario</label>
</div>
</div>

How to style a radio button to look like a regular Learn More Button

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>

Why is there some exceeding scale at the bottom when unfocusing?

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>

How can I keep tab key behavior with this custom checkbox?

I created a custom checkbox on this site and I am trying to use it with tabindex, but I know that this CSS uses display: none which doesn't allow taborder, so this is my question: How can I use this custom checkbox in a form while keeping the input's properties?
You can attach the tabindexto the label that is standing in for the checkbox and then attach a keyboard event to the label to toggle the checkbox when a key is pressed. In this example I am toggling it when the user presses the space bar, like a real checkbox would do.
var checkbox = document.getElementById('myonoffswitch'),
label = document.querySelector('#myonoffswitch + label');
label.addEventListener('keypress', function (evt) {
if (evt.key === ' ') {
checkbox.checked = !checkbox.checked;
} else if (evt.key === 'Enter') {
// .form returns a reference to the parent form
checkbox.form.submit();
}
}, false);
.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: "";
padding-left: 10px;
background-color: #2E8DEF; color: #FFFFFF;
}
.onoffswitch-inner:after {
content: "";
padding-right: 10px;
background-color: #CCCCCC; color: #333333;
text-align: right;
}
.onoffswitch-switch {
display: block; width: 25px; margin: 2.5px;
background: #000000;
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;
}
<form id="theform">
<input><br>
<br>
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked>
<label class="onoffswitch-label" for="myonoffswitch" tabindex="0">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div><br>
<input><br>
<input type="checkbox" id="normal-checkbox"><label for="normal-checkbox">Normal Checkbox</label>
</form>

multiple switcher works incorrect CSS

I am trying to make an on/off switch, but it works incorrectly. I have a few rows with the switch. It's auto generated so that when I click every row except the first it switches 1 row.
Here is the html:
<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>
and css:
.onoffswitch {
position: relative;
width: 88px;
-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: 1px solid #D6D6D6;
border-radius: 5px;
}
.onoffswitch .onoffswitch-inner {
display: block;
width: 200%;
margin-left: -100%;
transition: margin 0.3s ease-in 0s;
}
.onoffswitch .onoffswitch-inner:before, .onoffswitch-inner:after {
display: block;
float: left;
width: 50%;
height: 33px;
padding: 0;
line-height: 33px;
color: white;
font-family: Trebuchet, Arial, sans-serif;
font-weight: bold;
box-sizing: border-box;
}
.onoffswitch-label .onoffswitch-inner:before {
content: "Yes";
padding-left: 10px;
background-color: blue; color: #FFFFFF;
}
.onoffswitch-label .onoffswitch-inner:after {
content: "No";
padding-right: 10px;
background-color: #ccc; color: #999999;
text-align: right;
}
.onoffswitch-label .onoffswitch-switch {
display: block;
width: 30px;
margin: 5px 0;
background: #FFFFFF;
position: absolute; top: 0; bottom: 0;
right: 53px;
border: 2px solid #D6D6D6; border-radius: 5px;
transition: all 0.3s ease-in 0s;
}
.onoffswitch > .onoffswitch-checkbox:checked + .onoffswitch-label > .onoffswitch-inner {
margin-left: 0;
}
.onoffswitch > .onoffswitch-checkbox:checked + .onoffswitch-label >.onoffswitch-switch {
right: 0px;
}
I try to do it with CSS only, without JS(JQuery, etc...)
Can you tell me what's wrong?
Your code is correct here's an example: https://jsfiddle.net/Lfv76omw/
You should set an unique id and name attribute for the elements
name="onoffswitch" id="myonoffswitch" and for="myonoffswitch" must be unique.
I simply added an incremental number like
name="onoffswitch1" id="myonoffswitch1" and for="myonoffswitch1"
name="onoffswitch2" id="myonoffswitch2" and for="myonoffswitch2"
name="onoffswitch3" id="myonoffswitch3" and for="myonoffswitch3"