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;
}
Related
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>
i have included some checkboxes within my html page so the user can check and uncheck them, i also wrote the style to highlight the checkbox borders and also the label but still nothing work.
can do with a bit of help.
<input type="checkbox" name="createartwork" id="createartwork">
<label class="label-for-check" for="createartwork">I want your Designer to create my artwork</label>
input[type=checkbox]:checked + label::after{
background-color: #FD6418;
}
input:checked {
height: 50px;
width: 50px;
}
<input type="checkbox" name="createartwork" id="createartwork">
<label class="label-for-check" for="createartwork">I want you to create my artwork</label>
:checked + label {
background-color: #FD6418;
font-weight: bold;
}
Try this.
You're a bit off track, you cannot style the original <input> tag, you can fake a new checkbox with your label, so using the code you provided:
HTML:
<input type="checkbox" name="createartwork" id="createartwork">
<label class="label-for-check" for="createartwork">
I want you to create my artwork</label>
CSS:
input[type=checkbox]
+ label::before {
content: '';
width: 5px;
height: 5px;
background-color: #FF0;
margin-right: 5px;
padding: 0px 5px;
}
input[type=checkbox]:checked + label::before{
content: 'x';
background-color: #FF0;
margin-right: 5px;
padding: 0px 5px;
}
input[type=checkbox] {
display: none;
}
And of course, a working example
I wanted to use custom checkboxes on a website so for starters, I worked with a span that I gave a custom background-color and once checked, I changed the background-color in CSS and this worked fine, the background-color changed when the checkbox was clicked on. However, once I replaced the color values by an image background url, it shows the background image in the initial state but it won't change the background-image after the value is checked. The checkbox flashes for a brief time but then continues to have the original background image.
The URL is correct, I can open the image in my browser. I've tried by giving the label rather than the span inside it the background properties but this has the same result. In the inspector in Google Chrome, the new background property is also 'active' (and the old one 'crossed out') so I just can't understand why it continues to show the old one.
The HTML:
<input type="checkbox" id="unlimited" name="unlimited" value="unlimited" />
<label for="unlimited"><span></span>Unlimited amount</label>
CSS:
input[type="checkbox"] {
display:none;
}
input[type="checkbox"] + label span {
display:inline-block;
width:30px;
height:30px;
margin:-1px 4px 0 0;
vertical-align:middle;
background: url("../../static/img/checkbox1.jpg");
cursor:pointer;
}
input[type="checkbox"]:checked + label span {
background: url("../../static/img/checkbox2.jpg");
}
Edit: Fixed typo. Here is the code with just background colors, which works as you'd expect, once clicked the color changes to green, you click again it changes to blue.
CSS:
input[type="checkbox"] {
display:none;
}
input[type="checkbox"] + label span {
display:inline-block;
width:30px;
height:30px;
margin:-1px 4px 0 0;
vertical-align:middle;
background: blue;
cursor:pointer;
}
input[type="checkbox"]:checked + label span {
background: green;
}
You have a typo
.input[type="checkbox"] + label span
See the full-stop/period in front of input?
input[type="checkbox"] {
display: none;
}
input[type="checkbox"] + label>span {
display: inline-block;
width: 30px;
height: 30px;
margin: -1px 4px 0 0;
vertical-align: middle;
background-image: url(http://lorempixel.com/output/abstract-q-c-30-30-4.jpg);
cursor: pointer;
}
input[type="checkbox"]:checked + label span {
background: url(http://lorempixel.com/output/people-q-c-30-30-3.jpg);
}
<input type="checkbox" id="unlimited" name="unlimited" value="unlimited" />
<label for="unlimited"><span></span>Unlimited amount</label>
The text in your question sounds like you're accidentally double clicking. But a checkbox doesn't have a double click event, so you're simply clicking twice, unselecting the checkbox the second time.
However, the code in your question shows an error in the CSS for the unchecked one, namely a extraneous dot in front of the input. If I remove that, it works normally.
(Note that I needed to replace the images by something generic.)
input[type="checkbox"] {
display: none;
}
input[type="checkbox"] + label span {
display: inline-block;
width: 30px;
height: 30px;
margin: -1px 4px 0 0;
vertical-align: middle;
background: url("http://dummyimage.com/30/FF0/000.png&text=%20");
cursor: pointer;
}
input[type="checkbox"]:checked + label span {
background: url("http://dummyimage.com/30/FF0/000.png&text=✔");
}
<input type="checkbox" id="unlimited" name="unlimited" value="unlimited" />
<label for="unlimited"><span></span>Unlimited amount</label>
I have the following mark-up using the bootstrap framework.
<div class="col-md-4">
<div class="custom-container">
<img class="center-block img-responsive img-circle invite-contact-trybe" src="{$img}" alt="Contact Image">
<input class="invite-contact-checkbox" type="checkbox">
</div>
</div>
I would like to achieve the following:
Is there anyway to do this using CSS?
I would obviously need 3 states:
Initial:
.custom-container input[type=checkbox]{}
Some form of hover state:
.custom-container input[type=checkbox]:hover{}
When it is checked:
.custom-container input[type=checkbox]:checked{}
Can anyone suggest a solution?
Background image checkbox replacement
Let's create this
This is a very simple example using a :before pseudo element as well as :checked and :hover states.
With this clean HTML
<input type="checkbox" id="inputOne" />
<label for="inputOne"></label>
Note the matching for and id attributes, this attaches the label to the checkbox. Also, the order of elements is very important; the label must come after the input so we can style with input:checked
As well as this Basic CSS
The checkbox is hidden with display: none and all interaction is done with the label
The :after pseudo element is given a unicode tick (\2714) but this could also be ticked with a background image.
The jagged edge caused by the border-radius can be softened by a matching color box-shadow. The inside edge of the border looks fine when the background image is not a solid block of color.
The transition: all 0.4s creates a smooth fade in / out for the border.
I have added more guidance in CSS comments.
Complete Example
input[type=checkbox] {
display: none;
}
/*
- Style each label that is directly after the input
- position: relative; will ensure that any position: absolute children will position themselves in relation to it
*/
input[type=checkbox] + label {
position: relative;
background: url(http://i.stack.imgur.com/ocgp1.jpg) no-repeat;
height: 50px;
width: 50px;
display: block;
border-radius: 50%;
transition: box-shadow 0.4s, border 0.4s;
border: solid 5px #FFF;
box-shadow: 0 0 1px #FFF;/* Soften the jagged edge */
cursor: pointer;
}
/* Provide a border when hovered and when the checkbox before it is checked */
input[type=checkbox] + label:hover,
input[type=checkbox]:checked + label {
border: solid 5px #F00;
box-shadow: 0 0 1px #F00;
/* Soften the jagged edge */
}
/*
- Create a pseudo element :after when checked and provide a tick
- Center the content
*/
input[type=checkbox]:checked + label:after {
content: '\2714';
/*content is required, though it can be empty - content: '';*/
height: 1em;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
margin: auto;
color: #F00;
line-height: 1;
font-size: 18px;
text-align: center;
}
<input type="checkbox" id="inputOne" />
<label for="inputOne"></label>
The standard checkboxes rendered in most browsers are quite small and don’t increase in size even when a larger font is used. What is the best, browser-independent way to display larger checkboxes?
In case this can help anyone, here's simple CSS as a jumping off point. Turns it into a basic rounded square big enough for thumbs with a toggled background color.
input[type='checkbox'] {
-webkit-appearance:none;
width:30px;
height:30px;
background:white;
border-radius:5px;
border:2px solid #555;
}
input[type='checkbox']:checked {
background: #abd;
}
<input type="checkbox" />
Actually there is a way to make them bigger, checkboxes just like anything else (even an iframe like a facebook button).
Wrap them in a "zoomed" element:
.double {
zoom: 2;
transform: scale(2);
-ms-transform: scale(2);
-webkit-transform: scale(2);
-o-transform: scale(2);
-moz-transform: scale(2);
transform-origin: 0 0;
-ms-transform-origin: 0 0;
-webkit-transform-origin: 0 0;
-o-transform-origin: 0 0;
-moz-transform-origin: 0 0;
}
<div class="double">
<input type="checkbox" name="hello" value="1">
</div>
It might look a little bit "rescaled" but it works.
Of course you can make that div float:left and put your label besides it, float:left too.
Try this CSS
input[type=checkbox] {width:100px; height:100px;}
<input type="checkbox" />
I tried changing the padding and margin and well as the width and height, and then finally found that if you just increase the scale it'll work:
input[type=checkbox] {
transform: scale(1.5);
}
Pure modern 2020 CSS only decision, without blurry scaling or non-handy transforming. And with tick! =)
Works nice in Firefox and Chromium-based browsers.
So, you can rule your checkboxes purely ADAPTIVE, just by setting parent block's font-size and it will grow with text!
input[type='checkbox'] {
-moz-appearance: none;
-webkit-appearance: none;
appearance: none;
vertical-align: middle;
outline: none;
font-size: inherit;
cursor: pointer;
width: 1.0em;
height: 1.0em;
background: white;
border-radius: 0.25em;
border: 0.125em solid #555;
position: relative;
}
input[type='checkbox']:checked {
background: #adf;
}
input[type='checkbox']:checked:after {
content: "✔";
position: absolute;
font-size: 90%;
left: 0.0625em;
top: -0.25em;
}
<label for="check1"><input type="checkbox" id="check1" checked="checked" /> checkbox one</label>
<label for="check2"><input type="checkbox" id="check2" /> another checkbox</label>
<label for="check3" style="font-size:150%"><input type="checkbox" id="check3" checked="checked" /> bigger checkbox </label>
Here's a trick that works in most recent browsers (IE9+) as a CSS only solution that can be improved with javascript to support IE8 and below.
<div>
<input type="checkbox" id="checkboxID" name="checkboxName" value="whatever" />
<label for="checkboxID"> </label>
</div>
Style the label with what you want the checkbox to look like
#checkboxID
{
position: absolute fixed;
margin-right: 2000px;
right: 100%;
}
#checkboxID + label
{
/* unchecked state */
}
#checkboxID:checked + label
{
/* checked state */
}
For javascript, you'll be able to add classes to the label to show the state. Also, it would be wise to use the following function:
$('label[for]').live('click', function(e){
$('#' + $(this).attr('for') ).click();
return false;
});
EDIT to modify #checkboxID styles
I'm writtinga phonegap app, and checkboxes vary in size, look, etc.
So I made my own simple checkbox:
First the HTML code:
<span role="checkbox"/>
Then the CSS:
[role=checkbox]{
background-image: url(../img/checkbox_nc.png);
height: 15px;
width: 15px;
display: inline-block;
margin: 0 5px 0 5px;
cursor: pointer;
}
.checked[role=checkbox]{
background-image: url(../img/checkbox_c.png);
}
To toggle checkbox state, I used JQuery:
CLICKEVENT='touchend';
function createCheckboxes()
{
$('[role=checkbox]').bind(CLICKEVENT,function()
{
$(this).toggleClass('checked');
});
}
But It can easily be done without it...
Hope it can help!