Custom checkbox : add cross icon on uncheck using css? - html

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;
}

Related

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.

Creating a custom checkbox using CSS

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>

how to responsive and fix a checkbox in css

i want fix and match blue color checkbox in table
like this image
This image is for when the browser is in full screen
http://s8.picofile.com/file/8368937534/%D8%B3%D8%B3.jpg
But for me when I shrink the browser it looks like the photo below
http://s9.picofile.com/file/8368937576/%D8%B4%D8%B4.jpg
.container {
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;
}
/* Hide the browser's default checkbox */
.container input {
position: absolute;
opacity: 0;
cursor: pointer;
height: 0;
width: 0;
}
/* Create a custom checkbox */
.checkmark {
position: absolute;
top: 0;
left: 0;
height: 50px;
width: 107px;
background-color: #eee;
}
/* On mouse-over, add a grey background color */
.container:hover input~.checkmark {
background-color: #ccc;
}
/* When the checkbox is checked, add a blue background */
.container input:checked~.checkmark {
background-color: #2196f3;
}
/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
content: "";
position: absolute;
display: none;
}
/* Show the checkmark when checked */
.container input:checked~.checkmark:after {
display: block;
}
/* Style the checkmark/indicator */
.container .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);
}
<th scope="row">7:00</th>
<td>
<label class="container" style="direction:rtl">
<input type="checkbox"checked="checked"/>
<span class="checkmark"></span>
</label>
</td>
thanks.
checkmark {width: 100%;}
and
.container .checkmark:after {
left: 9px;
top: 5px;
width: 5px;
height: 10px;
border: solid #2196f3;
border-width: 0 3px 3px 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
}

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>

CSS3 border-radius on toggle input not working in IE11

For some reason my toggle button styles are not working in IE11. The input button should be a green circle but it's showing as a square box with a quite background and black border in IE11:
Here is how it is intended to look:
My code:
.toggle-field {
position: relative;
padding-top: 0.35rem;
}
.toggle-field .Toggle {
padding-left: 75px;
}
.toggle-field .toggle-element {
position: absolute;
top: 0;
left: 0;
width: 68px;
height: 34px;
border-radius: 18px;
border: 2px solid #ccc;
}
.toggle-field input {
appearance: none;
display: block;
position: absolute;
left: 4px;
top: 4px;
width: 26px;
height: 26px;
background: #ccc;
border-radius: 15px;
transition: left 117ms ease;
border: none;
}
.toggle-field input:focus + .toggle-element {
border-color: #aaa;
}
.toggle-field b {
position: absolute;
font-weight: normal;
line-height: 34px;
top: 1px;
left: 40px;
font-size: 0.65rem;
}
.toggle-field .Toggle.On input {
left: 38px;
background: #2ea664;
}
.toggle-field .Toggle.On b {
left: 12px;
}
<div class="field toggle-field null">
<label for="button1" class="Toggle On">
<input type="checkbox" id="button1" name="button1" value="on">
<span class="toggle-element"></span>
<b>YES</b> Question goes here
</label>
</div>
Most of what I have read says that I need to have a certain compatibility tag included, which I have:
<meta http-equiv="X-UA-Compatible" content="IE=edge">
Not sure what else to do. Has anyone else had this issue before?
I found a different way of doing this that works in IE11.
.toggle-field {
position: relative;
padding-top: 0.35rem;
}
.toggle-field .Toggle {
padding-left: 80px;
}
.toggle-field input {
appearance: none;
display: block;
position: absolute;
left: 6px;
top: 4px;
width: 26px;
height: 26px;
background: #ccc;
border-radius: 15px;
transition: left 117ms ease;
border: none;
}
.toggle-field .toggle-element {
background-color: #fff;
border: 2px solid #ccc;
bottom: 0;
cursor: pointer;
left: 0;
position: absolute;
right: 0;
top: 0;
transition: .4s;
width: 68px;
height: 32px;
}
.toggle-element:before {
background-color: #ccc;
bottom: 3px;
content: "";
height: 26px;
left: 4px;
position: absolute;
transition: .4s;
width: 26px;
}
input:checked + .toggle-element {
background-color: #fff;
}
input:checked + .toggle-element:before {
transform: translateX(34px);
background-color: #2ea664;
}
.toggle-element {
border-radius: 34px;
}
.toggle-element:before {
border-radius: 50%;
}
<div class="field toggle-field null">
<label for="checkbox" class="Toggle On">
<input type="checkbox" id="checkbox" />
<span class="toggle-element"></span>
</label>
</div>