why my CSS custom radio button doesn't work? - html

I try to make radio button by myself,But it doesn't work after I click label or the input radio button.
my code as bellow.
.radio input[type="radio"] {
display: none;
}
.radio {
margin-left: 5px;
position: relative;
padding-left: 22px;
}
.radio span {
width: 18px;
height: 18px;
background-color: rgba(255, 255, 255, 0.8);
border-radius: 50%;
position: absolute;
display: inline-block;
left: 0;
top: 5px;
}
.radio span::after {
content: "";
width: 16px;
height: 16px;
background-color: #448899;
border-radius: 50%;
display: inline-block;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) scale(0);
}
.radio input[type="radio"]:checked ~ .radio span::after {
transform: translate(-50%, -50%) scale(1);
}
<label class="radio">
<span></span><input type="radio" name="time" value="2000" id="morning" required />
上半天
</label>
<label class="radio">
<span></span><input type="radio" name="time" value="2500" id="afternoon" />
下半天
</label>
Even if I connect radio checked to another element,it still doesn't work.

Update the code as follows.
.radio {
display: flex;
align-items: center;
margin-left: 5px;
position: relative;
padding-left: 22px;
margin-bottom: 10px;
cursor: pointer;
}
.radio input[type="radio"] {
position: absolute;
opacity: 0;
z-index: -1;
}
.radio span {
width: 18px;
height: 18px;
background-color: rgba(255, 255, 255, 1);
border: 1px solid #484848;
border-radius: 50%;
position: absolute;
display: inline-block;
left: 0;
}
.radio span::after {
content: "";
width: 16px;
height: 16px;
background-color: #448899;
border-radius: 50%;
display: inline-block;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) scale(0);
}
.radio input[type="radio"]:checked ~ span::after {
transform: translate(-50%, -50%) scale(1);
}
<label class="radio">
<input type="radio" name="time" value="2000" id="morning" required />
<span></span>
上半天
</label>
<label class="radio">
<input type="radio" name="time" value="2500" id="afternoon" />
<span></span>
下半天
</label>

Related

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

Checkbox Styling based on CSS

I have a checkbox which should look as below,
If it is not selected it should be as below,
I have the following css with me
input[type="checkbox"]:checked + label::after {
content: '';
position: absolute;
width: 11px;
height: 6px;
background: rgba(0,0,0,0);
top: 10px;
left: 24px;
border: 1px solid white;
border-top: none;
border-right: none;
-webkit-transform: rotate(-45deg);
-moz-transform: rotate(-45deg);
-o-transform: rotate(-45deg);
-ms-transform: rotate(-45deg);
transform: rotate(-45deg);
}
input[type="checkbox"] {
line-height: 2.1ex;
}
input[type="radio"],
input[type="checkbox"] {
position: absolute;
left: -999em;
}
input[type="checkbox"] + label {
position: relative;
overflow: hidden;
cursor: pointer;
}
input[type="checkbox"] + label::before {
content: "";
display: inline-block;
vertical-align: -25%;
height: 20px;
width: 20px;
background-color: #1BAAD3;
border-radius: 4px;
margin-right: 8px;
}
When I uncheck the checkbox this is what I can see
I'm using angular-js to handle the event of select/unselect
HTML
<div class="form-check">
<input type="checkbox" ng-if="testKits" ng-model="testKits" class="form-check-input" id="testKits">
<label class="form-check-label form-label-text source-sans-pro" for="testKits">{{'Testkits' | translate}}</label>
</div>
Please see below snippet it may work for you:
input[type="checkbox"]:checked + label::after {
content: '';
position: absolute;
width: 11px;
height: 6px;
background: rgba(0,0,0,0);
top: 0px;
left: 6px;
border: 1px solid white;
border-top: none;
border-right: none;
-webkit-transform: rotate(-45deg);
-moz-transform: rotate(-45deg);
-o-transform: rotate(-45deg);
-ms-transform: rotate(-45deg);
transform: rotate(-45deg);
}
input[type="checkbox"] {
line-height: 2.1ex;
}
input[type="radio"],
input[type="checkbox"] {
position: absolute;
left: -999em;
}
input[type="checkbox"] + label {
position: relative;
overflow: hidden;
cursor: pointer;
}
input[type="checkbox"] + label::before {
content: "";
display: inline-block;
vertical-align: -25%;
height: 20px;
width: 20px;
background-color: #fff;
border-radius: 4px;
margin-right: 8px;
border: 2px solid #ddd;
}
input[type="checkbox"]:checked + label::before {
background-color: #1BAAD3;
border: 2px solid #1BAAD3;
}
<div class="form-check">
<input type="checkbox" class="form-check-input" id="testKits">
<label class="form-check-label form-label-text source-sans-pro" for="testKits">abc</label>
</div>

Add image into custom checkbox

I've made custom checkbox, here is what i've made so far, but what I need is something like this, where there is also text and one image (not icon/fa-fa icon) in the center of the checkbox. Did u know how to achieve it? Here is the code from snippet:
div {
display: inline-block;
}
input[type="checkbox"][id^="cb"] {
display: none;
}
label {
display: block;
position: relative;
cursor: pointer;
height: 120px;
outline: 1px solid white;
width: 120px;
}
label:before {
background-color: red;
color: white;
display: block;
position: absolute;
width: 20px;
height: 20px;
top: 1px;
left: 1px;
text-align: center;
line-height: 20px;
transition-duration: 0.4s;
-webkit-transition-duration: 0.4s;
-moz-transition-duration: 0.4s;
-ms-transition-duration: 0.4s;
}
label img {
height: 100%;
width: 100%;
}
:checked + label {
border-color: red;
}
:checked + label:before {
content: "✓";
background-color: red;
outline: 1px solid red;
z-index: 99;
}
:checked + label img {
z-index: -1;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
height: 95%;
width: 95%;
outline: 3px solid red;
}
<div>
<div>
<input type="checkbox" id="cb1" />
<label for="cb1"><img src="http://lorempixel.com/103/103" /></label>
</div>
<div>
<input type="checkbox" id="cb2" />
<label for="cb2"><img src="http://lorempixel.com/102/102" /></label>
</div>
</div>
Thanks in advance for your help.
You're doing just fine at your own. Your code looks a bit complex because you can use the label as a block-element (like a div or a wrapper). Take a step back, look at your layout 'inside' the checkbox and see it as a normal layout/styling element. Nothing fancy.
.styled {
/* could also be a background */
background-color: red;
padding: 1rem 4rem;
text-align: center;
cursor: pointer;
}
span {
display: block;
font-size: 16px;
font-family: Arial;
margin-top: 10px;
}
label {
display: block;
max-width: 100px;
margin: 0 auto;
}
#checkbox {
display: none;
}
input:checked+label {
background-color: green;
}
<input id="checkbox" type="checkbox">
<label class="styled" for="checkbox">
<img src="https://placehold.it/20x20" alt="image 1">
<img src="https://placehold.it/40x40" alt="image 2">
<img src="https://placehold.it/60x60" alt="image 3">
<span>Text</span>
</label>
<div>
<div><input type="checkbox" id="cb1" />
<label for="cb1">
<img src="http://lorempixel.com/103/103" />
<img src="https://dummyimage.com/120/000000/ffffff" />
<span>Text</span>
</label>
</div>
<div><input type="checkbox" id="cb2" />
<label for="cb2">
<img src="http://lorempixel.com/102/102" />
<img src="https://dummyimage.com/120/000000/ffffff" />
<span>Text</span>
</label>
</div>
</div>
div {
display: inline-block;
}
input[type="checkbox"][id^="cb"] {
display: none;
}
label {
display: block;
position: relative;
cursor: pointer;
height: 120px;
outline: 1px solid white;
width: 120px;
}
label:before {
background-color: red;
color: white;
display: block;
position: absolute;
width: 20px;
height: 20px;
top: 1px;
left: 1px;
text-align: center;
line-height: 20px;
transition-duration: 0.4s;
}
label img {
height: 100%;
width: 100%;
z-index: 1;
}
label img + img {
height: 60%;
width: 60%;
z-index: 2;
position: absolute;
top: 10%;
left: 20%;
}
span {
z-index: 2;
color: red;
position: absolute;
top: 75%;
left: 40%;
text-align: center;
}
:checked + label {
border-color: red;
}
:checked + label:before {
content: "✓";
background-color: red;
outline: 1px solid red;
z-index: 3;
}
:checked + label img + img {
position: absolute;
top: 40%;
left: 50%;
transform: translate(-50%, -50%);
height: 60%;
width: 60%;
outline: 0px solid transparent;
}
:checked + label img {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
height: 98%;
width: 98%;
outline: 3px solid red;
}

CSS Checkbox example

I want to make this example to work. I need it on my project but i don't understand CSS and can't seem to figure out why this isn't working. Any help would be appreciated.
Why is this example working on codepen : codepen link
But is not working in jsfiddle or my website : Jsfiddle link
Code(CSS):
.inputGroup {
background-color: #fff;
display: block;
margin: 10px 0;
position: relative;
label {
padding: 12px 30px;
width: 100%;
display: block;
text-align: left;
color: #3C454C;
cursor: pointer;
position: relative;
z-index: 2;
transition: color 200ms ease-in;
overflow: hidden;
&:before {
width: 10px;
height: 10px;
border-radius: 50%;
content: '';
background-color: #5562eb;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%) scale3d(1, 1, 1);
transition: all 300ms cubic-bezier(0.4, 0.0, 0.2, 1);
opacity: 0;
z-index: -1;
}
&:after {
width: 32px;
height: 32px;
content: '';
border: 2px solid #D1D7DC;
background-color: #fff;
background-image: url("data:image/svg+xml,%3Csvg width='32' height='32' viewBox='0 0 32 32' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M5.414 11L4 12.414l5.414 5.414L20.828 6.414 19.414 5l-10 10z' fill='%23fff' fill-rule='nonzero'/%3E%3C/svg%3E ");
background-repeat: no-repeat;
background-position: 2px 3px;
border-radius: 50%;
z-index: 2;
position: absolute;
right: 30px;
top: 50%;
transform: translateY(-50%);
cursor: pointer;
transition: all 200ms ease-in;
}
}
input:checked ~ label {
color: #fff;
&:before {
transform: translate(-50%, -50%) scale3d(56, 56, 1);
opacity: 1;
}
&:after {
background-color: #54E0C7;
border-color: #54E0C7;
}
}
input {
width: 32px;
height: 32px;
order: 1;
z-index: 2;
position: absolute;
right: 30px;
top: 50%;
transform: translateY(-50%);
cursor: pointer;
visibility: hidden;
}
}
// codepen formatting
.form {
padding: 0 16px;
max-width: 550px;
margin: 50px auto;
font-size: 18px;
font-weight: 600;
line-height: 36px;
}
body {
background-color: #D1D7DC;
font-family: 'Fira Sans', sans-serif;
}
*,
*::before,
*::after {
box-sizing: inherit;
}
html {
box-sizing: border-box;
}
code {
background-color: #9AA3AC;
padding: 0 8px;
}
Here is the Html code :
<form class="form">
<h2>Checkboxes</h2>
<div class="inputGroup">
<input id="option1" name="option1" type="checkbox"/>
<label for="option1">Option One</label>
</div>
<div class="inputGroup">
<input id="option2" name="option2" type="checkbox"/>
<label for="option2">Option Two</label>
</div>
<h2>Radio Buttons</h2>
<div class="inputGroup">
<input id="radio1" name="radio" type="radio"/>
<label for="radio1">Yes</label>
</div>
<div class="inputGroup">
<input id="radio2" name="radio" type="radio"/>
<label for="radio2">No</label>
</div>
</form>
Because you are not using compiled css.
See below snippet
.inputGroup {
background-color: #fff;
display: block;
margin: 10px 0;
position: relative;
}
.inputGroup label {
padding: 12px 30px;
width: 100%;
display: block;
text-align: left;
color: #3C454C;
cursor: pointer;
position: relative;
z-index: 2;
transition: color 200ms ease-in;
overflow: hidden;
}
.inputGroup label:before {
width: 10px;
height: 10px;
border-radius: 50%;
content: '';
background-color: #5562eb;
position: absolute;
left: 50%;
top: 50%;
-webkit-transform: translate(-50%, -50%) scale3d(1, 1, 1);
transform: translate(-50%, -50%) scale3d(1, 1, 1);
transition: all 300ms cubic-bezier(0.4, 0, 0.2, 1);
opacity: 0;
z-index: -1;
}
.inputGroup label:after {
width: 32px;
height: 32px;
content: '';
border: 2px solid #D1D7DC;
background-color: #fff;
background-image: url("data:image/svg+xml,%3Csvg width='32' height='32' viewBox='0 0 32 32' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M5.414 11L4 12.414l5.414 5.414L20.828 6.414 19.414 5l-10 10z' fill='%23fff' fill-rule='nonzero'/%3E%3C/svg%3E ");
background-repeat: no-repeat;
background-position: 2px 3px;
border-radius: 50%;
z-index: 2;
position: absolute;
right: 30px;
top: 50%;
-webkit-transform: translateY(-50%);
transform: translateY(-50%);
cursor: pointer;
transition: all 200ms ease-in;
}
.inputGroup input:checked ~ label {
color: #fff;
}
.inputGroup input:checked ~ label:before {
-webkit-transform: translate(-50%, -50%) scale3d(56, 56, 1);
transform: translate(-50%, -50%) scale3d(56, 56, 1);
opacity: 1;
}
.inputGroup input:checked ~ label:after {
background-color: #54E0C7;
border-color: #54E0C7;
}
.inputGroup input {
width: 32px;
height: 32px;
order: 1;
z-index: 2;
position: absolute;
right: 30px;
top: 50%;
-webkit-transform: translateY(-50%);
transform: translateY(-50%);
cursor: pointer;
visibility: hidden;
}
.form {
padding: 0 16px;
max-width: 550px;
margin: 50px auto;
font-size: 18px;
font-weight: 600;
line-height: 36px;
}
body {
background-color: #D1D7DC;
font-family: 'Fira Sans', sans-serif;
}
*,
*::before,
*::after {
box-sizing: inherit;
}
html {
box-sizing: border-box;
}
code {
background-color: #9AA3AC;
padding: 0 8px;
}
<form class="form">
<h2>Checkboxes</h2>
<div class="inputGroup">
<input id="option1" name="option1" type="checkbox"/>
<label for="option1">Option One</label>
</div>
<div class="inputGroup">
<input id="option2" name="option2" type="checkbox"/>
<label for="option2">Option Two</label>
</div>
<h2>Radio Buttons</h2>
<div class="inputGroup">
<input id="radio1" name="radio" type="radio"/>
<label for="radio1">Yes</label>
</div>
<div class="inputGroup">
<input id="radio2" name="radio" type="radio"/>
<label for="radio2">No</label>
</div>
</form>
<link href="https://fonts.googleapis.com/css?family=Fira+Sans" rel="stylesheet">

Background Color Fill Animation Ends in Offset

I'm trying to "fill" a circle when a user clicks on it. The main issue is that when the animation ends, it's offset on the top by just a little bit. I've tried to add a margin-top: -1px to it but it's then offset on the bottom!
I'm open to any suggestion to make the animation smoother but my main question is: Why is the end result offset from the top a tiny bit and how can I fix it?
#searchOptions label {
display: inline-block;
cursor: pointer;
font-size: 14px;
text-transform: uppercase;
color: #000;
font-weight: 400;
margin-right: 40px;
}
#searchOptions label .circle {
display: inline-block;
position: relative;
border: 2px solid #000;
width: 15px;
height: 15px;
border-radius: 50%;
margin-right: 12px;
}
#searchOptions label .circle:before {
display: none;
position: absolute;
content: '';
background-color: #000;
border-radius: 50%;
top: 50%;
transform: translateY(-50%);
left: 0;
right: 0;
margin: 0 auto;
width: 2px;
height: 2px;
}
#searchOptions label .txt {
position: relative;
top: -3px;
}
#searchOptions input[type=radio] {
height: 0;
width: 0;
opacity: 0;
}
#searchOptions input[type=radio]:checked + label .circle:before {
display: block;
width: 100%;
height: 100%;
animation: fillRadio ease-in 1s;
}
#-webkit-keyframes fillRadio {
0% {
top: 50%;
transform: translateY(-50%);
width: 1%;
height: 1%;
}
100% {
top: 0%;
transform: translateY(0%);
width: 100%;
height: 100%;
}
}
<div id="searchOptions">
<input type="radio" name="searchType" value="option1" id="option1" />
<label for="option1"><span class="circle"></span> <span class="txt">Option 1</span>
</label>
<input type="radio" name="searchType" value="option2" id="option2" />
<label for="option2"><span class="circle"></span> <span class="txt">Option 2</span>
</label>
</div>
<!-- id="searchOptions" -->
Here's a JSFiddle if anyone would rather use that.
I believe that the problem comes from a dimensiion of 15px and top of 50% -> that gives a fractional size
I have changed the size to 16px, that is even:
#searchOptions label {
display: inline-block;
cursor: pointer;
font-size: 14px;
text-transform: uppercase;
color: #000;
font-weight: 400;
margin-right: 40px;
}
#searchOptions label .circle {
display: inline-block;
position: relative;
border: 2px solid #000;
width: 16px;
height: 16px;
border-radius: 50%;
margin-right: 12px;
}
#searchOptions label .circle:before {
display: none;
position: absolute;
content: '';
background-color: #000;
border-radius: 50%;
top: 50%;
transform: translateY(-50%);
left: 0;
right: 0;
margin: 0 auto;
width: 2px;
height: 2px;
}
#searchOptions label .txt {
position: relative;
top: -3px;
}
#searchOptions input[type=radio] {
height: 0;
width: 0;
opacity: 0;
}
#searchOptions input[type=radio]:checked + label .circle:before {
display: block;
width: 100%;
height: 100%;
animation: fillRadio ease-in 1s;
}
#-webkit-keyframes fillRadio {
0% {
top: 50%;
transform: translateY(-50%);
width: 1%;
height: 1%;
}
100% {
top: 0%;
transform: translateY(0%);
width: 100%;
height: 100%;
}
}
<div id="searchOptions">
<input type="radio" name="searchType" value="option1" id="option1" />
<label for="option1"><span class="circle"></span> <span class="txt">Option 1</span>
</label>
<input type="radio" name="searchType" value="option2" id="option2" />
<label for="option2"><span class="circle"></span> <span class="txt">Option 2</span>
</label>
</div>
<!-- id="searchOptions" -->