Multiple css atribute selector with after or before pseudoclass - html

I'm trying creating custom check-box it two variants: one for multiple check-boxes and another style for single check-box (on/off). In CSS, when I'm trying to select only elements with proper class (for second case), nothing happens. For sure I do mistake in CSS statement below, but what kind of? Thanks!
input[type="checkbox"][class="toggle"]:not(:checked) + label:before,
input[type="checkbox"][class="toggle"]:checked + label:before {
background: red;
}
/*CHECKBOX STYLING*/
/*font styling*/
/* Base for label styling */
input[type="checkbox"]:not(:checked),
input[type="checkbox"]:checked {
position: absolute;
left: -9999px;
}
input[type="checkbox"]:not(:checked) + label,
input[type="checkbox"]:checked + label {
position: relative;
padding-left: 42px;
cursor: pointer;
font-size: 16pt;
}
/* checkbox aspect */
[type="checkbox"]:not(:checked) + label:before,
[type="checkbox"]:checked + label:before {
content: '';
position: absolute;
left:0; top: 0px;
width: 22px; height: 22px;
border: 2px solid #d7d6d5;
border-radius: 4px;
background: #f1f5f8;
}
[type="checkbox"]:checked + label:before{
border-color: #555555;
}
input[type="checkbox"][class="toggle"]:not(:checked) + label:before,
input[type="checkbox"][class="toggle"]:checked + label:before {
background: red;
}
/*checked mark aspect */
[type="checkbox"]:not(:checked) + label:after,
[type="checkbox"]:checked + label:after {
content: '\e909';
font-family: 'icomoon';
position: absolute;
top: 8px; left: 4px;
font-size: 10pt;
line-height: 0.8;
color: #555555;
}
/* 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);
}
<div class="checkbox">
<form action="#">
<p>
<input id="check1" type="checkbox" name="check" checked="checked" >
<label for="check1">Checked value</label>
</p>
<p>
<input id="check2" type="checkbox" name="check"">
<label for="check2">Unchecked value</label>
</p>
<p>
<input id="check3" type="checkbox">
<label for="check3" class="toggle">Only two values</label>
</p>
</form>
</div>

The class is present in <label>, not <input>, So you should add the class selector with the <label>.
input[type="checkbox"]:not(:checked) + label.toggle:before,
input[type="checkbox"]:checked + label.toggle:before {
background: red;
}
It is better to use the . operator to match elements having particular class since the attribute selector [class="toggle"] will only match elements strictly having just toggle class
/*CHECKBOX STYLING*/
/*font styling*/
/* Base for label styling */
input[type="checkbox"]:not(:checked),
input[type="checkbox"]:checked {
position: absolute;
left: -9999px;
}
input[type="checkbox"]:not(:checked) + label,
input[type="checkbox"]:checked + label {
position: relative;
padding-left: 42px;
cursor: pointer;
font-size: 16pt;
}
/* checkbox aspect */
[type="checkbox"]:not(:checked) + label:before,
[type="checkbox"]:checked + label:before {
content: '';
position: absolute;
left: 0;
top: 0px;
width: 22px;
height: 22px;
border: 2px solid #d7d6d5;
border-radius: 4px;
background: #f1f5f8;
}
[type="checkbox"]:checked + label:before {
border-color: #555555;
}
input[type="checkbox"]:not(:checked) + label.toggle:before,
input[type="checkbox"]:checked + label.toggle:before {
background: red;
}
/*checked mark aspect */
[type="checkbox"]:not(:checked) + label:after,
[type="checkbox"]:checked + label:after {
content: '\e909';
font-family: 'icomoon';
position: absolute;
top: 8px;
left: 4px;
font-size: 10pt;
line-height: 0.8;
color: #555555;
}
/* 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);
}
<div class="checkbox">
<form action="#">
<p>
<input id="check1" type="checkbox" name="check" checked="checked">
<label for="check1">Checked value</label>
</p>
<p>
<input id="check2" type="checkbox" name="check">
<label for="check2 ">Unchecked value</label>
</p>
<p>
<input id="check3 " type="checkbox">
<label for="check3 " class="toggle ">Only two values</label>
</p>
</form>
</div>

Related

Radio button does not change background color when on click HTML/CSS

I want to change background color of radio button by orange when I click on another radio button it changes color from white to orange as this
Code Snippet :
<!DOCTYPE html>
<html>
<head>
<style>
input [type="radio"]:checked,
input [type="radio"]:not(:checked) {
position: absolute;
left: -9999px;
}
input [type="radio"]:checked + label,
input [type="radio"]:not(:checked) + label {
position: relative;
padding-left: 28px;
cursor: pointer;
line-height: 20px;
display: inline-block;
color: #666;
}
input [type="radio"]:checked + label:before,
input [type="radio"]:not(:checked) + label:before {
content: '';
position: absolute;
left: 0;
top: 0;
width: 18px;
height: 18px;
border: 1px solid #ddd;
border-radius: 100%;
background: white;
}
input [type="radio"]:checked + label:after,
input [type="radio"]:not(:checked) + label:after {
content: '';
width: 12px;
height: 12px;
background: orange;
position: absolute;
top: 4px;
left: 4px;
border-radius: 100%;
-webkit-transition: all 0.2s ease;
transition: all 0.2s ease;
}
input [type="radio"]:not(:checked) + label:after {
opacity: 0;
-webkit-transform: scale(0);
transform: scale(0);
}
input [type="radio"]:checked + label:after {
opacity: 1;
-webkit-transform: scale(1);
transform: scale(1);
}
</style>
</head>
<body>
<div class="radio-1">
<input type="radio" class="radio-buttons" id="male" name="gender" value="male">
<label name="gender">Male</label>
</div>
<div class="radio-2">
<input type="radio" class="radio-buttons" id="female" name="gender" value="female">
<label name="gender">Female</label>
</div>
</body>
</html>
The code does not working as expected.
please tell me how can I improve this code so that change the radio button background color.
Any help will be awesome! Thanks!
You need to remove the spaces after input in css selectors so that browser can select the right element in the dom, also you need to add for="" attribute to your labels for respective radio buttons so when you click on labels then it changes the respective radio button also, rest of you code is perfect !
<!DOCTYPE html>
<html>
<head>
<style>
input[type="radio"]:checked,
input[type="radio"]:not(:checked) {
position: absolute;
left: -9999px;
}
input[type="radio"]:checked + label,
input[type="radio"]:not(:checked) + label {
position: relative;
padding-left: 28px;
cursor: pointer;
line-height: 20px;
display: inline-block;
color: #666;
}
input[type="radio"]:checked + label:before,
input[type="radio"]:not(:checked) + label:before {
content: '';
position: absolute;
left: 0;
top: 0;
width: 18px;
height: 18px;
border: 1px solid #ddd;
border-radius: 100%;
background: white;
}
input[type="radio"]:checked + label:after,
input[type="radio"]:not(:checked) + label:after {
content: '';
width: 12px;
height: 12px;
background: orange;
position: absolute;
top: 4px;
left: 4px;
border-radius: 100%;
-webkit-transition: all 0.2s ease;
transition: all 0.2s ease;
}
input[type="radio"]:not(:checked) + label:after {
opacity: 0;
-webkit-transform: scale(0);
transform: scale(0);
}
input[type="radio"]:checked + label:after {
opacity: 1;
-webkit-transform: scale(1);
transform: scale(1);
}
</style>
</head>
<body>
<div class="radio-1">
<input type="radio" class="radio-buttons" id="male" name="gender" value="male">
<label name="gender" for="male">Male</label>
</div>
<div class="radio-2">
<input type="radio" class="radio-buttons" id="female" name="gender" value="female">
<label name="gender" for="female">Female</label>
</div>
</body>
</html>
There are two small issues in the code you posted. Fixing those should make the buttons work as you intended:
In the CSS, you have spaces between input and [type="radio"], where you want to make it input[type="radio"] instead (so without a space inbetween).
In the HTML, there must be a for attribute on the label, which links to the id of the accompanying input. So for="male" on one label, and for="female" on the other. Otherwise a click on the label will not trigger a click on the hidden input.
I hope that helps!

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>

How do I customize checkboxes so that it looks similar to the image attached?

How do I style multiple checkboxes in an html form so that the appearance of the checkboxes is similar to as shown in the attached image?
You have to manually customize your input & label in a way using pseudo elements to get the effect in the checkbox you want here.
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>
This is how you can do it with no javascript. You can customize the appearence however you want.
li {
width:200px;
list-style:none;
position:relative;
height: 30px;
line-height:30px;
margin:10px 0;
}
label{
cursor:pointer;
width:100%;
float:left;
}
input[type=checkbox] {
appearance:none;
-webkit-appearance:none;
-moz-appearance:none;
}
input[type=checkbox]:after {
content: "";
width: 30px;
height: 30px;
background:url("http://www.clker.com/cliparts/c/C/o/E/v/d/check-no-md.png") no-repeat;
background-size:contain;
top: 0px;
right: 0px;
position: absolute;
display: inline-block;
}
input[type=checkbox]:checked:after {
content: "";
width: 30px;
height: 30px;
background:url("http://www.clker.com/cliparts/u/3/i/2/t/D/check-yes-md.png") no-repeat;
background-size:contain;
top: 0px;
right: 0px;
position: absolute;
display: inline-block;
}
<ul>
<li>
<label>dededede<input name="checkbox1" type="checkbox" /></label>
</li>
<li>
<label>Checkbox1 <input name="checkbox2" type="checkbox" /></label>
</li>
</ul>

Clicking label doesn't check my input field

I'm using the following code. The checkbox doesn't want to be checked with the style I gave it.
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<div class="checkbox">
<input name="remember" type="checkbox" ><label>Wachtwoord onthouden</label>
</div>
</div>
</div>'
And css:
col-md-4.control-label, #email, #password {
margin: 20px auto;
width: 300px;
resize: none;
font-family: 'Exo 2', sans-serif;
font-size: 1em;
outline: none;
}
.btn.btn-link, .checkbox {
display: block;
margin-top:20px;
margin-bottom:20px;
color: $white;
}
[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;
}
/* checkbox aspect */
[type="checkbox"]:not(:checked) + label:before,
[type="checkbox"]:checked + label:before {
content: '';
position: absolute;
left:0; top: 2px;
width: 17px; height: 17px;
border: 1px solid #aaa;
background: #f8f8f8;
border-radius: 3px;
box-shadow: inset 0 1px 3px rgba(0,0,0,.3)
}
/* checked mark aspect */
[type="checkbox"]:not(:checked) + label:after,
[type="checkbox"]:checked + label:after {
content: '✔';
position: absolute;
top: 3px; left: 4px;
font-size: 18px;
line-height: 0.8;
color: $background_color_green;
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);
}
It's pretty weird since I use the exact same style for some other part on the website, but it does work on there. It should work on both this part as that part, right?
You'll have to assign the label to the correct input, so either use the for attribute, or place your label around the input, (but that would break your current CSS code).
The following HTML should produce the desired result.
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<div class="checkbox">
<input id="remember" name="remember" type="checkbox" ><label for="remember">Wachtwoord onthouden</label>
</div>
</div>
</div>

How to keep custom checkbox and label in same scalable, relative position

I'm currently using this codepen example as a template for my checkbox input style and I'm very pleased with it on the whole. There is a very big problem though that the label/checkmark isn't attached to the checkbox cell itself, and fixed/relative positioning has been applied which assumes that the screen size/scale doesn't change. I need these elements to realign themselves together, at the same time, whenever the scale/size of the viewport changes. My code is below. Thanks in advance to any and all help, as always!
HTML/React.js file:
render() {
const styles = require('./checkbox.scss');
return (
<div className={styles.checkbox}>
<div id="cbArray">
<div id="confInput">
<div id="chkLabel"><h3>I understand this</h3></div>
</div>
<div id="cbComp">
<input type="checkbox" ref="chkBox1" id="test1" onChange={this.handleChange} />
<label htmlFor="test1"></label>
</div>
</div>
</div>
);
}
CSS:
/* 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: 25px;
cursor: pointer;
}
/* checkbox aspect */
[type="checkbox"]:not(:checked) + label:before,
[type="checkbox"]:checked + label:before {
content: '';
position: absolute;
left:0; top: 2px;
width: 17px; height: 17px;
border: 1px solid #aaa;
background: #f8f8f8;
border-radius: 3px;
box-shadow: inset 0 1px 3px rgba(0,0,0,.3)
}
/* checked mark aspect */
[type="checkbox"]:not(:checked) + label:after,
[type="checkbox"]:checked + label:after {
content: '✔';
position: absolute;
top: 0; left: 4px;
font-size: 14px;
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: 1px dotted blue;
}
/* hover style just for information */
label:hover:before {
border: 1px solid #4778d9!important;
}