checked not working properly for custom radio/checkboxes - css

I am trying to make a custom checkbox/radio but the problem is on checked doesn't add css.
My Code:
.custom-checkbox-label {
position: relative;
cursor: pointer;
.custom-checkbox {
background: #ffffff;
border: 1px solid #cccccc;
width: 24px;
height: 24px;
border-radius: 50%;
}
input[type='radio'] {
opacity: 0;
&:checked {
.custom-checkbox {
background: #08abe1;
}
}
}
}
<label htmlFor="delivery-type" className="custom-checkbox-label">
<div className="custom-checkbox"></div>
<input
id="delivery-type"
type="radio"
onChange={() => alert(1)}
/>
</label>
This is inside React that's why I am using htmlFor, and I did check the radio does get triggered the css inside checked does not apply.

Few things wrong with this -
div is not allowed inside a label - change it to a span
Your custom checkbox need to come after the input - so you can use the adjacent css selector
Your sass selector is wrong for the checked (it currently tries to select a custom-checkbox inside your input)
you need quotes around your onchange
/* new sass
.custom-checkbox-label {
position: relative;
cursor: pointer;
.custom-checkbox {
background: #ffffff;
border: 1px solid #cccccc;
display: inline-block;
width: 24px;
height: 24px;
border-radius: 50%;
}
input[type='radio'] {
opacity: 0;
&:checked {
&+.custom-checkbox {
background: #08abe1;
}
}
}
}
Which will compile to the below: */
.custom-checkbox-label {
position: relative;
cursor: pointer;
}
.custom-checkbox-label .custom-checkbox {
background: #ffffff;
border: 1px solid #cccccc;
display: inline-block;
width: 24px;
height: 24px;
border-radius: 50%;
}
.custom-checkbox-label input[type=radio] {
opacity: 0;
}
.custom-checkbox-label input[type=radio]:checked+.custom-checkbox {
background: #08abe1;
}
<label htmlFor="delivery-type" className="custom-checkbox-label">
<input
id="delivery-type"
type="radio"
onChange="{() => alert(1)}"
/>
<span className="custom-checkbox"></span>
</label>

Related

CSS Autofill Styes Not Behaving As Desired

Using just CSS, I have been working on the login section of my site. I love offering visual feedback for each state anything is on on the page, whether you've clicked it, hovered over it, then depending on what state it is, such as invalid, disabled, or whatnot, however when using the browsers autofill, this is playing havoc on, my thought to be completed section!
I am using Google Chrome, always the latest version.
How It Currently Works
Disabled
No input focus
Enter button has disabled styling
Default
Focus changes input styles
Entering text will change Enter styles
Button has :hover and :active styles for this state
Active - This is when something has been entered and you focus out
Input border and label text color property changes, different to not having any text inputted.
What The Problem Is
I have been attempting to have all current styles and methods stay whilst you use the browser's autofill. I have almost got this completely working, but I am facing these problems:
using autofill...
When you hover over the autofill list, it previews this within the input area which seems to be a state that I do not know how to yet style. Once you click to chose the autofill option, the font-size then adjusts, however all other styles work instantly with the following
.auth input[type="text"]:-webkit-autofill,
.auth input[type="text"]:-webkit-autofill:focus {
transition: all 600000s 0s;
}
After using autofill & focus out
The focus out styles stop working IF it is an auto filled object only, you can delete and enter your own which shows the correct behaviour. Only the label is working when you focus out after autofilling.
body {
padding: 0;
margin: 0;
line-height: 24px;
font-family: "Tahoma", Helvetica, Arial;
font-size: 12px;
background: #1c1e2f;
}
*, *::before, *::after {
box-sizing: border-box;
}
* {
outline: none;
padding: 0px;
margin: 0px;
}
.container {
position: relative;
background-color: #222437;
background-clip: border-box;
border: 1px solid #313452;
border-radius: 6px;
color: white;
margin-bottom: 15px;
}
.justify-content-center {
-webkit-box-pack: center;
-moz-box-pack: center;
-ms-flex-pack: center;
justify-content: center ;
}
.d-flex {
display: -webkit-box !important;
display: -ms-flexbox !important;
display: flex !important;
}
button {
border: none;
padding: 0px;
margin: 0px;
background: none;
cursor: pointer;
}
.auth {
width: 300px;
padding: 15px;
}
.auth input[type="text"] {
display: block;
border: 1px solid #313452;
font-size: 20px;
color: #1864c9;
width: 100%;
height: 55px;
padding: 0 15px;
margin-bottom: 10px;
position: relative;
z-index: 2;
background-color: transparent;
outline: none;
border-radius: 5px;
}
.auth .inputs {
position: relative;
margin: 16px 0 0 0;
}
.auth .input-label {
position: absolute;
top: 15px;
font-size: 1.1rem;
left: 14px;
color: rgb(122, 122, 122);
font-weight: 100;
transition: 0.1s ease;
background-color: #222437;
padding: 0 5px;
}
.auth label:before { display: none; }
.auth input[type="text"]:focus ~ .input-label {
top: -12px;
color: #1864c9;
font-size: 13px;
background-color: #222437;
z-index: 2;
}
.auth input[type="text"]:target ~ .input-label {
top: -7px;
color: #1864c9;
font-size: 13px;
z-index: 2;
}
.auth input[type="text"]::placeholder { opacity: 0; }
.auth input[type="text"]:not(:placeholder-shown) { border: 2px solid rgba(26,115,232,0.25); }
.auth input[type="text"]:not(:placeholder-shown):focus { border: 2px solid rgba(26,115,232,1.00); }
.auth input[type="text"]:not(:placeholder-shown) ~ .input-label {
top: -12px;
font-size: 13px;
z-index: 2;
}
.auth input[type="text"]:not(:placeholder-shown):focus ~ .input-label { color: #1864c9; }
.auth input[type="text"]:not(:placeholder-shown) ~ .input-label { color: rgba(24,100,201,0.50); }
.auth .input:focus { border: 2px solid #1a73e8; }
.auth .link-btn {
height: 20px;
text-decoration: none;
color: #1864c9;
}
.auth .submit {
float: right;
height: 38px;
margin-top:10px;
padding: 0 25px;
border-radius: 5px;
cursor: not-allowed;
color: #313452;
border: 1px solid #313452;
}
.auth input[type="text"]:not(:placeholder-shown) ~ .submit {
background-color: #1a73e8;
color: white;
cursor: pointer;
}
.auth input[type="text"]:not(:placeholder-shown) ~ .submit:hover {
background-color: #6CA5F0;
color: #7C7C7C;
cursor: pointer;
}
.auth input[type="text"]:not(:placeholder-shown) ~ .submit:active {
background-color: #0D4186;
color: #AAAAAA;
cursor: pointer;
}
.auth input[type="text"]:-webkit-autofill,
.auth input[type="text"]:-webkit-autofill:focus {
transition: all 600000s 0s;
}
<div class="flex-row d-flex justify-content-center">
<div class="container auth">
<form method="post">
<div class="inputs">
<input type="text" name="api_key" class="input" placeholder="api key">
<label class="input-label">Enter Key</label>
<button class="submit" value="Login">Enter</button>
</div>
</form>
</div>
</div>
Edit the above snippet on CodePen
After researching autofills i found this:
https://bugs.chromium.org/p/chromium/issues/detail?id=953689
So apparently this is a known bug in chromium and other browsers, which for some reason is flagged as Won't Fix.
And if you look at the mdn:
https://developer.mozilla.org/en-US/docs/Web/CSS/:autofill
It states that this is a know problem, you can't override :-webkit-autofill !important which is set by the browser.
But since i am not happy with this answer i managed to find atleast a somewhat solution to the problem using js from another stack overflow post:
// Fix autocomplete shit
function fix_autocomplete_shit() {
setTimeout(() => {
if ($(this).is(':-internal-autofill-selected')) {
var clone = $(this).clone(true, true);
$(this).after(clone);
$(this).remove();
}
}, 10);
}
$('input').on('input', fix_autocomplete_shit);
This is a pretty shit solution, but it might help you in some way.

Floating labels - label above input field

I'm working on an existing form, where I want to introduce floating labels.
A lot of the solutions I have seen on stack overflow are great, but require the label tag, to sit directly underneath the <input> tag. For example here.
On the form I'm working on though, my label tag, sits above the <input> tag. And the input tag, sits in a seperate div. (I am not allowed to move these tags around - please don't ask, restriction of the application!).
Is there anyway, I can achieve floating CSS labels, with this structure?
.test-field {
clear: both;
margin-bottom: 20px;
position: relative;
}
.test-field__input {
font-size: 0.875em;
line-height: 1;
-moz-appearance: none;
-webkit-appearance: none;
background-color: #f5f5f5;
border: 2px solid #eee;
border-radius: 4px;
color: grey;
height: calc((40 / 14) * 1em);
line-height: 1;
outline: 0;
padding: 0 8px;
vertical-align: top;
width: 100%;
}
/* // FOCUS */
.test-field__input:focus,
.test-field__input ~ input:checked:focus {
border-color: #5d7199;
}
/* // DISABLED */
.test-field--disabled .test-field__input {
background-color: #e8e8e3;
cursor: not-allowed;
}
.test-field--disabled .test-field__flag, .test-field--disabled .test-field__label {
color: #d0d0cb;
}
/* // ERROR */
.test-field--error .test-field__input, .test-field--error .test-field__selection .atc-field__input {
border-color: #ff4436;
color: #ff4436;
}
/* // FLOATING LABEL */
.test-field--floating .test-field-input {
position: relative;
margin-bottom: 1.5rem;
}
.test-field__label {
position: absolute;
top: 0;
padding: 7px 0 0 13px;
transition: all 200ms;
opacity: 0.5;
}
.test-field__input:focus + .test-field__label,
.test-field--floating .test-field__input:valid + .test-field__label {
font-size: 75%;
transform: translate3d(0, -100%, 0);
opacity: 1;
}
<div class="test-field test-field--floating">
<label for="a" class="test-field__label">Input label</label>
<input class="test-field__input" id="b" type="text" value="" required>
</div>
This seemed to be a good way to replicate floating labels, with a similar DOM structure:
const floatField = document.querySelectorAll('.ej-form-field-input-textbox');
const floatContainer = document.querySelectorAll('.ej-form-field');
for (let i = 0; i < floatField.length; i++) {
floatField[i].addEventListener('focus', () => {
floatContainer[i].classList.add('active');
});
};
for (let i = 0; i < floatField.length; i++) {
floatField[i].addEventListener('blur', () => {
floatContainer[i].classList.remove('active');
});
};
.ej-form-field {
border: solid 1px #ccc;
padding: 0 8px;
position: relative;
}
.ej-form-field input {
border: 1px solid red;
font-size: 16px;
outline: 0;
height: 30px;
/* padding: 16px 0 10px; */
}
label {
font-size: 16px;
position: absolute;
transform-origin: top left;
transform: translate(0, 16px) scale(1);
transition: all .1s ease-in-out;
height: 40px;
}
.ej-form-field.active label {
transform: translate(0, -6px) scale(.95);
color: red;
margin-left: 10px;
background-color: #fff;
border: 1px solid green;
height: 20px;
}
<div id="floatContainer" class="ej-form-field">
<label for="floatField">First name</label>
<div>
<input id="floatField" class="ej-form-field-input-textbox" type="text">
</div>
</div>
<div id="floatContainer" class="ej-form-field">
<label for="floatField">Last name</label>
<div>
<input id="floatField" class="ej-form-field-input-textbox" type="text">
</div>
</div>

Detect a clik and start a "function" on CSS

Now I have a code that makes a button not visible, and show it in x seconds, my idea was to start this timer, when a video is clicked, I don't need, that if they stop the video, the timer stops, but if it is possible to do it, it would be helpful. I can give, to this video, a name for the ID, and a name for the class, I already did that with the button, and give the name "button".
The code that I alredy have:
body.page-id-2090 #button{
visibility: hidden;
}
#button {
animation: show 0s both;
animation-delay: 30s;
}
#keyframes show {
from {
visibility: hidden;
}
to {
visibility: visible;
}
}
There is a few examples to try:
CSS selectors:
#element_id:active
example:
.button {
width: 50px;
height: 20px;
padding: 10px 20px;
text-align: center;
box-shadow: 1px 1px 1px #000;
color: red;
}
.button:active {
margin: 1px 1px 0;
box-shadow: -1px -1px 1px #000;
background-color: red;
color: white;
}
#btnClick {
display: block;
visibility: hidden;
}
<input type="checkbox" id="btnClick" />
<label class="button" for="btnClick">Check onClick CSS effect</label>
#element_id:target
example:
#elementToShow {
display: none;
padding: 20px;
}
#elementToShow:target {
display: block;
}
.element {
color: red;
font-size: 2rem;
}
Click to show element by CSS selector
<div class="element" id="elementToShow">Element visible!</div>

How to add an image to the back of a custom checkbox

I found a really clean looking custom checkbox on codepen and just updated a project to use it. Right now I've got the background colors different for the different sections of my form, but I'd like to just make background an icon rather than just a solid color
The commented out stuff doesn't work
.label-switch {
display: inline-block;
position: relative;
}
.label-switch::before,
.label-switch::after {
content: "";
display: inline-block;
cursor: pointer;
transition: all 0.5s;
}
.label-switch::before {
width: 3em;
height: 1em;
border: 1px solid #757575;
border-radius: 4em;
background: #888888;
}
.label-switch::after {
position: absolute;
left: 0;
top: -20%;
width: 1.5em;
height: 1.5em;
border: 1px solid #757575;
border-radius: 4em;
background: #ffffff;
}
.input-switch:checked~.label-switch::before {
background: #00a900;
border-color: #008e00;
}
.input-switch:checked~.label-switch::after {
left: unset;
right: 0;
background: #00ce00;
/*background-image:(url(http://pngimg.com/uploads/pokeball/pokeball_PNG8.png);*/
/*background: url(http://pngimg.com/uploads/pokeball/pokeball_PNG8.png);*/
border-color: #009a00;
}
<input class='input-switch' type="checkbox" id="demo" />
<label class="label-switch" for="demo"></label>
<span class="info-text"></span>
Everything you did was correct it's just the background image needed to have a background-position and background-size set if you toggle the checkbox in my snippet you should see the background pattern as you intended it to work.
.label-switch{
display: inline-block;
position: relative;
}
.label-switch::before, .label-switch::after{
content: "";
display: inline-block;
cursor: pointer;
transition: all 0.5s;
}
.label-switch::before {
width: 3em;
height: 1em;
border: 1px solid #757575;
border-radius: 4em;
background: #888888;
}
.label-switch::after {
position: absolute;
left: 0;
top: -20%;
width: 1.5em;
height: 1.5em;
border: 1px solid #757575;
border-radius: 4em;
background: #ffffff;
}
.input-switch:checked ~ .label-switch::before {
background: #00a900;
border-color: #008e00;
}
.input-switch:checked ~ .label-switch::after {
left: unset;
right: 0;
background: #00ce00;
background-image:url(http://pngimg.com/uploads/pokeball/pokeball_PNG8.png);
background-size:contain;
background-position:center;
border-color: #009a00;
}
<input class='input-switch' type="checkbox" id="demo"/>
<label class="label-switch" for="demo"></label>
<span class="info-text"></span>

AngularJS ng-class after :active or :focus

I have a form I am trying to create using CSS to achieve a halfway "material" feel. I achieved a lot of the animations by ditching the stock bootstrap forms entirely and rewriting them to get the maximum amount of flexibility.
EDIT: Forgot to link Codepen - http://codepen.io/h3xc0ntr0l/pen/ONwdLm
Essentially, the "form groups" are set to position:relative and used to position the input and a couple pseudo elements absolutely, yada yada. I am using angular and am not able to incorporate jquery into the project.
my problem is as follows
<div class="-form-group">
<input type="email" class="-form-input" ng-class='{"content":"user.email"}' ng-model="user.email"/>
<span class="in-email"></span>
</div>
this does not have the behavior i need. I guess if I have "ng-model", then whatever is in there is going to evaluate to true.
I also tried the following with scss:
.-form-input{
border: none;
box-shadow: none;
border-radius: 0;
font-size: 1rem;
background: none;
outline: none;
width: 100%;
margin-top: 2em;
position: relative;
z-index:4;
&:active, &:focus{
.in-email &{ //
&:before{
top: -50%;
color: transparent;
border-bottom: transparent;
}
}
}
}
the effects are achieved via
.in-email, .in-pwd{
&:before{
position: absolute;
top: 50%;
left: 0%;
width: 100%;
z-index:3;
color: black;
transition: 500ms ease all;
border-bottom: 1.5px solid rgba(128,128,128, .5);
}
}
I can not think of anything else that would prevent the span from coming back down after i leave the hover regardless of whether or not there is any content. does anyone have any tips?
(function(){
angular.module('app').controller([
'$scope', function($scope){
$scope.user = undefined;
}
]);
})();
form {
width: 100%;
padding: 1em;
border: .25px solid #e9e9e9;
box-shadow: 0 1rem 3rem 0 rgba(0, 0, 0, 0.33);
height: 20rem;
margin: 0 auto;
}
form .-form-group {
position: relative;
width: 100%;
height: auto;
transition: 500ms ease;
margin: 0 1em 2em 0;
}
form .-form-group .-form-input {
border: none;
box-shadow: none;
border-radius: 0;
font-size: 1rem;
background: none;
outline: none;
width: 100%;
margin-top: 2em;
position: relative;
z-index: 4;
}
.in-email form .-form-group .-form-input:active:before, .in-email form .-form-group .-form-input:focus:before {
top: -50%;
color: transparent;
border-bottom: transparent;
}
form .-form-group input:-webkit-autofill {
background-color: #e9e9e9 !important;
}
form .-form-group:after {
content: "";
display: block;
transition: 250ms 500ms ease all;
border-bottom: 1.5px solid #288690;
width: 0;
}
form .-form-group:hover:after, form .-form-group:focus:after, form .-form-group:active:after {
content: "";
width: 100%;
top: 90%;
}
form .-form-group:hover .in-email:before, form .-form-group:hover .in-pwd:before, form .-form-group:focus .in-email:before, form .-form-group:focus .in-pwd:before, form .-form-group:active .in-email:before, form .-form-group:active .in-pwd:before {
top: -50%;
color: transparent;
border-bottom: transparent;
}
form .button {
border-radius: 1px;
border: none;
}
form .in-email:before, form .in-pwd:before {
position: absolute;
top: 50%;
left: 0%;
width: 100%;
z-index: 3;
color: black;
transition: 500ms ease all;
border-bottom: 1.5px solid rgba(128, 128, 128, 0.5);
}
.content:before {
content: "" !important;
}
.in-email:before {
content: "Email Address";
}
.in-pwd:before {
content: "Password";
}
.nav, .pagination, .carousel, .panel-title a {
cursor: pointer;
}
#ui-root {
margin-top: 5rem;
padding: 1rem;
}
.ui-container {
position: relative;
width: 100%;
padding: 0 10% 0 10%;
}
.button {
background-color: #288690 !important;
}
<link rel="stylesheet" href="http://getbootstrap.com/dist/css/bootstrap.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<div class="container">
<div class="ui-container">
<div class="row" id="ui-root">
<div class="col-sm-12 col-md-4 col-md-offset-4">
<form autocomplete="off" class="text-center">
<!-- chrome autcomplete -->
<div class="-form-group">
<input type="email" class="-form-input" ng-class='{"content": "user.email"}' ng-model="user.email"/>
<span class="in-email"></span>
</div>
<div class="-form-group">
<input type="password" class="-form-input" ng-class='{"content": "user.password"}' ng-model="user.password" />
<span ng-class='{"content": "user.password"}' class="in-pwd"></span>
</div>
<button class="btn btn-primary button">Login</button>
</form>
</div>
</div>
</div>
</div>

Resources