Do you know how I could style a checkbox when it is disabled?
E.g.:
<input type="checkbox" value="All Terrain Vehicle"
name="exfilter_All Terrain Vehicle"
id="exfilter_All_Terrain_Vehicle"
class="exfilter" disabled="">
Use the attribute selector in the css
input[disabled]{
outline:1px solid red; // or whatever
}
for checkbox exclusively use
input[type=checkbox][disabled]{
outline:1px solid red; // or whatever
}
$('button').click(function() {
const i = $('input');
if (i.is('[disabled]'))
i.attr('disabled', false)
else
i.attr('disabled', true);
})
input[type=checkbox][disabled] {
outline: 2px solid red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" value="tasd" disabled />
<input type="text" value="text" disabled />
<button>disable/enable</button>
You can't style a disabled checkbox directly because it's controlled by the browser / OS.
However you can be clever and replace the checkbox with a label that simulates a checkbox using pure CSS. You need to have an adjacent label that you can use to style a new "pseudo checkbox". Essentially you're completely redrawing the thing but it gives you complete control over how it looks in any state.
I've thrown up a basic example so that you can see it in action: http://jsfiddle.net/JohnSReid/pr9Lx5th/3/
Here's the sample:
input[type="checkbox"] {
display: none;
}
label:before {
background: linear-gradient(to bottom, #fff 0px, #e6e6e6 100%) repeat scroll 0 0 rgba(0, 0, 0, 0);
border: 1px solid #035f8f;
height: 36px;
width: 36px;
display: block;
cursor: pointer;
}
input[type="checkbox"] + label:before {
content: '';
background: linear-gradient(to bottom, #e6e6e6 0px, #fff 100%) repeat scroll 0 0 rgba(0, 0, 0, 0);
border-color: #3d9000;
color: #96be0a;
font-size: 38px;
line-height: 35px;
text-align: center;
}
input[type="checkbox"]:disabled + label:before {
border-color: #eee;
color: #ccc;
background: linear-gradient(to top, #e6e6e6 0px, #fff 100%) repeat scroll 0 0 rgba(0, 0, 0, 0);
}
input[type="checkbox"]:checked + label:before {
content: '✓';
}
<div><input id="cb1" type="checkbox" disabled checked /><label for="cb1"></label></div>
<div><input id="cb2" type="checkbox" disabled /><label for="cb2"></label></div>
<div><input id="cb3" type="checkbox" checked /><label for="cb3"></label></div>
<div><input id="cb4" type="checkbox" /><label for="cb4"></label></div>
Depending on your level of browser compatibility and accessibility, some additional tweaks will need to be made.
Use the :disabled CSS3 pseudo-selector
You can select it using css like this:
input[disabled] { /* css attributes */ }
Checkboxes (radio buttons and <select>) are OS-level components, not browser-level. You cannot reliably style them in a manner that will be consistent across browsers and operating systems.
Your best bet it to put an overlay on top and style that instead.
Use CSS's :disabled selector (for CSS3):
checkbox-style { }
checkbox-style:disabled { }
Or you need to use javascript to alter the style based on when you enable/disable it (Assuming it is being enabled/disabled based on your question).
input[type='checkbox'][disabled][checked] {
width:0px; height:0px;
}
input[type='checkbox'][disabled][checked]:after {
content:'\e013'; position:absolute;
margin-top:-10px;
opacity: 1 !important;
margin-left:-5px;
font-family: 'Glyphicons Halflings';
font-style: normal;
font-weight: normal;
}
If you're trying to stop someone from updating the checkbox so it appears disabled then just use JQuery
$('input[type=checkbox]').click(false);
You can then style the checkbox.
do not put disabled in the input and apply the following styles
input[type="checkbox"] {
pointer-events: none;
}
This is supported by IE too:
HTML
class="disabled"
CSS
.disabled{
...
}
In case if you really want to add some colors to a checkbox, try this workaround.
input[type=checkbox][disabled] {
outline: 5px solid red;
outline-offset: -20px;
}
Related
I want to make a website more accessible by considering user with high contrast mode (or maybe just dark mode).
Now I have a problem with custom radio buttons which are just HTML elements that have a round border: when the user has set the default background color of his browser to black, the background-color of my elements is also black, making it look like a checked option, even though I set the background-color of this elements to white.
To prevent this I made the white border thicker, so it looks like a white background. But it also creates the problem, that sometimes there is a tiny black dot in the middle of my buttons at some zoom-levels or resolutions.
Is there any way to prevent the browser from overwriting the background-color I declared in my CSS?
Is there anything I missed out on like an attribute that prevents this behaviour?
I could also replace this elements or implement them in a different way, but at this point i am just curious if there isn't any simple solution to that.
The style of my radio buttons looks like this:
input.check + label:before {
display: inline-block;
content: "";
width: 1rem;
height: 1rem;
background: #fefefe;
border: 0.2rem solid #fefefe;
border-radius: 50%;
position: absolute;
left: -1rem;
}
input.check:checked + label:before {
background: #000000;
}
Here is a link to a picture of my radio when checked / or default black background-color
I make the 'checked' look of my radio button by setting the background-color to black - like the browser does in this case.
Edit
So far there are good answers for improving the accessability of custom radio buttons.
To be a little bit more specific on my problem:
I tested the page in Firefox with the following color settings
But this seems to overwrite every background-color no matter what you specified for each element. Is there a way to make an exception for this overwriting?
Using a pseudo element - which is already mooted in the question - it is possible to style the button exactly as you would like.
The main difference in this snippet and the CSS in the question is that we set the actual input element to opacity 0. This means it is still 'there' for accessibility purposes but it can't be actually seen.
Also, using radial-gradient for coloring the buttons we can have rings of different colors, or just one color as best suits the background chosen. As the code in the question does, we also size everything in terms of rems so that the user who has changed their browser setting for this gets the benefit.
body {
background-color: black;
}
input {
opacity: 0;/* keep the input element in the DOM for accessibility it's just not seen*/
}
/* a pseudo element - this is what the user actually sees and we can style it */
label > input[type="radio"] + *::before {
content: "";
display: inline-block;
width: 2rem;
height: 2rem;
margin-right: 1rem;
border-radius: 50%;
border-style: solid;
border-width: 0.2rem;
border-color: white;
background: radial-gradient(white 0%, white 100%);/* choose what you want - one color or rings */
border-color: white;
}
label > input[type="radio"]:checked + *::before {
background: radial-gradient(red 0%, red 50%, white 50%, white 100%);/* choose what you want here too */
border-color: red;
}
label {
font-size: 2rem;
color: white;
}
<label>
<input type="radio" name="mybutton" value="A" checked />
<span>A</span>
</label>
<label>
<input type="radio" name="mybutton" value="B" />
<span>B</span>
</label>
<label>
<input type="radio" name="mybutton" value="C" />
<span>C</span>
</label>
In order to remove the default browser styling for the radio buttons you need appearance: none;
You will find more on that here, and you can check browser support here.
From there, you can use the background property to create the checked effect, you don't even need to use ::before.
I created this fiddle to demonstrate.
The code looks like this:
:root {
--background-color: white;
--text-color: black;
--fill-color: black;
--border-color: black;
}
#media (prefers-color-scheme: dark) {
:root {
--background-color: black;
--text-color: white;
--fill-color: white;
--border-color: white;
}
}
body {
background: var(--background-color);
color: var(--text-color);
}
div {
display: flex;
align-items: center;
}
input[type=radio] {
appearance: none;
margin: 0 0.5rem 0 0;
border: 1px solid var(--border-color);
border-radius: 50%;
width: 1rem;
height: 1rem;
}
input[type=radio]:checked {
background: radial-gradient(var(--fill-color) 0%, var(--fill-color) 30%, var(--background-color) 31%);
}
<p>Select an option:</p>
<div>
<input class="check" type="radio" id="huey" name="duck" value="huey" checked>
<label for="huey">Huey</label>
</div>
<div>
<input class="check" type="radio" id="dewey" name="duck" value="dewey">
<label for="dewey">Dewey</label>
</div>
<div>
<input class="check" type="radio" id="louie" name="duck" value="louie">
<label for="louie">Louie</label>
</div>
I would like to make the input of the checkbox colored, but it wont allow me. It needs to be disabled, because I don't want it to be clickable.
https://jsfiddle.net/x7ujvm4f/
<p>
<label class="disabled-label"><input type="checkbox" class="disabled-checkbox" checked="checked" disabled="disabled"/>Disabled Checkbox</label>
</p>
Here is what I did:
.disabled-label:after {
content: " ";
background-color: #00A0D1;
display: inline-block;
visibility: visible;
}
.disabled-label:checked:after {
content: '✔';
box-shadow: 0px 2px 4px rgba(155, 155, 155, 0.15);
border-radius: 3px;
height: 13px;
display: block;
width: 13px;
text-align: center;
font-size: 9px;
color: white;
}
As per W3C we cant modifies radio or checkbox color or shape. so, in that case, we need to design your own control which looks and feel like checkbox or radio button.
its very simple please refer this like.
https://www.w3schools.com/howto/howto_css_custom_checkbox.asp
Hi there I try to make it possible that you first need to click on the element and after that it should chang the background everytime you hover over it. Important is that No JavaScript or JQuery should be used. It's not a link so :visited and href is not an option
*Pseudocode*
*if* .lst-c:active *than* .lst-c:hover {
background: blue;
border-radius: 20px 20px 0px 0px !important;
}
You can try the Checkbox Hack
http://timpietrusky.com/advanced-checkbox-hack
input[type=checkbox] {
position: absolute;
top: -9999px;
left: -9999px;
}
div {
background: green;
}
input[type=checkbox]:checked ~ div:hover {
background: red;
}
<label for="toggle-1">Do Something</label>
<input type="checkbox" id="toggle-1">
<div>Control me</div>
Checkbox
/* Hide */
.is { display: none }
/* Is checked */
.is:checked + label .content:hover { background: blue; }
/* Not checked */
.content:hover { background: red; }
<input type="checkbox" class="is" id="clicker">
<label for="clicker">
<div class="content">Hello</div>
</label>
This question already has answers here:
Styling input radio with css [duplicate]
(5 answers)
Closed 4 years ago.
I have a website where I'm trying to change the background color of the dot of a radio button. Right now it seems to be transparent so it gets the color of whatever the background is. I tried using CSS and setting "background: white;" for example, however this has no effect in the browser. Any ideas of cool tricks to use to achieve this?
Same question stands for checkbox as well.
jsBin demo
This technique uses the label element bound to hidden input elements, that receiving a :checked state will change the apperance of the :before pseudo element:
/* COMMON RADIO AND CHECKBOX STYLES */
input[type=radio],
input[type=checkbox]{
/* Hide original inputs */
visibility: hidden;
position: absolute;
}
input[type=radio] + label:before,
input[type=checkbox] + label:before{
height:12px;
width:12px;
margin-right: 2px;
content: " ";
display:inline-block;
vertical-align: baseline;
border:1px solid #777;
}
input[type=radio]:checked + label:before,
input[type=checkbox]:checked + label:before{
background:gold;
}
/* CUSTOM RADIO AND CHECKBOX STYLES */
input[type=radio] + label:before{
border-radius:50%;
}
input[type=checkbox] + label:before{
border-radius:2px;
}
<input type="radio" name="r" id="r1"><label for="r1">Radio 1</label>
<input type="radio" name="r" id="r2"><label for="r2">Radio 2</label>
<input type="checkbox" name="c1" id="c1"><label for="c1">Check 1</label>
<input type="checkbox" name="c2" id="c2"><label for="c2">check 2</label>
It's been well stablished that you cannot change every detail on browser generated controls. For example the color of the arrow on a select dropdown, or the dot of a radio, etc...
You can create your custom controls, use some library like JQuery UI, or.... maybe play around a little with css.
Here's an experiment to fake a colored dot on a radio, using :before pseudo element:
http://jsfiddle.net/bvtngh57/
input[type="radio"]:checked:before {
content: "";
display: block;
position: relative;
top: 3px;
left: 3px;
width: 6px;
height: 6px;
border-radius: 50%;
background: red;
}
Result:
The preferred method for styling the non-label elements of checkboxes and radio buttons with CSS is to essentially replace them with images that represent their current state (unchecked, checked, etc).
See this article by Ryan Seddon: http://www.thecssninja.com/css/custom-inputs-using-css
The browser itself handles the look of radio buttons and checkboxes, as well as dropdown/selects. You can however hide the radio buttons, replace them with images, and then modify your radio/check value using jQuery. Font Awesome (http://fortawesome.github.io/Font-Awesome/icons/) has some cool icons that you can use for this.
Here is a demo
<div>
Radio 1 -
<input type="radio" name="radio" class="radio" value="1" />
<span class="red fa fa-circle-o"></span>
</div>
<div>
Radio 2 -
<input type="radio" name="radio" class="radio" value="2" />
<span class="blue fa fa-circle-o"></span>
</div>
$('span.fa').on('click', function() {
$('span.fa').removeClass('fa fa-dot-circle-o').addClass('fa fa-circle-o');
$(this).removeClass('fa-circle-o').addClass('fa-dot-circle-o');
//Check corresponding hidden radio
$(this).prev('input.radio').prop('checked', true);
});
There are many ways to do this, all of them involve to get rid of the DOM styles since it's impossible to do it without these "tricks". Here's a sample by Chris Coyier (just read the page, skip step 1 and simply prepare teh images and CSS)
/*
Hide the original radios and checkboxes
(but still accessible)
:not(#foo) > is a rule filter to block browsers
that don't support that selector from
applying rules they shouldn't
*/
li:not(#foo) > fieldset > div > span > input[type='radio'],
li:not(#foo) > fieldset > div > span > input[type='checkbox'] {
/* Hide the input, but have it still be clickable */
opacity: 0;
float: left;
width: 18px;
}
li:not(#foo) > fieldset > div > span > input[type='radio'] + label,
li:not(#foo) > fieldset > div > span > input[type='checkbox'] + label {
margin: 0;
clear: none;
/* Left padding makes room for image */
padding: 5px 0 4px 24px;
/* Make look clickable because they are */
cursor: pointer;
background: url(off.png) left center no-repeat;
}
/*
Change from unchecked to checked graphic
*/
li:not(#foo) > fieldset > div > span > input[type='radio']:checked + label {
background-image: url(radio.png);
}
li:not(#foo) > fieldset > div > span > input[type='checkbox']:checked + label {
background-image: url(check.png);
}
You can style ionic radio buttons using css alone. Check the fiddle:
https://jsfiddle.net/sreekanthjayan/0d9vj86k/
<div>
<ion-radio class="radio radio-inline radio-gray" ng-model="choice" ng-value="'A'">iOS</ion-radio>
<ion-radio class="radio radio-inline radio-teal" ng-model="choice" ng-value="'B'">Android</ion-radio>
<ion-radio class="radio radio-inline radio-blue" ng-model="choice" ng-value="'C'">Windows Phone</ion-radio>
</div>
.radio .radio-icon {
visibility: visible !important;
}
.radio .radio-icon:before {
content: "" !important;
border: 2px solid black !important;
width: 24px !important;
height: 24px !important;
border-radius: 50% !important;
overflow: hidden !important;
}
.radio .radio-icon:after {
content: "" !important;
position: absolute !important;
right: 20px !important;
top: 22px !important;
background: black !important;
width: 12px !important;
height: 12px !important;
border-radius: 50% !important;
overflow: hidden !important;
transition: -webkit-transform .28s cubic-bezier(0.420, 0.000, 0.000, 1.650);
transition: transform .28s cubic-bezier(0.420, 0.000, 0.000, 1.650);
-webkit-transform: scale(0);
transform: scale(0);
}
.radio.item-radio > input[type=radio]:checked ~ .radio-icon:after {
-webkit-transform: scale(1);
transform: scale(1);
}
.radio .item-content {
background-color: #fff;
margin: 0;
padding-right: 50px;
padding-left: 0px;
}
.radio.item-radio > input[type=radio]:checked ~ .item-content {
background-color: #fff;
}
.radio-inline.item {
display: inline-block;
border: none;
margin: 0;
height: 50px;
}
.radio-blue .radio-icon:after {
background: #2196F3 !important;
}
.radio-blue .radio-icon:before {
border-color: #2196F3 !important;
}
.radio-teal .radio-icon:after {
background: #009688 !important;
}
.radio-teal .radio-icon:before {
border-color: #009688 !important;
}
.radio-gray .radio-icon:after {
background: #B6B6B6 !important;
}
.radio-gray .radio-icon:before {
border-color: #B6B6B6 !important;
}
So, I've been stuck at this for a couple of hours. I'm essentially trying to get a checkbox to work as a toggle button. I want the styles applied by jquery to be only applied when it's checked and back to it's initial if it has been deselected.
The HTML markup:
<form class="simple_form new_mailing_list_form" data-remote="true" id="new_mailing_list_form" method="post">
<div class="input boolean optional mailing_list_form_opt_in">
<input name="mailing_list_form[opt_in]" type="hidden" value="0">
<label class="boolean optional control-label checkbox toggle-button" for="mailing_list_form_opt_in">
<input checked="checked" class="boolean optional" id="mailing_list_form_opt_in" name="mailing_list_form[opt_in]" type="checkbox" value="1">
Yes, I would like to join the mailing list.
</label>
</div>
The SCSS:
#new_mailing_list_form {
.opt {
color: $white;
background-color: $selectiveYellow !important;
border: 2px solid $selectiveYellow !important;
}
.checkbox {
margin: 0px;
padding: 0px;
}
div label input {
margin-right:100px;
}
.mailing_list_form_opt_in label {
cursor: pointer;
background: transparent;
border: 2px solid $selectiveYellow;
border-radius:2px;
font-size: 15px;
font-weight: normal;
line-height: 1.4;
overflow:auto;
margin:4px;
padding: 8px 15px;
width: auto;
&:hover {
background-color: $sunglow;
border: 2px solid $sunglow;
color: $white;
}
}
.mailing_list_form_opt_in label {
display:block;
}
.mailing_list_form_opt_in label input {
display: none;
}
.mailing_list_form_opt_in input:checked {
background-color:$selectiveYellow;
color:$white;
}
}
JQuery:
$('#mailing_list_form_opt_in').change(function () {
$(this).parent().css({ 'background-color':'#ffbb00','border':'2px solid #ffbb00', 'color':'#fff' });
});
I've tried using a conditional statement as well, but I start to descend into spaghetti JQuery which doesn't even work.
Work on it so far: Working CodePen link
You could use jQuery's toggleClass() method to change the background whenever a user clicks the element.
$("#checkbox_elem").on( "click", function(){
$(this).toggleClass( 'background-class' );
});
Now all you have to do is have a default style on the element, and place the new CSS rules into the background-class class definition. Clicking the element will toggle the class on the element.
You could use an explicit check on the element if you want to add some more functionality:
$("#checkbox_elem").on( "click", function(){
if ( $(this).is(':checked') ){
// the checkbox is marked as "checked"
// here you can manipulate the style accordingly
}else{
// the checkbox is NOT marked as "checked"
// here you can manipulate the style accordingly
}
});
So, I'm sharing my pure HTML5/CSS3 solution (which doesn't use any JS/JQuery!) to this problem so that it could be helpful for others stuck on something similar.
I refactored my markup as follows,
HTML:
<input id="mailing_list_form_opt_in" name="mailing_list_form[opt_in]" type="checkbox" value="1">
<label for="mailing_list_form_opt_in">Yes, I would like to join the mailing list.</label>
and for the styles, I used the adjacent selector + & the pseudo class :checked to show the behavior on that state. The corresponding styles for that are as follows,
SCSS:
input[type=checkbox] + label {
background: transparent;
border: 2px solid $selectiveYellow;
border-radius: 2px;
-webkit-font-smoothing: subpixel-antialiased;
font-size: 15px;
font-weight: normal;
line-height: 1.4;
overflow: auto;
margin: 4px;
padding: 8px 15px;
#include transition( 0.25s linear);
width: auto;
&:hover {
background-color: $sunglow;
border: 2px solid $sunglow;
color: $white;
}
}
input[type=checkbox]:checked + label {
background: $selectiveYellow !important;
border: 2px solid $selectiveYellow !important;
color: $white;
}
input[type=checkbox] {
display: none;
}
Works perfectly, added a Codepen so that you can check that out as well! Hope this helps others! :D