checkbox formatting and functionality - html

HTML:
<input type="checkbox" id="checkentire"> PLAY ENTIRE LESSON
<br>
<input type="checkbox" id="checkloop"> LOOP
CSS:
input[type=checkbox] {
width:45px;
margin:5px 0;
cursor:pointer;
vertical-align:middle;
}
input[type=checkbox]:hover {
background:lightgreen;
}
Problems:
1. input[type=checkbox]:hover - doesn't work at all.
2. cursor:pointer - works only on graphic symbol, not on text.
3. width:45px - increases graphic margins instead of graphic itself.
4. vertical-align:middle - graphic and text are not aligned.
5. turning on and off works only by clicking on graphic, instead on graphic and text.

You can wrap your checkbox element inside a wrapper element and work with relative and absolute positioning. That makes it easy to control the width and vertical alignment as well as the hovering state and the fact that you can click on the label text to check the checkbox.
Here is an example. This is actually kind of what bootstrap does.
.checkbox-wrapper {
position: relative;
display: inline-block;
width: auto;
margin-top: 10px;
margin-bottom: 10px;
background:tomato;
padding: 0 20px;
}
.checkbox-wrapper label {
display: inline-block;
max-width: 100%;
min-height: 40px;
line-height: 40px;
vertical-align: middle;
padding-left: 20px;
cursor: pointer;
}
.checkbox-wrapper input[type=checkbox] {
position: absolute;
margin: 0 0 0 -20px;
top: 50%;
transform: translate(0,-50%);
}
.checkbox-wrapper:hover {
background:lightgreen;
}
<div class="checkbox-wrapper">
<label for="checkentire">
<input type="checkbox" id="checkentire">
PLAY ENTIRE LESSON
</label>
</div>

You may try this to get your checkbox:hover working.
https://jsfiddle.net/k7hcgjk5/1/
In fact, you can not directly customize your checkbox. Instead, simply create a custom checkbox styled span which does that for you.
EDIT: Well, I forgot. Obviously, to handle your checked state, you need to add a customization for that as well. I updated the fiddle accordingly.

In many times to control the look and size of the checkbox you need to use "custom checkboxes", benefits are having consistent look on browsers (*), also you can have circles instead of squares, or blue instead of red, and you can have :hover for the label itself too, possibilities are many, just like this:
JS Fiddle -updated 2
#test-checkbox{
display:none;
}
#test-checkbox + label{
color:black;
cursor:pointer;
}
#test-checkbox:checked + label{
color:#0A0;
cursor:pointer;
}
#test-checkbox + label:hover{
outline:1px dotted blue;
outline-offset:4px;
}
#test-checkbox + label::before{
content:'';
width:12px;
height:12px;
display:inline-block;
border:2px solid #AAA;
border-radius:3px;
margin-right:5px;
cursor:pointer;
position:relative;
top:2px;
}
#test-checkbox:checked + label::after{
width:5px;
height:12px;
content:'';
display:inline-block;
border-right:4px solid #0A0;
border-bottom:4px solid #0A0;
position:absolute;
left:14px;
top:6px;
-ms-transform: rotate(45deg); /* IE 9 */
-webkit-transform: rotate(45deg); /* Chrome, Safari, Opera */
transform: rotate(45deg);
}
<input id="test-checkbox" type="checkbox">
<label for="test-checkbox">Check Me</label>
--------------------------------------------------------------------------------
(*) IE8 doesn't support the pseudo elements ::before and ::after which are the correct syntax, it supports :before and :after which are also supported by other browsers, for more information check ::before and ::after

Related

Change color of Icon-Image on :hover – what other ways are there?

I have the following problem and it drives me crazy:
Basicly I have a div-container with an background. This background should change when I hover it (see pichture). It is an png and instead of white it should turn red.
What I have done until now:
First: CSS sprite
Thought it will be the best solution but becuase the div changes it's size (responsive) and the icon does not have a fixed size it was not very clean: I had a small offset on hovering. Not sure why… mybe this can be fixed… 
Second: 2 separate images
But this is not an option in this case because I need to work with inline styles. :hover ist not available as inline style.
Thrid: tried mask-box-image
Was a woderful solution… but Firefox does not support it.
Does anyone has another idea how to solve it?
Give This a Try
CSS
.icon-cont{
height:300px;
width:300px;
background-color: #ff0000;
text-align:center;
}
.icon-cont:hover{
background-color: transparent;
}
.icon-cont:hover .icon,
.icon-cont:hover .icon::before,
.icon-cont:hover .icon::after
{
border-color:#ff0000;
}
.icon{
height:0px;
border-bottom:2px solid #fff;
width:60%;
line-height:300px;
position: relative;
margin:auto;
top:50%;
}
.icon::before{
content:"";
position: absolute;
top:0;
bottom: 0;
left:-30px;
margin:auto;
height:20px;
width:20px;
border:2px solid #fff;
border-radius:50px;
}
.icon::after{
content:"";
position: absolute;
top:0;
bottom: 0;
right:-30px;
margin:auto;
height:20px;
width:20px;
border:2px solid #fff;
border-radius:50px;
}
HTML
<div class="icon-cont">
<div class="icon"></div>
</div>
Link for reference
hope this helps..
May be it will help
I posted an example following
.box {
padding: 20px;
display: inline-block;
background:tomato;
}
.box:hover {
background: transparent;
}
.box:hover span {
color: tomato;
}
.box span {
display: inline-block;
color: #fff;
}
<div class="box">
<span>a</span>
<span>----</span>
<span>b</span>
</div>
You can't change color of .png with css. I think you should make a font out of your icons in order to change their color with css later.
I haven't done that myself, but I know those fonts, like font-awesome can change color. There are some automatic generators in google to make your own font.
Try this.

Custom checkbox only works with background color, not image

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>

How do I make an HTML checkbox look like a paper form checkbox? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have several html checkboxes I'd like to style like a paper form:
Is there any way to do this with CSS?
Is there any way to build a control that looks like this but acts like a checkbox?
When the box is checked, there should be an X in it:
Update
For those of you looking for a similar question: Make checkbox in chrome look like one in IE
This one has some pretty good answers also.
Here is a solution based on your mockups:
body {
font-family: Arial, sans-serif;
}
/* Hide default checkbox */
input[type=checkbox] {
display: none;
}
/* Style custom checkbox (indicated by <span>) that corresponds to its hidden version */
input[type=checkbox] + label span {
border: 1px solid #000;
display: inline-block;
width: 15px;
height: 15px;
margin: 0 4px 0 8px;
padding: 2px;
text-align: center;
vertical-align: middle;
cursor: pointer;
}
/* 'Checked' state */
input[type=checkbox]:checked + label span::after {
content: "X";
}
<form>
Occupant
<input type="checkbox" id="owner" name="occ" value="Owner"/>
<label for="owner"><span></span>Owner</label>
<input type="checkbox" id="tenant" name="occ" value="Tenant"/>
<label for="tenant"><span></span>Tenant</label>
<input type="checkbox" id="vacant" name="occ" value="Vacant"/>
<label for="vacant"><span></span>Vacant</label>
</form>
You could try something like this but you need to create your checkbox images, to use it.
input[type=checkbox] {
position: relative;
visibility: hidden;
cursor: pointer;
}
input[type=checkbox]:after {
display: block;
content: "";
position: absolute;
top: 0;
right: -30px;
visibility: visible;
height: 50px;
width: 50px;
text-align: center;
border-radius: 4px;
background:url(http://www.clipartbest.com/cliparts/yTo/gj4/yTogj4zEc.png);
background-size:100%;
color: #fff;
font-weight: 600;
cursor: pointer;
}
input[type=checkbox]:checked:after {
content: "";
background: url(http://www.clipartbest.com/cliparts/ncX/jL6/ncXjL6rcB.png);
background-size:100%;
}
<input type="checkbox" />
JSFiddle
Mozilla FireFox UPDATE
Using just some dummy elements, for here using label and using it by applying CSS to it.
label.checkbox input[type="checkbox"] {display:none;}
label.checkbox span {
display:inline-block;
width:25px; /* This must be depend on image resolution */
height:25px; /* This must be depend on image resolution */
background:url(http://www.clipartbest.com/cliparts/yTo/gj4/yTogj4zEc.png);
background-size:100%;
-moz-background-size:100%;
background-repeat:no-repeat;
vertical-align:middle;
margin:3px;
}
label.checkbox :checked + span {
background:url(http://www.clipartbest.com/cliparts/ncX/jL6/ncXjL6rcB.png);
background-size:100%;
-moz-background-size:100%;
}
<label class="checkbox"><input type="checkbox"/><span></span> Whatever you wanna say here... </label>
Note : But still will not work in old browsers
HTML
< span onclick="this.innerHTML = (this.innerHTML ? '' : 'X')">
CSS
span{
display:inline-block;
width:15px;
height:15px;
font:14px verdana;
border:solid 1px black;
color:green;
vertical-align:baseline;
cursor:pointer;}
http://jsfiddle.net/ApHME/59/
It's possible to hide the real input[type=checkbox] and use an :after pseudo-element to create anything you want in front of the checkbox. Here's a question with a similar problem: css only checkbox (with content attribute). Then just use CSS the way you want to stylize it.

How to change style of radio and checkbox input [duplicate]

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

Checkboxes in web pages – how to make them bigger?

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!