I'm styling my checkboxes to look like toggle switches which works beautifully in Chrome, UC Browser, Safari for Windows and is a bit choppy in Firefox but acceptable. Unfortunately in Edge it doesn't work at all.
I've looked through the CSS properties I'm using and it appears as though all are supported by Edge so I don't understand what I'm missing.
.toggle{
width:34px;
height:16px;
border-radius:40px;
position:relative;
-webkit-appearance: none;
margin:2px 0 0 0px;
box-shadow: inset 1px 1px 1px rgba(0, 0, 0, 0.4);
background: -webkit-linear-gradient(#c6c6c6,#e3e3e3);
cursor:hand;cursor:pointer;pointer:hand;
outline: 0;
}
.toggle:checked{
background: -webkit-linear-gradient(#77178F,#AD73BB);
box-shadow: inset 1px 1px 3px rgba(0, 0, 0, 0.4);
}
.toggle:before {
content:'';
letter-spacing:1px;
color: rgba(0,0,0,.15);
font-size:10px;
font-weight:100;
text-shadow:1px 1px 1px rgba(255,255,255,1);
width:7px;
height:7px;
padding:2px;
top:2px;
left:2px;
position:absolute;
border-radius:40px;
background: linear-gradient(#ebebeb,#f1f1f1);
box-shadow: -2px 2px 8px rgba(0, 0, 0, 0.2),
-1px 1px 2px rgba(0, 0, 0, 0.3),
inset 1px 1px 1px #ffffff;
transition: all 0.4s;
}
.toggle:checked:before {
left:20px;
background:#f1f1f1;
}
.toggle:checked:after {
background: linear-gradient(#d8eec4,#5fa718);
box-shadow: inset -1px -1px 4px #417f0b,
inset 1px 1px 2px #5b9f1a;
}
<input type=checkbox class=toggle>
It looks to me like -webkit-appearance:none is not doing what it's supposed to (hide the original checkbox) but that also is supposedly supported.
If anybody has any experience with this issue and can give me some insight it would be much appreciated.
It's better to hide a checkbox, wrap it with another element (e.g. label) and add a sibling to the checkbox. You can find an example of checkbox-switch here https://www.w3schools.com/howto/howto_css_switch.asp.
Or you can adjust code below:
.toggle {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
.toggle input[type=checkbox] {
opacity: 0; /* or use display: none; to hide default checkbox */
width: 0;
height: 0;
}
.toggle .slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: linear-gradient(#ebebeb, #f1f1f1);
box-shadow: inset 1px 1px 1px rgba(0, 0, 0, 0.4);
-webkit-transition: .4s;
transition: .4s;
}
.toggle .slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background: #ffffff;
-webkit-transition: .4s;
transition: .4s;
}
.toggle input[type=checkbox]:checked + .slider {
background: linear-gradient(#77178F,#AD73BB);
box-shadow: inset 1px 1px 3px rgba(0, 0, 0, 0.4);
}
.toggle input[type=checkbox]:checked + .slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
.toggle .slider.round {
border-radius: 34px;
}
.toggle .slider.round:before {
border-radius: 50%;
}
<label class="toggle">
<input type="checkbox">
<span class="slider round"></span>
</label>
You could add a label to meet your requirements as a workaround. In this way you could connect a label with a checkbox and apply the styles to the label instead of the checkbox itself.
Here is my testing code.
CSS.
.toggle {
visibility: hidden;
opacity: 0;
}
.toggle + label {
width: 34px;
height: 16px;
border-radius: 40px;
position: relative;
box-shadow: inset 1px 1px 1px rgba(0, 0, 0, 0.4);
background: linear-gradient(#c6c6c6,#e3e3e3);
cursor: pointer;
outline: 0;
display: inline-block;
}
.toggle + label:before {
content: '';
letter-spacing: 1px;
color: rgba(0,0,0,.15);
font-size: 10px;
font-weight: 100;
text-shadow: 1px 1px 1px rgba(255,255,255,1);
width: 7px;
height: 7px;
padding: 2px;
top: 2px;
left: 2px;
position: absolute;
border-radius: 40px;
background: linear-gradient(#ebebeb,#f1f1f1);
box-shadow: -2px 2px 8px rgba(0, 0, 0, 0.2), -1px 1px 2px rgba(0, 0, 0, 0.3), inset 1px 1px 1px #ffffff;
transition: all 0.4s;
}
.toggle:checked + label {
background: linear-gradient(#77178F,#AD73BB);
box-shadow: inset 1px 1px 3px rgba(0, 0, 0, 0.4);
}
.toggle:checked + label:before {
left: 20px;
background: #f1f1f1;
}
.toggle:checked + label:after {
background: linear-gradient(#d8eec4,#5fa718);
box-shadow: inset -1px -1px 4px #417f0b, inset 1px 1px 2px #5b9f1a;
}
HTML
<input type="checkbox" class="toggle" id="mycheckbox">
<label for="mycheckbox" />
Adding another flexible variation of the selected answer to the mix: https://codepen.io/hawatt/pen/MWKyJJZ
HAML
.toggle-switch
%input{type: 'checkbox', id: 'away-from-desk' }
%span.toggle-handle
%label{'for' => 'away-from-desk'} Away from desk
SCSS
$toggle-width: 2.8rem;
$toggle-height: 1.5rem;
$toggle-handle-diameter: calc(#{$toggle-height} * 0.8);
.toggle-switch {
position: relative;
display: flex;
align-items: center;
input {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
width: $toggle-width;
height: $toggle-height;
display: inline-flex;
position: relative;
border-radius: $toggle-height;
overflow: hidden;
outline: none;
border: none;
cursor: pointer;
background-color: #c0c0c0;
transition: all ease 0.3s;
z-index: 0;
text-indent: -5000px;
}
.toggle-handle {
pointer-events: none;
display: flex;
position: absolute;
z-index: 2;
width: $toggle-handle-diameter;
height: $toggle-handle-diameter;
background: #fff;
border-radius: 50%;
left: 0;
margin: 0 0.35rem;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
transition: all ease 0.3s;
}
input:checked,
input.enabled {
background-color: dodgerblue;
& + .toggle-handle {
left: calc(#{$toggle-width} - #{$toggle-handle-diameter} - 0.25rem);
}
}
label {
cursor: pointer;
}
}
Related
I have a range slider styled in Chrome.. I'm having major issues trying to get it to look the same in IE11 and firefox.. I've tried following some guides on how to make it compatible using the ms- and moz- for the webkit tags but it's still not formatting the same. I understand it may be a little different but its just nowhere near the same result. here's the code so far.. basically the range slider needs to be formatted same as the button above it.... any help much appreciated.
.switch {
position: relative;
display: block;
vertical-align: top;
width: 100px;
height: 30px;
padding: 3px;
margin: 0 10px 10px 0;
background: linear-gradient(to bottom, #eeeeee, #FFFFFF 25px);
background-image: -webkit-linear-gradient(top, #eeeeee, #FFFFFF 25px);
border-radius: 18px;
box-shadow: inset 0 -1px white, inset 0 1px 1px rgba(0, 0, 0, 0.05);
cursor: pointer;
box-sizing:content-box;
}
.switch-input {
position: absolute;
top: 0;
left: 0;
opacity: 0;
box-sizing:content-box;
}
.switch-label {
position: relative;
display: block;
height: inherit;
font-size: 10px;
text-transform: uppercase;
background: #eceeef;
border-radius: inherit;
box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.12), inset 0 0 2px rgba(0, 0, 0, 0.15);
box-sizing:content-box;
}
.switch-label_1 {
position: relative;
display: block;
height: inherit;
font-size: 10px;
text-transform: uppercase;
background: #eceeef;
border-radius: inherit;
box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.12), inset 0 0 2px rgba(0, 0, 0, 0.15);
box-sizing:content-box;
}
.switch-label:before, .switch-label:after, .switch-label_1:before, .switch-label_1:after {
position: absolute;
top: 50%;
margin-top: -.5em;
line-height: 1;
-webkit-transition: inherit;
-moz-transition: inherit;
-o-transition: inherit;
transition: inherit;
box-sizing:content-box;
}
.switch-label:before, .switch-label_1:before {
content: attr(data-off);
right: 11px;
color: #aaaaaa;
text-shadow: 0 1px rgba(255, 255, 255, 0.5);
}
.switch-label:after, .switch-label_1:after {
content: attr(data-on);
left: 11px;
color: #FFFFFF;
text-shadow: 0 1px rgba(0, 0, 0, 0.2);
opacity: 0;
}
.switch-input:checked ~ .switch-label {
background: #86b692;
box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.15), inset 0 0 3px rgba(0, 0, 0, 0.2);
}
.switch-input:checked ~ .switch-label:before {
opacity: 0;
}
.switch-input:checked ~ .switch-label:after {
opacity: 1;
}
.switch-input:checked ~ .switch-label_1 {
background: #a9d2d5;
box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.15), inset 0 0 3px rgba(0, 0, 0, 0.2);
}
.switch-input:checked ~ .switch-label_1:before {
opacity: 0;
}
.switch-input:checked ~ .switch-label_1:after {
opacity: 1;
}
.switch-handle {
position: absolute;
top: 4px;
left: 4px;
width: 28px;
height: 28px;
background: linear-gradient(to bottom, #FFFFFF 40%, #f0f0f0);
background-image: -webkit-linear-gradient(top, #FFFFFF 40%, #f0f0f0);
border-radius: 100%;
box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2);
}
.switch-handle:before {
content: "";
position: absolute;
top: 50%;
left: 50%;
margin: -6px 0 0 -6px;
width: 12px;
height: 12px;
background: linear-gradient(to bottom, #eeeeee, #FFFFFF);
background-image: -webkit-linear-gradient(top, #eeeeee, #FFFFFF);
border-radius: 6px;
box-shadow: inset 0 1px rgba(0, 0, 0, 0.02);
}
.switch-input:checked ~ .switch-handle {
left: 74px;
box-shadow: -1px 1px 5px rgba(0, 0, 0, 0.2);
}
/* Transition
========================== */
.switch-label, .switch-label_1, .switch-handle {
transition: All 0.3s ease;
-webkit-transition: All 0.3s ease;
-moz-transition: All 0.3s ease;
-o-transition: All 0.3s ease;
}
.range {
display: block;
vertical-align: top;
width: 300px;
height: 30px;
padding: 3px;
margin: 0 10px 10px 0;
background: linear-gradient(to bottom, #eeeeee, #FFFFFF 25px);
background-image: -webkit-linear-gradient(top, #eeeeee, #FFFFFF 25px);
border-radius: 18px;
box-shadow: inset 0 -1px white, inset 0 1px 1px rgba(0, 0, 0, 0.05);
cursor: pointer;
box-sizing:content-box;
}
.range-frame{
position: relative;
display: block;
height: inherit;
font-size: 10px;
background: #eceeef;
border-radius: inherit;
box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.12), inset 0 0 2px rgba(0, 0, 0, 0.15);
box-sizing:content-box;
}
input[type=range] {
-webkit-appearance: none;
margin: 10px 0;
width: 100%;
}
input[type=range]:focus {
outline: none;
}
input[type=range]::-webkit-slider-runnable-track {
width: 100%;
height: 35px;
cursor: pointer;
animate: 0.2s;
box-shadow: 0px 0px 0px #000000, 0px 0px 0px #0d0d0d;
background: lightgray;
border-radius: 25px;
border: 0px solid #000101;
}
input[type=range]::-webkit-slider-thumb {
box-shadow: 0px 0px 0px #000000, 0px 0px 0px #0d0d0d;
border: 0px solid #000000;
height: 35px;
width: 39px;
border-radius: 35px;
background: silver;
cursor: pointer;
-webkit-appearance: none;
margin-top: -1px;
}
input[type=range]:focus::-webkit-slider-runnable-track {
background: lightgray;
}
input[type=range]::-moz-range-track {
width: 100%;
height: 35px;
cursor: pointer;
animate: 0.2s;
box-shadow: 0px 0px 0px #000000, 0px 0px 0px #0d0d0d;
background: lightgray;
border-radius: 25px;
border: 0px solid #000101;
}
input[type=range]::-moz-range-thumb {
box-shadow: 0px 0px 0px #000000, 0px 0px 0px #0d0d0d;
border: 0px solid #000000;
height: 35px;
width: 39px;
border-radius: 35px;
background: silver;
cursor: pointer;
}
input[type=range]::-ms-track {
width: 100%;
height: 35px;
cursor: pointer;
animate: 0.2s;
background: transparent;
border-color: transparent;
border-width: 39px 0;
color: transparent;
}
input[type=range]::-ms-fill-lower {
background: lightgray;
border: 0px solid #000101;
border-radius: 35px;
box-shadow: 0px 0px 0px #000000, 0px 0px 0px #0d0d0d;
}
input[type=range]::-ms-fill-upper {
background: transparent;
border: 0px solid #000101;
border-radius: 35px;
box-shadow: 0px 0px 0px #000000, 0px 0px 0px #0d0d0d;
}
input[type=range]::-ms-thumb {
box-shadow: 0px 0px 0px #000000, 0px 0px 0px #0d0d0d;
border: 0px solid #000000;
height: 35px;
width: 39px;
border-radius: 35px;
background: silver;
cursor: pointer;
}
input[type=range]:focus::-ms-fill-lower {
background: lightgray;
}
input[type=range]:focus::-ms-fill-upper {
background:lightgray;
}
.output-value{
margin: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 10px;
color: #aaaaaa;
pointer-events: none;
}
<html>
<head>
<title>Roland</title>
<link rel="stylesheet" href="range.css" />
<link rel="stylesheet" href="switch.css">
</head>
<body>
<form>
<label class="switch">
<input class="switch-input" type="checkbox" Filter="PL" Column="1">
<span class="switch-label" data-on="PL" data-off="PL"></span>
<span class="switch-handle"></span>
</label>
<label class="range">
<span class="range-frame">
<span for fader class="output-value" id="rangevalue">0 Nm</span>
<input class="range-input" id="fader" type="range" min="0" max="150" step="1" value="0" onchange="outputUpdate(value)">
</span>
</label>
</form>
<script>
function outputUpdate(vol) {
document.getElementById('rangevalue').innerHTML=vol + ' Nm';
}
</script>
</body>
</html>
here's what it should look like on all browsers -
thanks
C
The input type range is quite customizable. Unfortunately, each browser has its own way and thus we have to write longer codes than expected. Than also you will not get the exact similar results. The result will look little bit different from other browsers.
You can refer an example below which I try to customize to make it looks like your range slider. I tested it with various browsers and it looks almost similar in most of the browsers.
<!DOCTYPE html>
<html>
<head>
<style>
input[type=range] {
-webkit-appearance: none;
margin: 10px 0;
width: 100%;
}
input[type=range]:focus {
outline: none;
}
input[type=range]::-webkit-slider-runnable-track {
width: 100%;
height: 35px;
cursor: pointer;
animate: 0.2s;
box-shadow: 0px 0px 0px #000000, 0px 0px 0px #0d0d0d;
background: lightgray;
border-radius: 25px;
border: 0px solid #000101;
}
input[type=range]::-webkit-slider-thumb {
box-shadow: 0px 0px 0px #000000, 0px 0px 0px #0d0d0d;
border: 0px solid #000000;
height: 35px;
width: 39px;
border-radius: 35px;
background: silver;
cursor: pointer;
-webkit-appearance: none;
margin-top: -1px;
}
input[type=range]:focus::-webkit-slider-runnable-track {
background: lightgray;
}
input[type=range]::-moz-range-track {
width: 100%;
height: 35px;
cursor: pointer;
animate: 0.2s;
box-shadow: 0px 0px 0px #000000, 0px 0px 0px #0d0d0d;
background: lightgray;
border-radius: 25px;
border: 0px solid #000101;
}
input[type=range]::-moz-range-thumb {
box-shadow: 0px 0px 0px #000000, 0px 0px 0px #0d0d0d;
border: 0px solid #000000;
height: 35px;
width: 39px;
border-radius: 35px;
background: silver;
cursor: pointer;
}
input[type=range]::-ms-track {
width: 100%;
height: 35px;
cursor: pointer;
animate: 0.2s;
background: transparent;
border-color: transparent;
border-width: 39px 0;
color: transparent;
}
input[type=range]::-ms-fill-lower {
background: lightgray;
border: 0px solid #000101;
border-radius: 35px;
box-shadow: 0px 0px 0px #000000, 0px 0px 0px #0d0d0d;
}
input[type=range]::-ms-fill-upper {
background: lightgray;
border: 0px solid #000101;
border-radius: 35px;
box-shadow: 0px 0px 0px #000000, 0px 0px 0px #0d0d0d;
}
input[type=range]::-ms-thumb {
box-shadow: 0px 0px 0px #000000, 0px 0px 0px #0d0d0d;
border: 0px solid #000000;
height: 35px;
width: 39px;
border-radius: 35px;
background: silver;
cursor: pointer;
}
input[type=range]:focus::-ms-fill-lower {
background: lightgray;
}
input[type=range]:focus::-ms-fill-upper {
background:lightgray;
}
body {
padding: 30px;
}
</style>
</head>
<body>
<input type="range">
</body>
</html>
Output in various browsers:
Reference:
(1) Codepen example
(2) Styling Cross-Browser Compatible Range Inputs with CSS
Further, you can try to modify it based on your own requirement or use it as a reference for making the changes in your own code.
In my project we needed to style the checkbox element. I have seen that a common aproach is to style the :before element of the associated label instead.
So far I have managed to implement most of it, but I am having an issue with the :before element pushing the first line of the label text to the right.
I am trying to align all the text in the label to the right of the :before element, but so far the second line always starts at the same place than the :before element.
I am not a frontend dev, but I have tried changing padding, margin, left, position, etc. But I am at a loss.
This is how it looks:
I want all the text of the label ot start at the red line.
You can see what I have so far in this Fiddle.
.container-box{
display:block;
max-width:200px;
}
.neat-checkbox {
display: inline;
width:40px;
}
.neat-checkbox input[type=checkbox] {
position: absolute;
top: 0;
left: 0;
margin-left: 0;
cursor: pointer;
opacity: 0;
z-index: 1;
font-size: 10px !important;
display: none;
}
.neat-checkbox label {
margin-left: 0px;
margin-top: 2px;
line-height: 17px !important;
}
.neat-checkbox label:before {
font-family: "FontAwesome";
content: "\f00c";
color: white;
position: relative;
left: -5px;
top: 0px;
outline: none;
cursor: pointer;
display: inline-block;
width: 17px;
height: 17px;
border: 1px solid #ced4da;
border-radius: 3px;
background-color: #fff;
transition: border 0.15s ease-in-out, color 0.15s ease-in-out;
box-sizing: border-box;
font-size: 11px;
padding-left: 2px;
line-height: 17px;
}
.neat-checkbox input[type=checkbox]:checked + label:before {
background-color: #2bb19e;
border-color: #2bb19e;
color: #fff;
font-family: "FontAwesome";
content: "\f00c";
font-size: 11px;
padding-left: 2px;
line-height: 17px;
}
.neat-checkbox input[type="checkbox"]:focus + label::before {
border-color: #66afe9;
outline: 0;
-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(102, 175, 233, 0.6);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(102, 175, 233, 0.6);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(102, 175, 233, 0.6);
}
<div class="container-box">
<div>
<label>Some other stuff</label>
</div>
<div class="neat-checkbox">
<input type="checkbox" class="checkbox-list" id="checkboxId_25600" name="name_25600" value="25600">
<label for="checkboxId_25600">Label For Checkbox MOre more more</label>
</div>
</div>
Give the label some left padding and position the :before element absolutely within it. EG:
.container-box {
display: block;
max-width: 200px;
}
.neat-checkbox {
display: inline;
width: 40px;
}
.neat-checkbox input[type=checkbox] {
position: absolute;
top: 0;
left: 0;
margin-left: 0;
cursor: pointer;
opacity: 0;
z-index: 1;
font-size: 10px !important;
display: none;
}
.neat-checkbox label {
margin-left: 0px;
margin-top: 2px;
line-height: 17px !important;
display: inline-block; /* make it block-level */
position: relative; /* so that child elements can be positioned relative to it */
padding-left: 25px; /* allow some space for the pseudo checkbox */
}
.neat-checkbox label:before {
font-family: "FontAwesome";
content: "\f00c";
color: white;
left: 0px;
top: 0px;
outline: none;
cursor: pointer;
display: block; /* make it block-level */
position: absolute; /* give an absolute position (defaults to 0,0) */
width: 17px;
height: 17px;
border: 1px solid #ced4da;
border-radius: 3px;
background-color: #fff;
transition: border 0.15s ease-in-out, color 0.15s ease-in-out;
box-sizing: border-box;
font-size: 11px;
padding-left: 2px;
line-height: 17px;
}
.neat-checkbox input[type=checkbox]:checked+label:before {
background-color: #2bb19e;
border-color: #2bb19e;
color: #fff;
font-family: "FontAwesome";
content: "\f00c";
font-size: 11px;
padding-left: 2px;
line-height: 17px;
}
.neat-checkbox input[type="checkbox"]:focus+label::before {
border-color: #66afe9;
outline: 0;
-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(102, 175, 233, 0.6);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(102, 175, 233, 0.6);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(102, 175, 233, 0.6);
}
<div class="container-box">
<div>
<label>Some other stuff</label>
</div>
<div class="neat-checkbox">
<input type="checkbox" class="checkbox-list" id="checkboxId_25600" name="name_25600" value="25600">
<label for="checkboxId_25600">Label For Checkbox MOre more more</label>
</div>
</div>
The problem is that you are moving the checkbox using position relative, so the original space is preserved but you just move the visual 5px to the left.
You can add a margin-right: -5px if you use left: -5px or remove the left property and just use margin-left: -5px instead.
I'm looking a solution how to change css background color with padding or white color around the checkbox, I tried and play around using margin, padding height and etc, but no luck.
So when user click the checkbox, it would show background color, it's working, but I'd like to have white padding around the checkbox, is it possible?
.checkbox {
margin: 0 0 1em 2em;
}
.checkbox .tag {
color: #595959;
display: block;
float: left;
font-weight: bold;
position: relative;
width: 120px;
}
.checkbox label {
display: inline;
}
.checkbox .input-assumpte {
display: none;
}
.input-assumpte + label:after {
background-color: #fafafa;
border: 1px solid #cacece;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05),
inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05);
display: inline-block;
transition-duration: 0.3s;
width: 16px;
height: 16px;
content: "";
margin-left: 10px;
padding: 5px;
}
.input-assumpte:checked + label:after {
background-color: #0e4caa;
padding: 5px;
}
<div class="checkbox">
<input type="checkbox" class="input-assumpte" id="input-confidencial" />
<label for="input-confidencial">Confidencial</label>
</div>
Exactly what you want is background-clip, easy:
.checkbox {
margin: 0 0 1em 2em;
}
.checkbox .tag {
color: #595959;
display: block;
float: left;
font-weight: bold;
position: relative;
width: 120px;
}
.checkbox label {
display: inline;
}
.checkbox .input-assumpte {
display: none;
}
.input-assumpte + label::after {
background-color: #fafafa;
border: 4px solid #0e4caa;
background-clip: content-box;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05);
display: inline-block;
transition-duration: 0.3s;
width: 16px;
height: 16px;
content: "";
margin-left: 10px;
padding: 5px;
}
.input-assumpte:checked + label::after {
background-color: #0e4caa;
padding: 5px;
}
<div class="checkbox">
<input type="checkbox" class="input-assumpte" id="input-confidencial" />
<label for="input-confidencial">Confidencial</label>
</div>
Also on JSFiddle.
The background-clip CSS property sets whether an element's background
extends underneath its border box, padding box, or content box.
Just to mention, there is also one more solution for extra border using outline, but it extends the element:
.checkbox {
margin: 0 0 1em 2em;
}
.checkbox .tag {
color: #595959;
display: block;
float: left;
font-weight: bold;
position: relative;
width: 120px;
}
.checkbox label {
display: inline;
}
.checkbox .input-assumpte {
display: none;
}
.input-assumpte + label::after {
background-color: #fafafa;
border: 4px solid #fafafa;
outline: 4px solid #0e4caa;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05);
display: inline-block;
transition-duration: 0.3s;
width: 16px;
height: 16px;
content: "";
margin-left: 10px;
padding: 5px;
}
.input-assumpte:checked + label::after {
background-color: #0e4caa;
padding: 5px;
}
<div class="checkbox">
<input type="checkbox" class="input-assumpte" id="input-confidencial" />
<label for="input-confidencial">Confidencial</label>
</div>
You can use the CSS box-shadow property:
.checkbox {
margin: 0 0 1em 2em;
}
.checkbox .tag {
color: #595959;
display: block;
float: left;
font-weight: bold;
position: relative;
width: 120px;
}
.checkbox label {
display: inline;
}
.checkbox .input-assumpte {
display: none;
}
.input-assumpte+label:after {
background-color: #fafafa;
border: 1px solid #cacece;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05);
display: inline-block;
transition-duration: 0.3s;
width: 16px;
height: 16px;
content: "";
margin-left: 10px;
padding: 5px;
}
.input-assumpte:checked+label:after {
background-color: #0e4caa;
padding: 5px;
box-shadow: inset 0px 0px 0px 4px white
}
<div class="checkbox">
<input type="checkbox" class="input-assumpte" id="input-confidencial" />
<label for="input-confidencial">Confidencial</label>
</div>
inset shadow should be sufficient for this
box-shadow:0 1px 2px rgba(0, 0, 0, 0.05),
inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05), inset 0 0 0 2px #0e4caa ,inset 0 0 0 4px white;
Demo
.checkbox {
margin: 0 0 1em 2em;
}
.checkbox .tag {
color: #595959;
display: block;
float: left;
font-weight: bold;
position: relative;
width: 120px;
}
.checkbox label {}
.checkbox .input-assumpte {
display: none;
}
.input-assumpte+label:after {
background-color: #fafafa;
border: 1px solid #cacece;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05);
display: inline-block;
transition-duration: 0.3s;
width: 16px;
height: 16px;
content: "";
margin-left: 10px;
padding: 5px;
}
.input-assumpte:checked+label:after {
background-color: #0e4caa;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05), inset 0 0 0 2px #0e4caa, inset 0 0 0 4px white;
padding: 5px;
}
<div class="checkbox">
<input type="checkbox" class="input-assumpte" id="input-confidencial" />
<label for="input-confidencial">Confidencial</label>
</div>
outline with a negative offset could also work:
outline: solid white 2px;
outline-offset: -5px;
.checkbox {
margin: 0 0 1em 2em;
}
.checkbox .tag {
color: #595959;
display: block;
float: left;
font-weight: bold;
position: relative;
width: 120px;
}
.checkbox label {}
.checkbox .input-assumpte {
display: none;
}
.input-assumpte+label:after {
background-color: #fafafa;
border: 1px solid #cacece;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05);
display: inline-block;
transition-duration: 0.3s;
width: 16px;
height: 16px;
content: "";
margin-left: 10px;
padding: 5px;
}
.input-assumpte:checked+label:after {
background-color: #0e4caa;
outline: solid white 2px;
outline-offset: -5px;
padding: 5px;
}
<div class="checkbox">
<input type="checkbox" class="input-assumpte" id="input-confidencial" />
<label for="input-confidencial">Confidencial</label>
</div>
Tooltip CSS
.has-tooltip .tooltip {
position: absolute;
left: 50%;
top: -40px;
opacity: 0;
-webkit-transition: ease-out 300ms opacity;
-moz-transition: ease-out 300ms opacity 0ms;
-o-transition: ease-out 300ms opacity 0ms;
transition: ease-out 300ms opacity 0ms;
-webkit-transition-delay: 0ms;
pointer-events: none;
z-index: 9999;
}
.has-tooltip .tooltip span {
position: relative;
left: -50%;
display: block;
padding: 2px 8px;
font-size: 14px;
color: #fff;
white-space: nowrap;
background: rgba(45, 45, 45, 1);
border: 1px solid rgba(204,204,204,0.60);
-webkit-border-radius: 6px;
-moz-border-radius: 6px;
border-radius: 6px;
-webkit-box-shadow: 0px 0px 0px 6px rgba(0,0,0,0.50);
-moz-box-shadow: 0px 0px 0px 6px rgba(0,0,0,0.50);
box-shadow: 0px 0px 0px 6px rgba(0,0,0,0.50);
}
View Live Example
What I Am Trying To Achieve
By only having one element, have a tooltip styled as seen in the link above with a tooltip arrow which also has the same look applied to appear to be one element. I've seen multiple which place another element within for the arrow however I'm hoping to achieve this without needing to do so.
Questions
How can I have a tooltip arrow which gives the impression it is part of the tooltip with the same styles applied.
Note: If you provide code, could you please fully explain what you have done so I can implement a top tip as well as a bottom tip.
You could use a pseudo element, i.e. ::before, and by rotate it 45 deg and positioning it about half its height from the bottom so it looks like an arrow.
a.has-tooltip {
position: relative;
transition: opacity 0.5s;
}
a.has-tooltip span {
position: absolute;
left: 20%; top: 50%;
transform: translate(0,-150%);
background: #333;
color: white;
border-radius: 10px;
box-shadow: 2px 2px 6px rgba(0,0,0,0.5);
padding: 10px;
white-space: nowrap;
opacity: 0;
transition: opacity 0.2s;
border: 4px solid #999;
}
a.has-tooltip span::before {
content: '';
position: absolute;
left: 20%; top: calc(100% - 10px);
height: 20px; width: 20px;
transform: rotate(45deg);
background: inherit;
box-shadow: 2px 2px 4px rgba(0,0,0,0.3);
z-index: -1;
border: inherit;
border-width: 0 4px 4px 0;
}
a.has-tooltip:hover span {
opacity: 1;
}
<br><br><br>
<a href="#" class="has-tooltip" id="link"><span>Hi! I'm a tooltip!</span>
Hi! I'm a link!
</a>
Updated based on a comment
When one make a shadow like this, and as it is actually a square, their 2 shadows intersect
a.has-tooltip {
position: relative;
transition: opacity 0.5s;
}
a.has-tooltip span {
position: absolute;
left: 20%; top: -10%;
transform: translate(0,-150%);
background: #333;
color: white;
border-radius: 5px;
-webkit-box-shadow: 0px 0px 0px 6px rgba(0,0,0,0.50);
-moz-box-shadow: 0px 0px 0px 6px rgba(0,0,0,0.50);
box-shadow: 0px 0px 0px 6px rgba(0,0,0,0.50);
padding: 10px;
white-space: nowrap;
opacity: 0;
transition: opacity 0.2s;
border: 1px solid #FFFFFF;
}
a.has-tooltip span::before {
content: '';
position: absolute;
left: 10%; top: calc(100% - 10px);
height: 20px; width: 20px;
transform: rotate(45deg);
background: inherit;
-webkit-box-shadow: 4px 4px 0px 2px rgba(0,0,0,0.50);
-moz-box-shadow: 4px 4px 0px 2px rgba(0,0,0,0.50);
box-shadow: 4px 4px 0px 2px rgba(0,0,0,0.50);
z-index: -1;
border-right: 1px solid #FFFFFF;
border-bottom: 1px solid #FFFFFF;
}
a.has-tooltip:hover span { opacity: 1; }
<br><br><br><br>
<a href="#" class="has-tooltip" id="link"><span>Hi! I'm a tooltip!</span>
Hi! I'm a link!
</a>
To fix that is almost impossible, unless one use a SVG, though here is 2 possible solutions.
Give the shadow a non transparent color
a.has-tooltip {
position: relative;
transition: opacity 0.5s;
}
a.has-tooltip span {
position: absolute;
left: 20%; top: -10%;
transform: translate(0,-150%);
background: #333;
color: white;
border-radius: 5px;
-webkit-box-shadow: 0px 0px 0px 6px rgba(128,128,128,1);
-moz-box-shadow: 0px 0px 0px 6px rgba(128,128,128,1);
box-shadow: 0px 0px 0px 6px rgba(128,128,128,1);
padding: 10px;
white-space: nowrap;
opacity: 0;
transition: opacity 0.2s;
border: 1px solid #FFFFFF;
}
a.has-tooltip span::before {
content: '';
position: absolute;
left: 10%; top: calc(100% - 10px);
height: 20px; width: 20px;
transform: rotate(45deg);
background: inherit;
-webkit-box-shadow: 4px 4px 0px 2px rgba(128,128,128,1);
-moz-box-shadow: 4px 4px 0px 2px rgba(128,128,128,1);
box-shadow: 4px 4px 0px 2px rgba(128,128,128,1);
z-index: -1;
border-right: 1px solid #FFFFFF;
border-bottom: 1px solid #FFFFFF;
}
a.has-tooltip:hover span { opacity: 1; }
<br><br><br><br>
<a href="#" class="has-tooltip" id="link"><span>Hi! I'm a tooltip!</span>
Hi! I'm a link!
</a>
Use the other pseudo, ::after, as the shadow, which will be transparent. With this one need to play a little with the size of the main shadow and the position of the ::after pseudo.
a.has-tooltip {
position: relative;
transition: opacity 0.5s;
}
a.has-tooltip span {
position: absolute;
left: 20%; top: -10%;
transform: translate(0,-150%);
background: #333;
color: white;
border-radius: 5px;
-webkit-box-shadow: 0px 0px 0px 5px rgba(0,0,0,0.50);
-moz-box-shadow: 0px 0px 0px 5px rgba(0,0,0,0.50);
box-shadow: 0px 0px 0px 5px rgba(0,0,0,0.50);
padding: 10px;
white-space: nowrap;
opacity: 0;
transition: opacity 0.2s;
border: 1px solid #FFFFFF;
}
a.has-tooltip span::before {
content: '';
position: absolute;
left: 10%; top: calc(100% - 10px);
height: 20px; width: 20px;
transform: rotate(45deg);
background: inherit;
z-index: -1;
border-right: 1px solid #FFFFFF;
border-bottom: 1px solid #FFFFFF;
}
a.has-tooltip span::after {
content: '';
position: absolute;
left: calc(10% - 5px); top: calc(100% + 6px);
width: 0;
height: 0;
border-left: 16px solid transparent;
border-right: 16px solid transparent;
border-top: 16px solid rgba(0,0,0,0.50);
z-index: -2;
}
a.has-tooltip:hover span { opacity: 1; }
<br><br><br><br>
<a href="#" class="has-tooltip" id="link"><span>Hi! I'm a tooltip!</span>
Hi! I'm a link!
</a>
<style>
input[type=checkbox]{display:none}
label{color: black;}
input[type=checkbox]:checked + label{color:red;}
</style>
<label><input type="checkbox">Click</label>
I have a checkbox use label to click, when check box clicked, the label should change the color. What I try to achieve is do it without for="id". I place input inside of label, but the color wont change.
Is any way to do this without id?
It's a bit of a hack but you can use something like this:
With input outside label..
<input type="checkbox"><label>Click</label>
input[type=checkbox] {
display: inline-block;
width: 100%;
position: relative;
z-index: 1;
opacity: 0;
}
label {
position: absolute;
top: 8px;
color: black;
}
input[type=checkbox]:checked + label {
color: red;
}
An example: http://jsfiddle.net/96v7C/
If anyone stumbles over this old question I'd like to pick up Vangel Tzo's answer and optimize it a bit. I would recommend to switch position:absolute; and position:relative; for the label and input fields to allow longer text to wrap properly without overlapping any content below it:
.hidden-checkboxes {
font: 14px 'Open Sans', sans-serif;
}
.hidden-checkboxes input[type=checkbox] {
position: absolute;
left: 0;
top: 0;
display: inline-block;
width: 100%;
z-index: 1;
opacity: 0;
height: 30px;
}
.hidden-checkboxes label {
position: relative;
left: 0;
top: 0;
padding: 5px;
display: inline-block;
width: 100%;
}
.hidden-checkboxes input[type=checkbox]:checked + label {
background: #3298dc;
color: white;
}
.hidden-checkboxes div {
position: relative;
margin-bottom: 1px;
}
<div class="hidden-checkboxes" style="width:300px;">
<div>
<input type="checkbox"><label>Check me please.</label>
</div>
<div>
<input type="checkbox"><label>Or how about me?</label>
</div>
<div>
<input type="checkbox"><label>How about some long text that will cause wrapping of text?</label>
</div>
</div>
Remark: Font and margin-bottom are only used for styling the demo.
Here is a compleat radio butons and checkbox example.
jsFiddle
/*style for iinpul checkbox*/
input[type=radio], input[type=checkbox] {
display:none;
}
input[type=radio] + label, input[type=checkbox] + label {
display:inline-block;
line-height: 20px;
color: #333;
text-align: center;
text-shadow: 0 1px 1px rgba(255, 255, 255, 0.75);
vertical-align: middle;
cursor: pointer;
background: -moz-linear-gradient(center top, #FFFFFF 20%, #F6F6F6 50%, #EEEEEE 52%, #F4F4F4 100%) repeat scroll 0 0 padding-box rgba(0, 0, 0, 0);
border: 1px solid #AAAAAA;
border-radius: 5px;
box-shadow: 0 0 3px #FFFFFF inset, 0 1px 1px rgba(0, 0, 0, 0.1);
color: #444444;
display: block;
height: 34px;
line-height: 34px;
overflow: hidden;
position: relative;
text-decoration: none;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
font-size: 13px;
}
input[type=radio]:checked + label, input[type=checkbox]:checked + label {
background-image: none;
outline: 0;
-webkit-box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.15), 0 1px 2px rgba(0, 0, 0, 0.05);
-moz-box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.15), 0 1px 2px rgba(0, 0, 0, 0.05);
box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.15), 0 1px 2px rgba(0, 0, 0, 0.05);
background-color:#e0e0e0;
}
.c-custom-checkbox {
padding-left: 20px;
position: relative;
cursor: pointer;
}
.c-custom-checkbox input[type=checkbox] {
position: absolute;
opacity: 0;
overflow: hidden;
clip: rect(0 0 0 0);
height: 15px;
width: 15px;
padding: 0;
border: 0;
left: 0;
}
.c-custom-checkbox .c-custom-checkbox__img {
display: inline;
position: absolute;
left: 0;
width: 15px;
height: 15px;
background-image: none;
background-color: #fff;
color: #000;
border: 1px solid #333;
border-radius: 3px;
cursor: pointer;
}
.c-custom-checkbox input[type=checkbox]:checked + .c-custom-checkbox__img {
background-position: 0 0;
background-color: tomato;
}
<label class="c-custom-checkbox">
<input type="checkbox" id="valueCheck" />
<i class="c-custom-checkbox__img"></i>Some Label
</label>