CSS Pseudo elements positioning issue - html

I am facing an issue with positioning and alignment of pseudo elements. I have pasted the snippet below. Could you please tell me why the elements are overlapping and how to get them to appear next to each other without the overlap?
.switch {
display: inline-block;
margin-right: 15px;
}
.switch label {
position: relative;
cursor: pointer;
}
.switch input[type="checkbox"] + label::before {
position: absolute;
display: inline-block;
content: '';
height: 18px;
width: 33px;
border: 1px solid gray;
border-radius: 9px;
background-color: gray;
top: 2px;
right: -50px;
}
.switch input[type="checkbox"]:checked + label::before {
border: 1px solid red;
background-color: red;
right: -50px;
}
.switch input[type="checkbox"] + label::after {
position: absolute;
display: inline-block;
content: '';
height: 14px;
width: 14px;
border: 1px solid white;
border-radius: 50%;
background-color: white;
top: 4px;
right: -33px;
}
.switch input[type="checkbox"]:checked + label::after {
right: -48px;
}
.switch input[type="checkbox"] {
display: none;
}
<div class="switch">
<input type="checkbox" id="checkbox1">
<label for="checkbox1">Lorem ipsum Test</label>
</div>
<div class="switch">
<input type="checkbox" id="checkbox2">
<label for="checkbox2">Test1</label>
</div>
<div class="switch">
<input type="checkbox" id="checkbox3">
<label for="checkbox3">Lorem ipsum Test2</label>
</div>

They overlap because position: absolute takes elements out of the normal flow:
In the absolute positioning model, a box is explicitly offset with
respect to its containing block. It is removed from the normal flow
entirely (it has no impact on later siblings). [...] the contents of
an absolutely positioned element do not flow around any other boxes.
They may obscure the contents of another box (or be obscured
themselves)
You can add some margin to fix that:
.switch label {
margin-right: 50px;
}
.switch {
display: inline-block;
margin-right: 15px;
}
.switch label {
display: block;
margin-right: 50px;
position: relative;
cursor: pointer;
}
.switch input[type="checkbox"] + label::before {
position: absolute;
display: inline-block;
content: '';
height: 18px;
width: 33px;
border: 1px solid gray;
border-radius: 9px;
background-color: gray;
top: 2px;
right: -50px;
}
.switch input[type="checkbox"]:checked + label::before {
border: 1px solid red;
background-color: red;
right: -50px;
}
.switch input[type="checkbox"] + label::after {
position: absolute;
display: inline-block;
content: '';
height: 14px;
width: 14px;
border: 1px solid white;
border-radius: 50%;
background-color: white;
top: 4px;
right: -33px;
}
.switch input[type="checkbox"]:checked + label::after {
right: -48px;
}
.switch input[type="checkbox"] {
display: none;
}
<div class="switch">
<input type="checkbox" id="checkbox1">
<label for="checkbox1">Lorem ipsum Test</label>
</div>
<div class="switch">
<input type="checkbox" id="checkbox2">
<label for="checkbox2">Test1</label>
</div>
<div class="switch">
<input type="checkbox" id="checkbox3">
<label for="checkbox3">Lorem ipsum Test2</label>
</div>

When an element is absolutely positioned it gets out of the normal flow. This means that it does not affect other elements with its dimensions/positioning.
A solution is to take into consideration its dimensions/positioning in the container element (the label in this case)
So giving a padding-right:50px to the label and positioning the pseudo-elements accordingly will fix it.
Updated code
.switch {
display: inline-block;
margin-right: 15px;
}
.switch label {
position: relative;
cursor: pointer;
padding-right:50px;
}
.switch input[type="checkbox"] + label::before {
position: absolute;
display: inline-block;
content: '';
height: 18px;
width: 33px;
border: 1px solid gray;
border-radius: 9px;
background-color: gray;
top: 2px;
right: 0;
}
.switch input[type="checkbox"]:checked + label::before {
border: 1px solid red;
background-color: red;
right: 0;
}
.switch input[type="checkbox"] + label::after {
position: absolute;
display: inline-block;
content: '';
height: 14px;
width: 14px;
border: 1px solid white;
border-radius: 50%;
background-color: white;
top: 4px;
right: 17px;
}
.switch input[type="checkbox"]:checked + label::after {
right: 2px;
}
.switch input[type="checkbox"] {
display: none;
}
<div class="switch">
<input type="checkbox" id="checkbox1">
<label for="checkbox1">Lorem ipsum Test</label>
</div>
<div class="switch">
<input type="checkbox" id="checkbox2">
<label for="checkbox2">Test1</label>
</div>
<div class="switch">
<input type="checkbox" id="checkbox3">
<label for="checkbox3">Lorem ipsum Test2</label>
</div>

Related

Change radio button outer border color and thickness

I am trying to change 2 radio buttons border color and border thickness but for some reason it is not doing anything.
Here is my code
input[type='radio']:after {
width: 10px;
height: 10px;
border-radius: 15px;
top: -5px;
left: 3px;
position: relative;
background-color: transparent;
content: '';
display: inline-block;
visibility: visible;
border: none;
}
input[type='radio']:checked:after {
width: 10px;
height: 10px;
border-radius: 15px;
top: -5px;
left: 2.7px;
position: relative;
background-color: #f07f09;
content: "";
display: inline-block;
visibility: visible;
border: none;
}
input[type=radio]:checked {
border-color: #bbbbbb;
border: solid 3px;
}
input[type=radio]:checked:before {
border-color: #bbbbbb;
border: solid 3px;
}
<input type="radio" name="contact-option" id="1" class="otp-text-gap" [(ngModel)]="contact" value="cell" (change)="changeOTPChannel()"><span class="otp-channel-text">Cell number</span>
<input type="radio" name="contact-option" id="2" class="otp-text-gap" [(ngModel)]="contact" value="email" (change)="changeOTPChannel()"><span class="otp-channel-text">Email</span>
There are a few things which you should keep in mind, pseudo-classes are not supported by input types
you should not add an id as a number
In this below example I have used the label to trigger onchange for radio
input[type='radio']{
display:none;
}
input[type='radio'] + label{
display:inline-block;
position:relative;
padding-left: 25px;
margin-right:20px;
}
input[type='radio']:checked + label:after {
width: 10px;
height: 10px;
border-radius: 15px;
top: 4px;
left: 4px;
position: absolute;
background-color: #f07f09;
content: "";
}
input[type=radio] + label:before {
content: "";
border-color: #bbbbbb;
border: solid 3px;
width: 13px;
position: absolute;
left: 0;
height: 13px;
border-radius: 50%;
}
<input type="radio" name="contact-option" id="one" class="otp-text-gap" [(ngModel)]="contact" value="cell" (change)="changeOTPChannel()"><label for="one" class="otp-channel-text">Cell number</label>
<input type="radio" name="contact-option" id="two" class="otp-text-gap" [(ngModel)]="contact" value="email" (change)="changeOTPChannel()"><label for="two" class="otp-channel-text">Email</label>

How to style a radio button with border and color

What is the best way to give a different color of my radio button as well the border color?
I tried using this code but it does not seem to work perfectly.
li.payment_method_bacs input[type='radio']:checked:before {
background: black !important;
content: "";
display: block;
position: relative;
top: 19px;
left: 3px;
width: 6px;
height: 6px;
border-radius: 50%;
border: 3px solid black;
}
li.payment_method_bacs input[type='radio']:checked:before {
background: black !important;
content: "";
display: block;
position: relative;
top: 19px;
left: 3px;
width: 6px;
height: 6px;
border-radius: 50%;
border: 3px solid black;
}
<input id="payment_method_bacs" type="radio" class="input-radio" name="payment_method" value="bacs" data-order_button_text="" checked="checked">
You are applying :after and :before on the input element which is invalid with css. So you need to wrap other things like a div to achieve this. The div will use the checked value of radio and with :after and :before element will do the thing as needed. I am using 2 approaches.
Approach 1: using div as faux element:
li {
list-style: none; display:inline-block; margin-right:20px;
}
.radioBox {
position: relative;
background: transparent;
z-index: 1;
width: 13px;
height: 13px;
cursor: pointer;
}
.radioBox>div {
position: absolute;
z-index: 0;
top: 0;
left: 0;
width: 13px;
height: 13px;
display: inline-block;
border: 2px solid black;
border-radius: 12px;
}
.radioBox>input {
position: absolute;
z-index: 2;
width: 13px;
height: 13px;
opacity: 0;
}
.radioBox > input:checked + div:after {
position: absolute;
content: " ";
font-size: 0;
display: block;
left: 1px;
top: 1px;
width: 10px;
height: 10px;
background: black;
border-radius: 10px;
}
<ul>
<li class="payment_method_bacs">
<div class="radioBox "><input id="payment_method_bacs" type="radio" class="input-radio" name="payment_method" value="bacs" data-order_button_text="" >
<div></div>
</div>
</li>
<li>
<div class="radioBox ">
<input id="payment_method_bacs1" type="radio" class="input-radio" name="payment_method" value="bacs" data-order_button_text="" checked="checked" >
<div></div>
</div>
</li>
</ul>
Approach 2 : Using label as faux element.
ul,li{list-style:none; display:inline-block;}
label {
display: inline-block;
cursor: pointer;
position: relative;
padding-left: 25px;
margin-right: 15px;
font-size: 13px;
}
input[type=radio] {
display: none;
}
label:before {
content: "";
display: inline-block;
width: 16px;
height: 16px;
margin-right: 10px;
position: absolute;
left: 0;
bottom: 1px;
background-color: #fff;
border:1px solid #000;
border-radius:50%;
}
.radio label:before {
border-radius: 8px;
}
input[type=radio]:checked + label:before {
content: "\2022";
color: #000;
font-size: 30px;
text-align: center;
line-height: 18px;
}
<ul>
<li>
<div class="radio">
<input id="payment_method_bacs" type="radio" class="input-radio" name="payment_method" value="bacs" data-order_button_text="" checked="checked">
<label for="payment_method_bacs">Visa</label>
</div>
</li>
<li>
<div class="radio">
<input id="payment_method_bacs1" type="radio" class="input-radio" name="payment_method" value="bacs" data-order_button_text="" >
<label for="payment_method_bacs1">Paypal</label>
</div>
</li>
</ul>
add html in input and lable tag input will be hide if check box will check than you can add style anything in lable
input[type="radio"] {
display: none;
}
input[type="radio"]:checked + label::before {
background: red none repeat scroll 0 0;
border: 0 solid #333;
content: "";
font-size: 0;
height: 20px;
width: 20px;
}
<span class="input-lif in-lidt">
<input class="contact_radio" id="rbsrt1" name="contact" value="individual" checked="checked" type="radio">
<label for="rbsrt1"> Individual </label>
</span>
Changing the appearance of form inputs it's a little bit tricky, as each browser renders them in a different manner.
To ensure you get the same result for different browsers, you might consider using an image (inline, or as a background image for the element containing the input) or an element, as your input display.
<label id="myLabel" for="payment_method_bacs">
<input id="payment_method_bacs"
type="radio"
class="input-radio"
name="payment_method"
value="bacs"
data-order_button_text=""
checked="checked">
</label>
#myLabel{
width: 20px;
height: 20px;
background: white;
-moz-border-radius: 50px;
-webkit-border-radius: 50px;
border-radius: 50px;
border:2px solid #ccc;
display:inline-block;
}
#myLabel input{
display:none;
}
Fiddle provided here: https://jsfiddle.net/omfv8qhj/

How to create a tick mark in a custom checkbox?

I want to make a custom checkbox with CSS, but not sure how do it.
.custom-checkbox {
border: 3px solid #7e8a94;
float: right;
height: 20px;
width: 20px;
border-radius: 5px;
cursor: pointer;
display: inline-block;
}
.custom-checkbox.hover {
border: 3px solid #43D8B0;
}
.custom-checkbox.active {
border: 3px solid #43D8B0;
}
.custom-checkbox.active.checkmark {}
<input type='checkbox' class='checkbox'>
<div class="custom-checkbox">
<div class="checkmark"></div>
</div>
I added active class to custom-checkbox on checking, but not sure how to create a tick inside a box then?
You have to manually customize your input & add a label in a way using pseudo elements to get the desired effect in the checkbox you want here.
Also, you can use content: '✔'; in your css to provide a tick on click.
I built you a demo to refer to, check the following code:
[type="checkbox"]:not(:checked),
[type="checkbox"]:checked {
position: absolute;
left: -9999px;
}
[type="checkbox"]:not(:checked)+label,
[type="checkbox"]:checked+label {
position: relative;
padding-left: 25px;
cursor: pointer;
}
[type="checkbox"]:not(:checked)+label:before,
[type="checkbox"]:checked+label:before {
content: '';
position: absolute;
left: 0;
top: -5px;
width: 40px;
height: 40px;
background: #fff;
border-radius: 3px;
box-shadow: inset 0 1px 3px rgba(0, 0, 0, .1);
border-radius: 50%;
background-color: #5cf1b3;
outline: none;
}
[type="checkbox"]:not(:checked)+label:after,
[type="checkbox"]:checked+label:after {
content: '✔';
position: absolute;
top: 8px;
left: 10px;
font-size: 24px;
line-height: 0.8;
color: #fff;
transition: all .2s;
}
[type="checkbox"]:not(:checked)+label:after {
opacity: 0;
transform: scale(0);
}
[type="checkbox"]:checked+label:after {
opacity: 1;
transform: scale(1);
}
<p>
<input type="checkbox" id="test1" />
<label for="test1"></label>
</p>
You will need to use :checked pseudo class for it. In the following example it's using a span tag to create the custom checkbox style, and using a pseudo element :before with a special character ✔ inside for the tick.
.custom-checkbox input {
display: none;
}
.custom-checkbox span {
border: 3px solid #7e8a94;
float: right;
height: 20px;
width: 20px;
border-radius: 5px;
cursor: pointer;
display: flex;
justify-content: center;
align-items: center;
}
.custom-checkbox:hover span,
.custom-checkbox input:checked + span {
border: 3px solid #43D8B0;
}
.custom-checkbox input:checked + span:before {
content: "✔";
}
<label class="custom-checkbox">
<input type="checkbox">
<span></span>
</label>
You can check custom checkbox in different-different states below snippet
/* Base for label styling */
[type="checkbox"]:not(:checked),
[type="checkbox"]:checked {
position: absolute;
left: -9999px;
}
[type="checkbox"]:not(:checked) + label,
[type="checkbox"]:checked + label {
position: relative;
padding-left: 1.95em;
cursor: pointer;
}
/* checkbox aspect */
[type="checkbox"]:not(:checked) + label:before,
[type="checkbox"]:checked + label:before {
content: '';
position: absolute;
left: 0; top: 0;
width: 1.25em; height: 1.25em;
border: 2px solid #ccc;
background: #fff;
border-radius: 4px;
box-shadow: inset 0 1px 3px rgba(0,0,0,.1);
}
/* checked mark aspect */
[type="checkbox"]:not(:checked) + label:after,
[type="checkbox"]:checked + label:after {
content: '✔';
position: absolute;
top: .1em; left: .3em;
font-size: 1.3em;
line-height: 0.8;
color: #09ad7e;
transition: all .2s;
}
/* checked mark aspect changes */
[type="checkbox"]:not(:checked) + label:after {
opacity: 0;
transform: scale(0);
}
[type="checkbox"]:checked + label:after {
opacity: 1;
transform: scale(1);
}
/* disabled checkbox */
[type="checkbox"]:disabled:not(:checked) + label:before,
[type="checkbox"]:disabled:checked + label:before {
box-shadow: none;
border-color: #bbb;
background-color: #ddd;
}
[type="checkbox"]:disabled:checked + label:after {
color: #999;
}
[type="checkbox"]:disabled + label {
color: #aaa;
}
/* accessibility */
[type="checkbox"]:checked:focus + label:before,
[type="checkbox"]:not(:checked):focus + label:before {
border: 2px dotted blue;
}
/* hover style just for information */
label:hover:before {
border: 2px solid #4778d9!important;
}
<p>
<input type="checkbox" id="test1" />
<label for="test1">Red</label>
</p>
<p>
<input type="checkbox" id="test2" checked="checked" />
<label for="test2">Yellow</label>
</p>
<p>
<input type="checkbox" id="test3" checked="checked" disabled="disabled" />
<label for="test3">Green</label>
</p>
<p>
<input type="checkbox" id="test4" disabled="disabled" />
<label for="test4">Brown</label>
</p>

Radio button inside label width issue

I have custom radio buttons and I am try to display multiple radio buttons in one line. Currently, my code works fine but my issue is that I am adding fixed width to label which in my case is width:50px;.
If my text is longer... my text overlaps the next radio button. Is there a way I can avoid having fixed widths? Function like calc() does't seem to help in my situation.
I also, tried just using min-width instead of width.
* {
box-sizing: border-box;
}
input[type="radio"] {
display: none;
cursor: pointer;
}
label {
cursor: pointer;
display: inline-block;
width: 50px;
position: relative;
}
.radio span.custom > span {
margin-left: 22px;
}
.radio .custom {
background-color: #fff;
border: 1px solid #ccc;
border-radius: 3px;
display: inline-block;
height: 20px;
left: 0;
position: absolute;
top: 0;
width: 20px;
}
.radio input:checked + .custom:after {
background-color: #0574ac;
border-radius: 100%;
border: 3px solid #fff;
content: "";
display: block;
height: 12px;
position: absolute;
top: 0;
width: 12px;
}
.radio input + .custom {
border-radius: 100%;
}
.checkbox input:checked .custom {
background-color: blue;
border-color: blue;
}
<label class="radio">
<input type="radio">
<span class="custom"><span>One</span></span>
</label>
UPDATE:
I need the text to be inside the <span class="custom"> tag. I don't have to have the inner span tag there though
JSFiddle Demo
Try using :after pseudo element to construct the rounded radio button. And the change the .custom to display: inline so that it takes up the content width. Also add white-space: nowrap to the span in-order for it take the full width of text without breaking.
* {
box-sizing: border-box;
}
input[type="radio"] {
display: none;
cursor: pointer;
}
label {
cursor: pointer;
min-width: 50px;
display:inline-block;
position:relative;
}
.radio span.custom > span {
margin-left: 24px;
margin-right: 10px;
display: inline-block;
white-space: nowrap;
line-height: 22px;
}
.radio .custom {
display: inline;
}
.radio .custom:after{
content: '';
height: 20px;
left: 0;
top: 0;
width: 20px;
background-color: #fff;
border: 1px solid #ccc;
border-radius: 3px;
position: absolute;
}
.radio input:checked + .custom:before {
background-color: #0574ac;
border-radius: 100%;
border: 3px solid #fff;
content: "";
display: block;
height: 12px;
position: absolute;
top: 2px;
width: 12px;
z-index: 1;
left: 2px;
}
.radio input + .custom:after {
border-radius: 100%;
}
.checkbox input:checked .custom {
background-color: blue;
border-color: blue;
}
<label class="radio">
<input type="radio">
<span class="custom"><span>One</span></span>
</label>
<label class="radio">
<input type="radio">
<span class="custom"><span>Two (longer text)</span></span>
</label>
<label class="radio">
<input type="radio">
<span class="custom"><span>Three (another longer text)</span></span>
</label>
<label class="radio">
<input type="radio">
<span class="custom"><span>Four</span></span>
</label>
I think a lot of things went wrong here starting from the structure of the html. First, you've got two unnecessarily nested span elements where the outter span is constraining the width of the inner span.
Personally, I'd get rid of the inner span element to make your life easier with simpler css. Then, i'd get rid of all those absolute and relative positioning that aren't needed
See example below
* {
box-sizing: border-box;
}
input[type="radio"] {
display: none;
cursor: pointer;
}
label {
cursor: pointer;
display: inline-block;
}
.radio span.custom > span {
margin-left: 22px;
}
.radio .custom {
background-color: #fff;
border: 1px solid #ccc;
border-radius: 3px;
display: inline-block;
height: 18px;
left: 0;
top: 0;
width: 18px;
vertical-align: middle;
padding: 2px;
}
.radio input:checked + .custom:after {
background-color: #0574ac;
border-radius: 100%;
content: "";
display: block;
height: 12px;
width: 12px;
}
.radio input + .custom {
border-radius: 100%;
}
.checkbox input:checked .custom {
background-color: blue;
border-color: blue;
}
<label class="radio">
<input type="radio">
<span class="custom"></span>
A veeeeeeeeeeeeeeeeeeeeeeery loooooooooooooong text for this button
</label>
<label class="radio">
<input type="radio">
<span class="custom"></span>
Two
</label>
I did made a few adjustments
I rearranged your code. Is this how you want?
JSFiddle : https://jsfiddle.net/fz5vhwtx/5/
I remove all the position absolute because it's width is not counted in the label. It's floating and I changed it to flex layout. Try to see it.
HTML
<div class="radio-holder">
<label class="radio">
<input type="radio" name="radio">
<span class="custom"></span>
<p>One</p>
</label>
<label class="radio">
<input type="radio" name="radio">
<span class="custom"></span>
<p>Two</p>
</label>
<label class="radio">
<input type="radio" name="radio">
<span class="custom"></span>
<p>One Hundred</p>
</label>
</div>
CSS
* {
box-sizing: border-box;
}
input[type="radio"] {
display: none;
cursor: pointer;
}
.radio-holder {
display: flex;
}
label {
cursor: pointer;
display: flex;
align-items: center;
position:relative;
margin-right: 20px;
}
.radio .custom {
background-color: #fff;
border: 1px solid #ccc;
border-radius: 3px;
display: inline-block;
height: 20px;
width: 20px;
margin-right: 3px;
}
.radio input:checked + .custom:after {
background-color: #0574ac;
border-radius: 100%;
border: 3px solid #fff;
content: "";
display: block;
height: 12px;
top: 0;
width: 12px;
}
.radio input + .custom {
border-radius: 100%;
}
.checkbox input:checked .custom {
background-color: blue;
border-color: blue;
}
Hope it helps you. Selamat siang.

Custom checkbox using only CSS and HTML

I need to create a custom checkbox using only html and CSS. So far I have:
HTML/CSS:
.checkbox-custom {
opacity: 0;
position: absolute;
}
.checkbox-custom,
.checkbox-custom-label {
display: inline-block;
vertical-align: middle;
margin: 5px;
cursor: pointer;
}
.checkbox-custom + .checkbox-custom-label:before {
content: '';
background: #fff;
border-radius: 5px;
border: 2px solid #ddd;
display: inline-block;
vertical-align: middle;
width: 10px;
height: 10px;
padding: 2px;
margin-right: 10px;
text-align: center;
}
.checkbox-custom:checked + .checkbox-custom-label:before {
content: "";
display: inline-block;
width: 1px;
height: 5px;
border: solid blue;
border-width: 0 3px 3px 0;
transform: rotate(45deg);
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
border-radius: 0px;
margin: 0px 15px 5px 5px;
}
<div>
<input id="checkbox-1" class="checkbox-custom" name="checkbox-1" type="checkbox">
<label for="checkbox-1" class="checkbox-custom-label">First Choice</label>
</div>
<div>
<input id="checkbox-2" class="checkbox-custom" name="checkbox-2" type="checkbox">
<label for="checkbox-2" class="checkbox-custom-label">Second Choice</label>
</div>
The checked checkbox should be a checkmark with the square bordered around it instead of just a checkmark. Searching for answers, I have only found cases where the checkmark is displayed using a UTF-8 character or an image. I am not able to use either of these for what I am trying to accomplish. I am only able to use plain CSS and HTML. Any suggestions?
codepen here: http://codepen.io/alisontague/pen/EPXagW?editors=110
The problem is that you are using the same pseudo element for the square border and the checkmark. The simple solution would be to continue using the :before pseudo element for the border, and then use an :after pseudo element for the checkmark.
Updated Example
You would have to absolutely position the :after pseudo element relative to the parent .checkbox-custom label element.
Here is the updated code:
.checkbox-custom {
display: none;
}
.checkbox-custom-label {
display: inline-block;
position: relative;
vertical-align: middle;
margin: 5px;
cursor: pointer;
}
.checkbox-custom + .checkbox-custom-label:before {
content: '';
background: #fff;
border-radius: 5px;
border: 2px solid #ddd;
display: inline-block;
vertical-align: middle;
width: 10px; height: 10px;
padding: 2px; margin-right: 10px;
}
.checkbox-custom:checked + .checkbox-custom-label:after {
content: "";
padding: 2px;
position: absolute;
width: 1px;
height: 5px;
border: solid blue;
border-width: 0 3px 3px 0;
transform: rotate(45deg);
top: 2px; left: 5px;
}
<h3>Checkboxes</h3>
<div>
<input id="checkbox-1" class="checkbox-custom" name="checkbox-1" type="checkbox">
<label for="checkbox-1" class="checkbox-custom-label">First Choice</label>
</div>
<div>
<input id="checkbox-2" class="checkbox-custom" name="checkbox-2" type="checkbox">
<label for="checkbox-2" class="checkbox-custom-label">Second Choice</label>
</div>
My previous answer is wrong because it uses the ::before pseudo-element selector on an element that isn't a container. Thanks to #BoltClock for pointing out my error. So, I came up with another solution that uses the Checkbox Hack and the CSS3 sibling selector (~).
Demo at CodePen
input[type=checkbox] {
position: absolute;
top: -9999px;
left: -9999px;
}
input[type=checkbox] ~ label::before {
content: '\2713';
display: inline-block;
text-align: center;
color: white;
line-height: 1em;
width: 1em;
height: 1em;
border: 1px inset silver;
border-radius: 0.25em;
margin: 0.25em;
}
input[type=checkbox]:checked ~ label::before {
color: black;
}
<form name="checkbox-form" id="checkbox-form">
<div>
<input id="checkbox-1" class="checkbox-custom" name="checkbox-1" type="checkbox">
<label for="checkbox-1" class="checkbox-custom-label">First Choice</label>
</div>
<div>
<input id="checkbox-2" class="checkbox-custom" name="checkbox-2" type="checkbox">
<label for="checkbox-2" class="checkbox-custom-label">Second Choice</label>
</div>
</form>
I didn't use StackOverflow's "Run Code-Snippet" functionality because it does something weird when I run it. I think it's because of the checkbox hack.
Custom checkbox using only HTML and CSS along with three checkbox states (checked, unchecked and half checked or unchecked(if it's used in a group of checkboxes))
<label class="checkboxcontainer"> one
<input type="checkbox">
<span class="checkmark"></span>
</label>
.checkboxcontainer input {
display: none;
}
.checkboxcontainer {
display: inlin-block;
padding-left: 30px;
position: relative;
cursor: pointer;
user-select: none;
}
.checkboxcontainer .checkmark {
display: inlin-block;
width: 25px;
height: 25px;
background: #eee;
position: absolute;
left: 0;
top: 0;
}
.checkboxcontainer input:checked+.checkmark {
background-color: #2196fc;
}
.checkboxcontainer input:checked+.checkmark:after {
content: "";
position: absolute;
height: 4px;
width: 9px;
border-left: 3px solid white;
border-bottom: 3px solid white;
top: 45%;
left: 50%;
transform: translate(-50%, -50%) rotate(-45deg);
}
.checkboxcontainer input:indeterminate+.checkmark:after {
content: "";
position: absolute;
height: 0px;
width: 9px;
border-left: 3px solid white;
border-bottom: 3px solid white;
top: 45%;
left: 50%;
transform: translate(-50%, -50%) rotate(180deg);
}
Jsfiddle: Demo
.checkbox-custom {
position: relative;
}
.checkbox-custom input[type=checkbox] {
display: none;
}
.checkbox-custom input[type=checkbox]~b {
cursor: pointer;
outline: 0;
position: relative;
display: inline-block;
margin: 4px 1px 0;
background-color: #ffffff;
border: 2px solid #ad823a;
width: 24px;
height: 24px;
vertical-align: middle;
line-height: 1;
text-align: center;
font-size: 20px;
color: #ad823a;
}
.checkbox-custom input[type=checkbox]:checked~b:after {
content: '\2713';
}
<div class="checkbox-custom">
<label>
<input type="checkbox">
<b></b>
<span>app 1</span>
</label>
</div>
<div class="checkbox-custom">
<label>
<input type="checkbox">
<b></b>
<span>app 1</span>
</label>
</div>