CSS only radio buttons not being checked on click - css

CSS only, I prefer not to add any additional JS. The 2nd radio button is not being "checked" when clicked.
Here's fiddle already created.
HTML:
<div class="account-container">
<div class="form-group">
<div class="control-container clearfix">
<div class="col-md-4 radio">
<input type="radio" name="Gender" value="Mujer" checked />
<label>Mujer</label>
</div>
<div class="col-md-4 radio">
<input type="radio" name="Gender" value="Hombre" />
<label>Hombre</label>
</div>
</div>
</div>
</div>
CSS:
.account-container .radio {
height: 100%;
margin: 0;
}
.account-container .radio label {
position: relative;
line-height: 40px;
cursor: pointer;
padding-left: 30px;
}
.account-container .radio label:before {
content: " ";
display: inline-block;
width: 22px;
height: 22px;
position: absolute;
left: 0;
top: 9px;
border: 1px solid #1fb5e1;
background-color: #fff;
border-radius: 50%;
}
.account-container .radio input[type="radio"]:checked + label:after {
background-color: #1fb5e1;
width: 14px;
height: 14px;
border-radius: 50%;
position: absolute;
content: " ";
left: 5px;
top: 14px;
}

<input type="radio" id="id_of_radio">
<label for="id_of_radio">

The issue is the position of your radio items, since they are not visible, you cannot click on it.
You should use the capability of the label, when you click on it, the bounded element is selected.
http://jsfiddle.net/fvzegu6o/1/
<div class="col-md-4 radio">
<input type="radio" name="Gender" value="Mujer" id="mujer" checked />
<label for="mujer">Mujer</label>
</div>
<div class="col-md-4 radio">
<input type="radio" name="Gender" value="Hombre" id="hombre" />
<label for="hombre">Hombre </label>
</div>

Related

HTML form field won't keep pseudoclass

I have the following form:
body {
background: #fff;
color: 404040
}
form {
display: block;
position: relative;
width: 80%;
background: #f9f9f9;
margin: 10px auto;
padding: 30px;
}
label {
display: block;
position: relative;
top: -20px;
left: 0px;
color: #999;
font-family: 'Helvetica', sans-serif;
font-size: 16px;
z-index: 1;
transition: all 0.3s ease-out;
}
input,
input:optional {
display: block;
position: relative;
background: none;
border: none;
border-bottom: 1px solid #ddd;
width: 100%;
font-family: 'Helvetica', sans-serif;
font-weight: bold;
font-size: 16px;
z-index: 2;
}
input:focus,
input:valid {
outline: none;
border-bottom: 1px solid #00aced;
}
input:focus+label,
input:required:valid+label {
top: -40px;
font-size: 11px;
color: #00aced;
}
.divider {
position: relative;
height: 30px;
width: auto;
background: none;
}
#hex1 {
width: 75px;
margin: 10px auto;
}
.headline {
margin-left: 100px;
font-size: 24px;
}
.submitBtn {
width: 250px;
height: 75px;
border-radius: 3px;
background: #37afac;
margin: 0 auto;
color: #fff
}
#title {
margin-top: -60px;
margin-left: 80px;
margin-bottom: 20px;
}
<form>
<h3>Details about the person you are reporting:</h3>
<div class='divider'></div>
<input type="text" name="firstname" required autocomplete="off" />
<label for="firstname">First Name</label>
<div class='divider'></div>
<input type="text" name="lastname" required autocomplete="off" />
<label for="lastname">Last Name</label>
<div class='divider'></div>
<input type="text" name="age" class="test" autocomplete="off" />
<label for="age">Age</label>
<div class='divider'></div>
<input type="text" name="gender" required autocomplete="off" />
<label for="gender">Gender</label>
<div class='divider'></div>
<input type="text" name="address 1" autocomplete="off" />
<label for="address1">Address 1</label>
<div class='divider'></div>
<input type="text" name="address 2" autocomplete="off" />
<label for="address2">Address 2</label>
<div class='divider'></div>
<input type="text" name="city" required autocomplete="off" />
<label for="city">City</label>
<div class='divider'></div>
<input type="text" name="state" required autocomplete="off" />
<label for="state">State</label>
<div class='divider'></div>
<input type="text" name="position" autocomplete="off" />
<label for="Position">Position (coach, referee, etc)</label>
<div class='divider'></div>
<input type="text" name="program" required autocomplete="off" />
<label for="Program">Program where individual works</label>
<div class='divider'></div>
<input type="text" name="IncidentDescription" required autocomplete="off" />
<label for="IncidentDescription">Incident description</label>
<div class='divider'></div>
<input type="text" name="IncidentLocation" required autocomplete="off" />
<label for="IncidentLocation">Incident location</label>
<div class='divider'></div>
Codepen: https://codepen.io/anon/pen/PazrBw
As each input receives focus, it animates via receiving a class in css. Using basic html5 validation, it's supposed to retain that class to show that it's been properly filled out.
My problem is that I need some fields to be not required. When fields are not marked required, the form automatically applies some of the :valid pseudoclass (the blue underline). The even bigger issue is that when it loses focus, it loses that class and collapses the label back down onto the input text.
What am I missing here? thank you!
You are not missing anything, it all behaves the way it's supposed to be based on your code. So you need an alternative approach.
I suggest using :placeholder-shown selector instead of :valid. This way, empty optional fields will no longer get selected.
To make this work, you first need to add placeholder=" " to your inputs. This feels a bit hacky because this way you are losing the ability to use placeholders in case you need them but remember that you are already saying no to using placeholders by putting labels in their place (well, if you ever need to include placeholders for the sake of accessibility, do it, and instead of empty placeholders put real ones, then hide them with CSS. Another topic, another time).
:placeholder-shown does not have IE/old browser support so we'll go with a good example of progressive enhancement here:
Label text will be already minimised and positioned on top of the input as a default (that is top: -40px; font-size: 11px;) so that it does not mess with user input. I believe that is good enough for IE/old browsers.
Then we add the nice animation effect for browsers that do support :placeholder-shown by including following styles:
input:not(:focus):placeholder-shown + label {
top: -20px;
font-size: 16px;
}
:not(:focus) is necessary to resize and reposition label as soon as user focuses an input because some browsers show placeholder even after focus (and hide it only after user starts typing).
body {
background: #fff;
color: 404040
}
form {
display: block;
position: relative;
width: 80%;
background: #f9f9f9;
margin: 10px auto;
padding: 30px;
}
label {
display: block;
position: relative;
/* top: -20px; */
top: -40px;
left: 0px;
color: #999;
font-family: 'Helvetica', sans-serif;
/* font-size: 16px; */
font-size: 11px;
z-index: 1;
transition: all 0.3s ease-out;
}
input/*,
input:optional */ {
display: block;
position: relative;
background: none;
border: none;
border-bottom: 1px solid #ddd;
width: 100%;
font-family: 'Helvetica', sans-serif;
font-weight: bold;
font-size: 16px;
z-index: 2;
}
input:focus/*,
input:valid*/ {
outline: none;
border-bottom: 1px solid #00aced;
}
input:focus+label/*,
input:required:valid+label*/ {
color: #00aced;
}
/* New styles >> */
input:not(:focus):placeholder-shown + label {
top: -20px;
font-size: 16px;
}
/* << */
.divider {
position: relative;
height: 30px;
width: auto;
background: none;
}
#hex1 {
width: 75px;
margin: 10px auto;
}
.headline {
margin-left: 100px;
font-size: 24px;
}
.submitBtn {
width: 250px;
height: 75px;
border-radius: 3px;
background: #37afac;
margin: 0 auto;
color: #fff
}
#title {
margin-top: -60px;
margin-left: 80px;
margin-bottom: 20px;
}
<form>
<h3>Details about the person you are reporting:</h3>
<div class='divider'></div>
<input placeholder=" " type="text" name="firstname" required autocomplete="off" />
<label for="firstname">First Name</label>
<div class='divider'></div>
<input placeholder=" " type="text" name="lastname" required autocomplete="off" />
<label for="lastname">Last Name</label>
<div class='divider'></div>
<input placeholder=" " type="text" name="age" class="test" autocomplete="off" />
<label for="age">Age</label>
<div class='divider'></div>
<input placeholder=" " type="text" name="gender" required autocomplete="off" />
<label for="gender">Gender</label>
<div class='divider'></div>
<input placeholder=" " type="text" name="address 1" autocomplete="off" />
<label for="address1">Address 1</label>
<div class='divider'></div>
<input placeholder=" " type="text" name="address 2" autocomplete="off" />
<label for="address2">Address 2</label>
<div class='divider'></div>
<input placeholder=" " type="text" name="city" required autocomplete="off" />
<label for="city">City</label>
<div class='divider'></div>
<input placeholder=" " type="text" name="state" required autocomplete="off" />
<label for="state">State</label>
<div class='divider'></div>
<input placeholder=" " type="text" name="position" autocomplete="off" />
<label for="Position">Position (coach, referee, etc)</label>
<div class='divider'></div>
<input placeholder=" " type="text" name="program" required autocomplete="off" />
<label for="Program">Program where individual works</label>
<div class='divider'></div>
<input placeholder=" " type="text" name="IncidentDescription" required autocomplete="off" />
<label for="IncidentDescription">Incident description</label>
<div class='divider'></div>
<input placeholder=" " type="text" name="IncidentLocation" required autocomplete="off" />
<label for="IncidentLocation">Incident location</label>
<div class='divider'></div>

Bootstrap 4 removes CSS styling for small screens?

I have been out of the web development scene for a number of years and am getting back into it and developing a website using Bootstrap 4.
This maybe a stupid question but I have some CSS styling for checkboxes for some form elements and when reducing the screen size < 768px the styling vanishes...
Is this something fundamental to Bootstrap (although have not had this happen with any other CSS styled elements?) or am I doing something very obviously wrong?
Here are some code segments:
.question input {
position: absolute;
left: -9999px;
}
.question label {
display: block;
position: relative;
margin: 5px;
padding: 15px 30px 15px 55px;
border: 3px solid #fff;
border-radius: 20px;
color: #fff;
background-color: #005552;
white-space: pre-wrap;
cursor: pointer;
user-select: none;
transition: background-color .2s, box-shadow .2s;
}
.question label::before {
content: '';
display: block;
position: absolute;
top: 10px;
bottom: 10px;
left: 10px;
width: 32px;
border: 3px solid #fff;
border-radius: 20px;
transition: background-color .2s;
}
.question input:checked + label {
background-color: #ab576c;
}
.question input:checked + label::before {
background-color: #fff;
}
<form target="nextQuestion.php" action="post" style="margin-bottom: 0px;">
<input type="hidden" name="percentage" id="percentage">
<input type="hidden" name="currentQuestion" id="currentQuestion" value="1">
<div class="container-fluid">
<div class="row">
<div class="col-12 my-auto toxic-red-background" align="center">
<h6 class="white-text">Your Progress Through Our Questionnaire: </h6>
<div class="progress">
<div class="progress-bar-animated bg-primary" role="progressbar" style="width: 5%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-12 toxic-teal-background questionnaire-left-section question">
<h3>Q1: Would you use out website again?</h3>
<p> </p>
<input type="hidden" value="Question2">
<input id="yes" type="checkbox" class="QuestionnaureInput">
<label for="yes" class="QuestionnaireLabel">Yes</label>
<input id="no" type="checkbox" class="QuestionnaureInput">
<label for="no" class="QuestionnaireLabel">No</label>
<input id="maybe" type="checkbox" class="QuestionnaureInput">
<label for="maybe" class="QuestionnaireLabel">Maybe</label>
</div>
</div>
<div class="row">
<div class="col-sm-12 clearfix">
<button class="btn btn-primary mb-2 float-left">Prev</button>
<button type="submit" class="btn btn-primary mb-2 float-right">Next</button>
</div>
</div>
</div>
</form>
Here are some screen grabs to illustrate what is happening:
On larger screen - All OK
Screen resized to < 768px and styling is removed
Any suggestions, answers or guidance gratefully received as I am pulling my hair out!!
Thanks in Advance!

Customize Bootstrap checkboxes

I'm using Bootstrap in my Angular application and all other styles are working like they should, but checkbox style doesn't. It just look like plain old checkbox.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<div class="container">
<form class="form-signin">
<h2 class="form-signin-heading">Please log in</h2>
<label for="inputEmail" class="sr-only">User name</label>
<input [(ngModel)]="loginUser.Username" type="username" name="username" id="inputEmail" class="form-control" placeholder="User name" required autofocus>
<label for="inputPassword" class="sr-only">Password</label>
<input [(ngModel)]="loginUser.Password" type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required>
<a *ngIf="register == false" (click)="registerState()">Register</a>
<div class="checkbox">
<label>
<input type="checkbox" [(ngModel)]="rememberMe" name="rememberme"> Remember me
</label>
</div>
<button *ngIf="register == false" (click)="login()" class="btn btn-lg btn-primary btn-block" type="submit">Log in</button>
</form>
</div>
What it looks like:
What I want it to look like with Bootstrap style:
Since Bootstrap 3 doesn't have a style for checkboxes I found a custom made that goes really well with Bootstrap style.
Checkboxes
.checkbox label:after {
content: '';
display: table;
clear: both;
}
.checkbox .cr {
position: relative;
display: inline-block;
border: 1px solid #a9a9a9;
border-radius: .25em;
width: 1.3em;
height: 1.3em;
float: left;
margin-right: .5em;
}
.checkbox .cr .cr-icon {
position: absolute;
font-size: .8em;
line-height: 0;
top: 50%;
left: 15%;
}
.checkbox label input[type="checkbox"] {
display: none;
}
.checkbox label input[type="checkbox"]+.cr>.cr-icon {
opacity: 0;
}
.checkbox label input[type="checkbox"]:checked+.cr>.cr-icon {
opacity: 1;
}
.checkbox label input[type="checkbox"]:disabled+.cr {
opacity: .5;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Default checkbox -->
<div class="checkbox">
<label>
<input type="checkbox" value="">
<span class="cr"><i class="cr-icon glyphicon glyphicon-ok"></i></span>
Option one
</label>
</div>
<!-- Checked checkbox -->
<div class="checkbox">
<label>
<input type="checkbox" value="" checked>
<span class="cr"><i class="cr-icon glyphicon glyphicon-ok"></i></span>
Option two is checked by default
</label>
</div>
<!-- Disabled checkbox -->
<div class="checkbox disabled">
<label>
<input type="checkbox" value="" disabled>
<span class="cr"><i class="cr-icon glyphicon glyphicon-ok"></i></span>
Option three is disabled
</label>
</div>
Radio
.checkbox label:after,
.radio label:after {
content: '';
display: table;
clear: both;
}
.checkbox .cr,
.radio .cr {
position: relative;
display: inline-block;
border: 1px solid #a9a9a9;
border-radius: .25em;
width: 1.3em;
height: 1.3em;
float: left;
margin-right: .5em;
}
.radio .cr {
border-radius: 50%;
}
.checkbox .cr .cr-icon,
.radio .cr .cr-icon {
position: absolute;
font-size: .8em;
line-height: 0;
top: 50%;
left: 13%;
}
.radio .cr .cr-icon {
margin-left: 0.04em;
}
.checkbox label input[type="checkbox"],
.radio label input[type="radio"] {
display: none;
}
.checkbox label input[type="checkbox"]+.cr>.cr-icon,
.radio label input[type="radio"]+.cr>.cr-icon {
opacity: 0;
}
.checkbox label input[type="checkbox"]:checked+.cr>.cr-icon,
.radio label input[type="radio"]:checked+.cr>.cr-icon {
opacity: 1;
}
.checkbox label input[type="checkbox"]:disabled+.cr,
.radio label input[type="radio"]:disabled+.cr {
opacity: .5;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.10/css/all.css" integrity="sha384-+d0P83n9kaQMCwj8F4RJB66tzIwOKmrdb46+porD/OvrJ+37WqIM7UoBtwHO6Nlg" crossorigin="anonymous">
<!-- Default radio -->
<div class="radio">
<label>
<input type="radio" name="o3" value="">
<span class="cr"><i class="cr-icon fa fa-circle"></i></span>
Option one
</label>
</div>
<!-- Checked radio -->
<div class="radio">
<label>
<input type="radio" name="o3" value="" checked>
<span class="cr"><i class="cr-icon fa fa-circle"></i></span>
Option two is checked by default
</label>
</div>
<!-- Disabled radio -->
<div class="radio disabled">
<label>
<input type="radio" name="o3" value="" disabled>
<span class="cr"><i class="cr-icon fa fa-circle"></i></span>
Option three is disabled
</label>
</div>
Custom icons
You can choose your own icon between the ones from Bootstrap or Font Awesome by changing [icon name] with your icon.
<span class="cr"><i class="cr-icon [icon name]"></i>
For example:
glyphicon glyphicon-remove for Bootstrap, or
fa fa-bullseye for Font Awesome
.checkbox label:after,
.radio label:after {
content: '';
display: table;
clear: both;
}
.checkbox .cr,
.radio .cr {
position: relative;
display: inline-block;
border: 1px solid #a9a9a9;
border-radius: .25em;
width: 1.3em;
height: 1.3em;
float: left;
margin-right: .5em;
}
.radio .cr {
border-radius: 50%;
}
.checkbox .cr .cr-icon,
.radio .cr .cr-icon {
position: absolute;
font-size: .8em;
line-height: 0;
top: 50%;
left: 15%;
}
.radio .cr .cr-icon {
margin-left: 0.04em;
}
.checkbox label input[type="checkbox"],
.radio label input[type="radio"] {
display: none;
}
.checkbox label input[type="checkbox"]+.cr>.cr-icon,
.radio label input[type="radio"]+.cr>.cr-icon {
opacity: 0;
}
.checkbox label input[type="checkbox"]:checked+.cr>.cr-icon,
.radio label input[type="radio"]:checked+.cr>.cr-icon {
opacity: 1;
}
.checkbox label input[type="checkbox"]:disabled+.cr,
.radio label input[type="radio"]:disabled+.cr {
opacity: .5;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.10/css/all.css" integrity="sha384-+d0P83n9kaQMCwj8F4RJB66tzIwOKmrdb46+porD/OvrJ+37WqIM7UoBtwHO6Nlg" crossorigin="anonymous">
<div class="checkbox">
<label>
<input type="checkbox" value="" checked>
<span class="cr"><i class="cr-icon glyphicon glyphicon-remove"></i></span>
Bootstrap - Custom icon checkbox
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="o3" value="" checked>
<span class="cr"><i class="cr-icon fa fa-bullseye"></i></span>
Font Awesome - Custom icon radio checked by default
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="o3" value="">
<span class="cr"><i class="cr-icon fa fa-bullseye"></i></span>
Font Awesome - Custom icon radio
</label>
</div>
You have to use Bootstrap version 4 with the custom-* classes to get this style:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- example code of the bootstrap website -->
<label class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input">
<span class="custom-control-indicator"></span>
<span class="custom-control-description">Check this custom checkbox</span>
</label>
<!-- your code with the custom classes of version 4 -->
<div class="checkbox">
<label class="custom-control custom-checkbox">
<input type="checkbox" [(ngModel)]="rememberMe" name="rememberme" class="custom-control-input">
<span class="custom-control-indicator"></span>
<span class="custom-control-description">Remember me</span>
</label>
</div>
Documentation: https://getbootstrap.com/docs/4.0/components/forms/#checkboxes-and-radios-1
Custom checkbox style on Bootstrap version 3?
Bootstrap version 3 doesn't have custom checkbox styles, but you can use your own. In this case: How to style a checkbox using CSS?
These custom styles are only available since version 4.
/* 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;
}
/* 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: #eee;
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);
}
<div class="container">
<h1>Custom Checkboxes</h1></br>
<label class="customcheck">One
<input type="checkbox" checked="checked">
<span class="checkmark"></span>
</label>
<label class="customcheck">Two
<input type="checkbox">
<span class="checkmark"></span>
</label>
<label class="customcheck">Three
<input type="checkbox">
<span class="checkmark"></span>
</label>
<label class="customcheck">Four
<input type="checkbox">
<span class="checkmark"></span>
</label>
</div>
Here you have an example styling checkboxes and radios using Font Awesome 5 free[
/*General style*/
.custom-checkbox label, .custom-radio label {
position: relative;
cursor: pointer;
color: #666;
font-size: 30px;
}
.custom-checkbox input[type="checkbox"] ,.custom-radio input[type="radio"] {
position: absolute;
right: 9000px;
}
/*Custom checkboxes style*/
.custom-checkbox input[type="checkbox"]+.label-text:before {
content: "\f0c8";
font-family: "Font Awesome 5 Pro";
speak: none;
font-style: normal;
font-weight: normal;
font-variant: normal;
text-transform: none;
line-height: 1;
-webkit-font-smoothing: antialiased;
width: 1em;
display: inline-block;
margin-right: 5px;
}
.custom-checkbox input[type="checkbox"]:checked+.label-text:before {
content: "\f14a";
color: #2980b9;
animation: effect 250ms ease-in;
}
.custom-checkbox input[type="checkbox"]:disabled+.label-text {
color: #aaa;
}
.custom-checkbox input[type="checkbox"]:disabled+.label-text:before {
content: "\f0c8";
color: #ccc;
}
/*Custom checkboxes style*/
.custom-radio input[type="radio"]+.label-text:before {
content: "\f111";
font-family: "Font Awesome 5 Pro";
speak: none;
font-style: normal;
font-weight: normal;
font-variant: normal;
text-transform: none;
line-height: 1;
-webkit-font-smoothing: antialiased;
width: 1em;
display: inline-block;
margin-right: 5px;
}
.custom-radio input[type="radio"]:checked+.label-text:before {
content: "\f192";
color: #8e44ad;
animation: effect 250ms ease-in;
}
.custom-radio input[type="radio"]:disabled+.label-text {
color: #aaa;
}
.custom-radio input[type="radio"]:disabled+.label-text:before {
content: "\f111";
color: #ccc;
}
#keyframes effect {
0% {
transform: scale(0);
}
25% {
transform: scale(1.3);
}
75% {
transform: scale(1.4);
}
100% {
transform: scale(1);
}
}
<script src="https://kit.fontawesome.com/2a10ab39d6.js"></script>
<div class="col-md-4">
<form>
<h2>1. Customs Checkboxes</h2>
<div class="custom-checkbox">
<div class="form-check">
<label>
<input type="checkbox" name="check" checked> <span class="label-text">Option 01</span>
</label>
</div>
<div class="form-check">
<label>
<input type="checkbox" name="check"> <span class="label-text">Option 02</span>
</label>
</div>
<div class="form-check">
<label>
<input type="checkbox" name="check"> <span class="label-text">Option 03</span>
</label>
</div>
<div class="form-check">
<label>
<input type="checkbox" name="check" disabled> <span class="label-text">Option 04</span>
</label>
</div>
</div>
</form>
</div>
<div class="col-md-4">
<form>
<h2>2. Customs Radios</h2>
<div class="custom-radio">
<div class="form-check">
<label>
<input type="radio" name="radio" checked> <span class="label-text">Option 01</span>
</label>
</div>
<div class="form-check">
<label>
<input type="radio" name="radio"> <span class="label-text">Option 02</span>
</label>
</div>
<div class="form-check">
<label>
<input type="radio" name="radio"> <span class="label-text">Option 03</span>
</label>
</div>
<div class="form-check">
<label>
<input type="radio" name="radio" disabled> <span class="label-text">Option 04</span>
</label>
</div>
</div>
</form>
</div>
As others have said, the style you're after is actually just the Mac OS checkbox style, so it will look radically different on other devices.
In fact both screenshots you linked show what checkboxes look like on Mac OS in Chrome, the grey one is shown at non-100% zoom levels.
For bootstrap 5, if the switch/checkbox is not styled, add appearance in your css file.
For example:
.form-switch, .form-check {
.form-check-input {
-webkit-appearance: none;
-moz-appearance: none;
}
}

CSS styling for input tags inside labels

I am using the below filter to sort a list:
<fieldset class="groups">
<h4 class="cufon_headings">Groups</h4>
<div class="checkbox">
<input type="checkbox" value="member1">
<label>Member 1</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" value="member2">
Member 2</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" value="member3">
Member 3</label>
</div>
</fieldset>
This is my CSS for the above filter:
/* Checkbox Styles*/
fieldset {
display: inline-block;
vertical-align: top;
margin: 0 1em 0 0;
background: #fff;
padding: .5em;
border-radius: 3px;
}
.checkbox{
display: block;
position: relative;
cursor: pointer;
margin-bottom: 8px;
}
.checkbox input[type="checkbox"]{
position: absolute;
display: block;
top: 0;
left: 0;
height: 100%;
width: 100%;
cursor: pointer;
margin: 0;
opacity: 0;
z-index: 1;
}
.checkbox label{
display: inline-block;
vertical-align: top;
text-align: left;
padding-left: 2em;
}
.checkbox label:before,
.checkbox label:after{
content: '';
display: block;
position: absolute;
}
.checkbox label:before{
left: 0;
top: 0;
width: 18px;
height: 18px;
margin-right: 10px;
background: #ddd;
border-radius: 3px;
}
.checkbox label:after{
content: '';
position: absolute;
top: 4px;
left: 4px;
width: 10px;
height: 10px;
border-radius: 2px;
background: #E50082;
opacity: 0;
pointer-events: none;
}
.checkbox input:checked ~ label:after{
opacity: 1;
}
.checkbox input:focus ~ label:before{
background: #eee;
}
The first checkbox (Member 1) is correctly styled, i.e. checking the first option marks the checkbox magenta. In this example the tag is outside of the tags.
The other chekcbox options (Member 2 and Member 3) do not turn magenta upon selecting them. With both the tag is inside the tag.
The preferred variant is having the tags inside the tags. However, I could not figure out how to adapt the CSS to get this working. Can anyone provide help? (Here is a link to the code on CopePen)
<fieldset class="groups">
<h4 class="cufon_headings">Groups</h4>
<div class="checkbox">
<input type="checkbox" value="member1">
<label>Member 1</label>
</div>
<div class="checkbox">
<input type="checkbox" value="member2">
<label>Member 2</label>
</div>
<div class="checkbox">
<input type="checkbox" value="member3">
<label>Member 3</label>
</div>
</fieldset>
Make sure that your labels are correct for member 2 and 3. You had the label tag including the input tag. The code above seems to work.
Charlie's answer is correct. In CSS, you can't affect a parent - only a child.
before and after are puesdo elements and they're not exactly acting like normal elements, so they're not siblings of input.
In order to solve your problem, Change every:
<div class="checkbox">
<label>
<input type="checkbox" value="memberN">
Member N
</label>
</div>
Into:
<div class="checkbox">
<input type="checkbox" value="memberN">
<label>
Member N
</label>
</div>
Thank you for your comments. It was intended that the first checkbox differs from the others to show the effect.
If it is not possible with CSS I guess I need to adapt how the plugin Contact Form 7 for Wordpress gives out the HTML for checkboxes. Not the preferred way to handle the issue.

Checked radio buttons misaligned when using custom CSS

I'm implementing some custom styles for radio buttons and am having problems trying to perfectly center the checked buttons. When I paste the exact same code into JS Fiddle, things work perfectly. Yet in my web app, they are each off centered (in their own way) like the image below, despite having the same classnames, positioning, etc:
Image link: Off-centered checked radio buttons
HTML:
<div class="RadioStyled">
<label class="control-label">
<input
class="form-control"
type="radio"
ref="check1"
>
<span class="radio-styled checked"></span>
<span class="label-text">Australia</span>
</label>
<div class="RadioStyled">
</div>
</div>
<div class="RadioStyled">
<label class="control-label">
<input
class="form-control"
type="radio"
ref="check2"
>
<span class="radio-styled checked"></span>
<span class="label-text">Belgium</span>
</label>
<div class="RadioStyled">
</div>
</div>
<div class="RadioStyled">
<label class="control-label">
<input
class="form-control"
type="radio"
ref="check3"
>
<span class="radio-styled checked"></span>
<span class="label-text">United Kingdom</span>
</label>
<div class="RadioStyled">
</div>
</div>
<div class="some-other-element">
<div class="RadioStyled">
<label class="control-label">
<input
class="form-control"
type="radio"
ref="check3"
>
<span class="radio-styled checked"></span>
<span class="label-text">New Zealand</span>
</label>
<div class="RadioStyled">
</div>
</div>
<div class="RadioStyled">
<label class="control-label">
<input
class="form-control"
type="radio"
ref="check3"
>
<span class="radio-styled checked"></span>
<span class="label-text">Canada</span>
</label>
<div class="RadioStyled">
</div>
</div>
</div>
CSS / SCSS:
.RadioStyled {
display: inline-block;
padding-right: 25px;
label.control-label {
cursor: pointer;
display: block;
line-height: 24px;
.label-text {
position: relative;
font-size: 14px;
color: black;
padding-left: 6px;
vertical-align: middle;
}
input[type="checkbox"], input[type="radio"] {
display: none;
}
.radio-styled {
border-radius: 50%;
border: 1px solid grey;
box-sizing: border-box;
display: inline-block;
height: 20px;
vertical-align: middle;
width: 20px;
}
.radio-styled.checked {
border: 1px solid grey;
background-color: green;
border-radius: 50%;
box-sizing: border-box;
display: inline-block;
width: 20px;
height: 20px;
vertical-align: middle;
}
.radio-styled.checked::before {
background-color: white;
border-radius: 50%;
box-sizing: border-box;
content: '';
display: inline-block;
height: 20px;
width: 20px;
// margin: -1px -1px;
transform: scale(0.5);
position: relative;
top: -1px;
right: 1px;
}
}
}
.some-other-element {
padding-left: 250px;
width: 400px;
}
Codepen here
Any idea what's happening here that makes each checked radio button seem to have a different position? Thanks!

Resources