Creating a custom checkbox using CSS - html

I have the following code for creating custom checkboxes in CSS. Most of it is referenced from W3Schools and works pretty damn well.
.checkbox-container {
display: inline-block;
position: relative;
padding-left: 25px;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
line-height: 16px;
}
.checkbox-container input {
/* Hide the default */
position: absolute;
opacity: 0;
cursor: pointer;
height: 0;
width: 0;
}
.checkbox-container .checkmark {
position: absolute;
height: 16px;
width: 16px;
top: 0;
left: 0;
background-color: #ffffff;
border: 1px solid rgba(0, 0, 0, 0.2);
border-radius: 0.4rem;
}
.checkbox-container:hover input~.checkmark {
background-color: #ffffff;
border-color: rgba(0, 0, 0, 0.3);
}
.checkbox-container input:checked~.checkmark {
background-color: #1890ff;
border-color: #1890ff;
}
.checkbox-container .checkmark:after {
content: "";
position: absolute;
display: none;
}
.checkbox-container input:checked~.checkmark:after {
display: block;
}
.checkbox-container .checkmark:after {
left: 5px;
top: 1.5px;
width: 4px;
height: 8px;
border: solid #ffffff;
border-width: 0 2px 2px 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
}
<div>
<label class="checkbox-container">
Remember me
<input type="checkbox">
<span class="checkmark"></span>
</label>
</div>
Now the problem is, I want to change my markup. In fact I want to simplify it to something like this:
<div class="checkbox-container">
<input type="checkbox" name="remember-checkbox">
<label for="remember-checkbox">Remember me</label>
</div>
However, I can't seem to convert the CSS code to work with the new markup. I have tried converting the .checkmark to label:before, but that does not seem to work. Moreover, is it possible to show the check mark without having the <span> element? Thanks for any help.

You can update like below. Don't forget that for works with ID not name
.checkbox-container {
display: inline-block;
position: relative;
padding-left: 25px;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
line-height: 16px;
}
.checkbox-container input {
/* Hide the default */
position: absolute;
opacity: 0;
cursor: pointer;
height: 0;
width: 0;
}
.checkbox-container label {
cursor:pointer;
}
.checkbox-container label:before {
content:"";
position: absolute;
height: 16px;
width: 16px;
top: 0;
left: 0;
background-color: #ffffff;
border: 1px solid rgba(0, 0, 0, 0.2);
border-radius: 0.4rem;
}
.checkbox-container:hover input~label:before {
background-color: #ffffff;
border-color: rgba(0, 0, 0, 0.3);
}
.checkbox-container input:checked~label:before {
background-color: #1890ff;
border-color: #1890ff;
}
.checkbox-container label:after{
content: "";
position: absolute;
display: none;
left: 6px;
top: 1.5px;
width: 4px;
height: 8px;
border: solid #ffffff;
border-width: 0 2px 2px 0;
transform: rotate(45deg);
}
.checkbox-container input:checked~label:after {
display: block;
}
<div class="checkbox-container">
<input type="checkbox" id="remember-checkbox">
<label for="remember-checkbox">Remember me</label>
</div>

Don't need to define id in checkbox input. You can also achieve by css only like checkbox input{ width:100%; height:100%; z-index:2; left:0; top:0 ...}.
You can follow below snippet.
.checkbox-container {
display: inline-block;
position: relative;
padding-left: 25px;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
line-height: 16px;
/*border: 1px solid red;*/
}
.checkbox-container input {
/* Hide the default */
position: absolute;
opacity: 0;
cursor: pointer;
height: 100%;
width: 100%;
left: 0;
top: 0;
z-index: 2;
}
.checkbox-container .checkmark{
position: relative;
cursor: pointer;
}
.checkbox-container .checkmark:before {
content: '';
position: absolute;
height: 16px;
width: 16px;
top: 0;
left: -25px;
background-color: #ffffff;
border: 1px solid rgba(0, 0, 0, 0.2);
border-radius: 0.25rem;
}
.checkbox-container:hover input~.checkmark:before {
background-color: #ffffff;
border-color: rgba(0, 0, 0, 0.3);
}
.checkbox-container input:checked~.checkmark:before {
background-color: #1890ff;
border-color: #1890ff;
}
.checkbox-container .checkmark:after{
content: "";
position: absolute;
left: -18.9px;
top: 1.9px;
width: 4px;
height: 8px;
border: solid #ffffff;
border-width: 0 2px 2px 0;
-webkit-transform: rotate(90deg) scale(0);
-ms-transform: rotate(90deg) scale(0);
transform: rotate(90deg) scale(0);
transition: 350ms;
}
.checkbox-container input:checked~.checkmark:after {
opacity: 1;
-webkit-transform: rotate(45deg) scale(1);
-ms-transform: rotate(45deg) scale(1);
transform: rotate(45deg) scale(1);
}
<div class="checkbox-container">
<input type="checkbox" name="remember-checkbox">
<label class="checkmark">Remember me</label>
</div>

Related

How can I align a toggle button on the center of a div element?

I'm looking to place the toggle button aligned to the center of my element "Client_name" I created another div but I can't get the button to drop down and put it more to the right. Do you have any ideas?
The jsfiddle link is just below
https://jsfiddle.net/6cyuwxvf/
.switch {
position: relative;
display: inline-block;
width: 73px;
height: 32px;
}
.switch input {display:none;}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #CD3B1B;
-webkit-transition: .4s;
transition: .4s;
border-radius: 34px;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
border-radius: 50%;
}
input:checked + .slider {
background-color: #89C445;
}
input:focus + .slider {
box-shadow: 0 0 1px #89C445;
}
input:checked + .slider:before {
-webkit-transform: translateX(86px);
-ms-transform: translateX(36px);
transform: translateX(38px);
}
.button10 {
background-color: white;
color: black;
border: 2px solid;
width: 500px;
height: 50px;
}
.div-permission {
display: table-cell;
vertical-align: middle;
height: 45px;
width: 900px;
border: 1px solid red;
}
You can use display: flex; with align-items: center; for vertical alignment and just add some margin to the switch.
.switch {
position: relative;
display: inline-block;
width: 73px;
height: 32px;
margin-left: 0.25rem;
}
.switch input {
display: none;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #CD3B1B;
-webkit-transition: .4s;
transition: .4s;
border-radius: 34px;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
border-radius: 50%;
}
input:checked+.slider {
background-color: #89C445;
}
input:focus+.slider {
box-shadow: 0 0 1px #89C445;
}
input:checked+.slider:before {
-webkit-transform: translateX(86px);
-ms-transform: translateX(36px);
transform: translateX(38px);
}
.button10 {
background-color: white;
color: black;
border: 2px solid;
width: 500px;
height: 50px;
}
.div-permission {
display: flex;
align-items: center;
height: 45px;
width: 900px;
border: 1px solid red;
}
<div class="div-permission">
<input type="text" class="button10" placeholder="Client_name" />
<label class="switch">
<input type="checkbox" id="togBtn" onclick="myFunction()" />
<div class="slider round"></div>
</label>
<input type="submit" id="myDIV" style="display: none;" name="valider" onclick="style.display = 'none'" />
</div>
In order to change the position, I use the CSS transform function.
Change your .switch class to this:
.switch {
position: relative;
display: inline-block;
width: 73px;
height: 32px;
top:50%;
left:10%;
transform: translate(-50%,-50%);
}
That should center it for you! All I added was the top, left, and transform functions in the class.

Transform: Translate Imprecise In Firefox?

Codepen
I just created a custom selection pop-up. Now inspecting it in other browsers, I can't avoid, but notice the little offset of the white "dot", inside the radio button, Firefox renders. Chrome and Edge will display it just fine, but Firefow won't. And rather out of curiosity I'd like to know, why's that? And how to avoid?
The "dot":
&::before {
content: '';
position: absolute;
top: 50%;
left: 50%;
width: 50%;
height: 50%;
border-radius: 50%;
background-color: var(--light);
transform: translate(-50%, -50%);
}
Entire radio button:
input[type=radio] {
position: relative;
height: 24px;
width: 24px;
margin: 0;
border-radius: 50%;
border: solid 1px var(--light-contrast);
background-color: var(--light);
appearance: none;
cursor: pointer;
&:checked {
border-color: var(--accent);
background-color: var(--accent);
}
&:focus {
outline: 0;
}
&::before {
content: '';
position: absolute;
top: 50%;
left: 50%;
width: 50%;
height: 50%;
border-radius: 50%;
background-color: var(--light);
transform: translate(-50%, -50%);
}
}
Whole snippet:
:root {
--dark: #212121;
--dark-contrast: #424242;
--light: #fafafa;
--light-contrast: #cfd8dc;
--accent: #2196f3;
}
*,
::after,
::before {
box-sizing: border-box;
}
html {
font-family: 'Roboto', sans-serif;
font-size: 1rem;
line-height: 1.5;
}
body {
display: grid;
place-items: center;
height: 100vh;
margin: 0;
background-color: var(--dark);
}
ul, li {
margin: 0;
padding: 0;
list-style: none;
}
.select {
display: flex;
align-items: center;
justify-content: space-between;
width: 100%;
max-width: 300px;
background-color: var(--dark-contrast);
color: #fff;
box-shadow: 0 2px 4px -1px rgba(0, 0, 0, 0.25);
cursor: pointer;
user-select: none;
padding: 1.25rem 1.5rem;
border-radius: 4px;
}
.select:focus {
outline: 0;
}
.select:focus .list {
opacity: 1;
transform: translate(-50%, -50%) scale(1);
pointer-events: all;
}
.select:hover {
background-color: #484848;
}
.select::after {
display: inline-block;
content: '';
border-left: .3755rem solid transparent;
border-right: .3755rem solid transparent;
border-top: .375rem solid #fff;
}
.select .list {
position: fixed;
top: 50%;
left: 50%;
width: clamp(300px, 75vw, 320px);
border-radius: 4px;
background-color: var(--light);
color: #000;
transform: translate(-50%, -50%) scale(0);
transition: all 400ms ease;
opacity: 0;
pointer-events: none;
overflow: hidden;
}
.select .list label {
display: flex;
align-items: center;
padding: 1.25rem 1.5rem;
cursor: pointer;
}
.select .list label:hover {
background-color: #ededed;
}
.select .list label:active {
background-color: #e1e1e1;
transition: 200ms ease;
}
.select .list label input[type=radio] {
margin-right: 1rem;
}
input[type=radio] {
position: relative;
height: 24px;
width: 24px;
margin: 0;
border-radius: 50%;
border: solid 1px var(--light-contrast);
background-color: var(--light);
appearance: none;
cursor: pointer;
}
input[type=radio]:checked {
border-color: var(--accent);
background-color: var(--accent);
}
input[type=radio]:focus {
outline: 0;
}
input[type=radio]::before {
content: '';
position: absolute;
top: 50%;
left: 50%;
width: 50%;
height: 50%;
border-radius: 50%;
background-color: var(--light);
transform: translate(-50%, -50%);
}
<div class="select" tabindex="1">
Select
<div class="list">
<label for="select-radio1">
<input type="radio" id="select-radio1" name="select-radio">
Chrome
</label>
<label for="select-radio2">
<input type="radio" id="select-radio2" name="select-radio">
Safari
</label>
<label for="select-radio3">
<input type="radio" id="select-radio3" name="select-radio">
Firefox
</label>
</div>
</div>
Subpixel rendering is handled differently across browsers.
As this SO-Post states:
[...] the problem arises from a different approach to subpixel calculus between browsers.
Also: there is no cross-browser solution, but only workarounds.

Custom checkbox : add cross icon on uncheck using css?

I've been searching a while here and there and can't seem to find a way to stylize the uncheck state of a checkbox. I can change the background to red, but I need to add a cross as well, is it possible in css?
Here is my code and thanks for any help
/* The customcheck */
.customcheck {
display: block;
position: relative;
padding-left: 35px;
margin-bottom: 12px;
cursor: pointer;
font-size: 22px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
right: 10px;
}
/* Hide the browser's default checkbox */
.customcheck input {
position: absolute;
opacity: 0;
cursor: pointer;
}
/* Create a custom checkbox */
.checkmark {
position: absolute;
top: 0;
left: 0;
height: 25px;
width: 25px;
background-color: red;
border-radius: 5px;
}
/* On mouse-over, add a grey background color */
.customcheck:hover input~.checkmark {
background-color: #ccc;
}
/* When the checkbox is checked, add a blue background */
.customcheck input:checked~.checkmark {
background-color: #02cf32;
border-radius: 5px;
}
/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
content: "";
position: absolute;
display: none;
}
/* Show the checkmark when checked */
.customcheck input:checked~.checkmark:after {
display: block;
}
/* Style the checkmark/indicator */
.customcheck .checkmark:after {
left: 9px;
top: 5px;
width: 5px;
height: 10px;
border: solid white;
border-width: 0 3px 3px 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
}
input[type=checkbox] {
position: relative;
}
<label class="customcheck">
<input type="checkbox" checked="checked">
<span class="checkmark"></span>
</label>
I have used :pseudo element to add a cross icon
.customcheck {
position: relative;
}
.customcheck input {
display: none;
}
.customcheck input~.checkmark {
background: #ee0b0b;
width: 25px;
display: inline-block;
position: relative;
height: 25px;
border-radius: 2px;
vertical-align:middle;
margin-right:10px;
}
.customcheck input~.checkmark:after,
.customcheck input~.checkmark:before {
content: '';
position: absolute;
width: 2px;
height: 16px;
background: #fff;
left: 12px;
top: 4px;
}
.customcheck input~.checkmark:after {
transform: rotate(-45deg);
z-index: 1;
}
.customcheck input~.checkmark:before {
transform: rotate(45deg);
z-index: 1;
}
.customcheck input:checked~.checkmark {
background: #3d8a00;
width: 25px;
display: inline-block;
position: relative;
height: 25px;
border-radius: 2px;
}
.customcheck input:checked~.checkmark:after {
display: none;
}
.customcheck input:checked~.checkmark:before {
background: none;
border: 2px solid #fff;
width: 6px;
top: 2px;
left: 9px;
border-top: 0;
border-left: 0;
height: 13px;
top: 2px;
}
<label class="customcheck">
<input type="checkbox" checked="checked">
<span class="checkmark"></span> Label Text
</label>
jep. Here is some quick code. You can always make it more beatiful. But this is the concept
<input class="customcheckInput" type="checkbox" checked="checked">
<label class="customcheck"></label>
.customcheck {
position: relative;
padding-left: 30px;
}
.customcheck:before {
content: "";
display: block;
position: relative;
border: 1px solid #000;
height: 20p;
width: 20px;
}
.customcheck:after {
content: "";
display: none;
position: absolute;
background: red;
height: 10p;
width: 10px;
left: 0;
top: 0;
}
.customcheckInput + .customcheck:after {
display: blocK;
}

Style Checkbox after Label with CSS

I have a Label and an Checkbox Input after it JS Fiddle Example:
<label for="accept">Accept</label>
<input id="accept" type="checkbox">
I need to style the checkbox so I tried the following:
<label for="accept">Accept</label>
<input id="accept" type="checkbox">
In this case the checkbox stops working and I am able to style it.
Then I swapped the position of Label and Checkbox Input:
<input id="accept" type="checkbox">
<label for="accept">Accept</label>
In this case it is working, styled, but the input and label appear in same line.
I would like to style the checkbox input and have the label and checkbox in different lines.
The CSS is the following:
input[type="checkbox"] {
opacity: 0;
}
label {
position: relative;
display: block;
padding-left: 22px;
}
label::before, label::after {
position: absolute;
content: "";
display: block;
}
label::before{
height: 16px;
width: 16px;
border: 1px solid;
left: 0px;
top: 3px;
}
label::after {
height: 5px;
width: 9px;
border-left: 2px solid;
border-bottom: 2px solid;
transform: rotate(-45deg);
left: 4px;
top: 7px;
}
input[type="checkbox"] + label::after {
content: none;
}
input[type="checkbox"]:checked + label::after {
content: "";
}
input[type="checkbox"]:focus + label::before {
outline: rgb(59, 153, 252) auto 5px;
}
Note: On my examples I also wasn't able to vertically align the checkbox with the label.
form input[type="checkbox"] {
opacity: 0;
}
form .checkicon {
position: relative;
display: block;
height: 16px;
width: 16px;
border: 1px solid;
margin-bottom:5px;
}
form .checkicon::after {
position: absolute;
content: "";
display: block;
}
form .checkicon::after {
height: 5px;
width: 9px;
border-left: 2px solid;
border-bottom: 2px solid;
transform: rotate(-45deg);
left: 3px;
top:3px
}
form input[type="checkbox"] + .checkicon::after {
content: none;
}
form input[type="checkbox"]:checked + .checkicon::after {
content: "";
}
form input[type="checkbox"]:focus + .checkicon::before {
outline: rgb(59, 153, 252) auto 5px;
}
div label {
display: block;
}
h4 { margin: 12px 0; }
<form>
<input id="accept1" type="checkbox">
<label class="checkicon" for="accept1"></label>
<label>Accept</label>
</form>
this is it ! hope this help!
JSFiddle Link: https://jsfiddle.net/dh72qb5f/12/
HTML
<div>
<label for="accept1">
<input id="accept1" type="checkbox">
<span class="checkbox-custom"></span>
Accept
</label>
</div>
CSS:
input[type="checkbox"] {
position: absolute;
opacity: 0;
cursor: pointer;
}
div{
position: relative;
}
label {
margin-left: 25px;
}
.checkbox-custom {
position: absolute;
top: 0px;
left: 0px;
height: 12px;
width: 12px;
background-color: transparent;
border-radius: 5px;
border: 2px solid #ccc;
}
.checkbox-custom::after {
position: absolute;
content: "";
left: 12px;
top: 12px;
height: 0px;
width: 0px;
border-radius: 5px;
border: solid #ccc;
border-width: 0 3px 3px 0;
-webkit-transform: rotate(0deg) scale(0);
-ms-transform: rotate(0deg) scale(0);
transform: rotate(0deg) scale(0);
opacity: 1;
}
input:checked ~ .checkbox-custom {
background-color: #ccc;
border-radius: 5px;
transform: rotate(0deg) scale(1);
opacity: 1;
border: 2px solid #ccc;
}
input:checked ~ .checkbox-custom::after {
transform: rotate(45deg) scale(1);
opacity: 1;
left: 3px;
top: 0px;
width: 4px;
height: 8px;
border: solid #ffffff;
border-width: 0 2px 2px 0;
background-color: transparent;
border-radius: 0;
}

how to change the color of a checkmark when a link name is clicked

i have the following anchor tag thats embedded dynamically to a <ul> tag, when the user clicks on the word TEST i want the checkmark to change colour to blue,i tried the following but its not working,what am i doing wrong?
.checkmark{
display: inline-block;
width: 22px;
/* height: 22px; */
height: 17px;
-ms-transform: rotate(45deg);
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
margin-left: 50%;
margin-bottom: 1px;
}
.checkmark:before {
content: '';
position: absolute;
width: 3px;
height: 9px;
background-color: #ccc;
left: 11px;
top: 6px;
}
.checkmark {
cursor: pointer;
}
.checkmark:after {
content: '';
position: absolute;
width: 3px;
height: 3px;
background-color: #ccc;
left: 8px;
top: 12px;
}
input[type="checkbox"]:checked + .checkmark:before,
input[type="checkbox"]:checked + .checkmark:after {
background-color: blue;
}
<a class="internal" data-destination="local" dataid="66027" data-nodeid="undefined" ><span><input type="checkbox" style="display:none;" id="cb66027 "></span>TEST<label for="cb66027" class="checkmark"></label></a>
An <a> wrapped round an <input> will not toggle the checked state when clicked. You should convert the <a> to a <label>.
This means that your current .checkmark label will need to be a different element. I've used a span.
You should also move the <input> outside of it's parent <span> so that the <input> and .checkmark are siblings.
.checkmark {
display: inline-block;
width: 22px;
/* height: 22px; */
height: 17px;
-ms-transform: rotate(45deg);
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
margin-left: 50%;
margin-bottom: 1px;
}
.checkmark:before {
content: '';
position: absolute;
width: 3px;
height: 9px;
background-color: #ccc;
left: 11px;
top: 6px;
}
.checkmark {
cursor: pointer;
}
.checkmark:after {
content: '';
position: absolute;
width: 3px;
height: 3px;
background-color: #ccc;
left: 8px;
top: 12px;
}
input[type="checkbox"]:checked+.checkmark:before,
input[type="checkbox"]:checked+.checkmark:after {
background-color: blue;
}
<label class="internal" data-destination="local" dataid="66027" data-nodeid="undefined">
<input type="checkbox" style="display:none;" id="cb66027 ">
TEST
<span for="cb66027" class="checkmark"></span>
</label>
You are using a wrong + selector...
input+.checkmark means... input and .checkmark should be adjacent element...which is not in your case...
...so remove the span and put input and .checkmark in same level...
...also use opacity:0 instead of display:none to input[checkbox]...
I have changes some of your css
Stack Snippet
.checkmark {
display: inline-block;
width: 22px;
/* height: 22px; */
height: 17px;
-ms-transform: rotate(45deg);
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
margin-left: 50%;
margin-bottom: 1px;
}
.checkmark:before {
content: '';
position: absolute;
width: 3px;
height: 9px;
background-color: #ccc;
left: 11px;
top: 6px;
}
.checkmark {
cursor: pointer;
}
.checkmark:after {
content: '';
position: absolute;
width: 3px;
height: 3px;
background-color: #ccc;
left: 8px;
top: 12px;
}
input[type="checkbox"]:checked+.checkmark:before,
input[type="checkbox"]:checked+.checkmark:after {
background-color: blue;
}
input[type="checkbox"] {
position: absolute;
width: 100%;
left: 0;
z-index: 9;
height: 100%;
top: 0;
cursor: pointer;
opacity: 0;
margin: 0;
}
a.internal {
position: relative;
}
<a class="internal" data-destination="local" dataid="66027" data-nodeid="undefined" for="cb66027"><input type="checkbox" id="cb66027 ">TEST<label class="checkmark"></label></a>