independent styles
I have this problem, at the moment of applying the styles they are applied in all the checboxes that are found, I only think that the styles of each checkbox are applied independently and that only 1 can be selected
<style media="screen">
/* Hide the checkbox */
form p {
position: relative; /* allows to position the checkbox */
}
[type="checkbox"]:not(:checked),
[type="checkbox"]:checked {
/* Hide the checkbox without
making it hidden for the
screen readers */
position: absolute;
left: 0;
opacity: 0.01;
}
/* Preparing the label */
[type="checkbox"]:not(:checked) + label,
[type="checkbox"]:checked + label {
position: relative; /* allows to position the custom boxes */
padding-left: 2.3em; /* space for upcoming boxes */
font-size: 1.05em;
line-height: 1.7;
cursor: pointer;
}
/* Box aspect */
[type="checkbox"]:not(:checked) + label:before,
[type="checkbox"]:checked + label:before {
content: '';
position: absolute;
left: 0;
top: 0;
width: 1.4em;
height: 1.4em;
border: 1px solid #aaa;
background: #FFF;
border-radius: .2em;
box-shadow: inset 0 1px 3px rgba(0,0,0, .1), 0 0 0 rgba(203, 34, 237, .2);
transition: all .275s;
}
/* Check aspect */
[type="checkbox"]:not(:checked) + label:after,
[type="checkbox"]:checked + label:after {
content: 'X';
position: absolute;
top: .38em;
left: .18em;
font-size: 1.6em;
color: #CB22ED;
line-height: 0;
transition: all .2s; /* Little transition */
}
/* Unchecked aspect */
[type="checkbox"]:not(:checked) + label:after {
opacity: 0;
transform: scale(0) rotate(45deg);
}
/* Checked aspect */
[type="checkbox"]:checked + label:after {
opacity: 1;
transform: scale(1) rotate(0);
}
</style>
<p>
<input type="checkbox" class="check" id="test1" />
<label for="test1" aria-describedby="label">YES</label>
</p>
<p>
<input type="checkbox" id="test2" />
<label for="test2" aria-describedby="label">NO</label>
</p>
As shown in the example, the styles appear for everyone but I only wish that each one had its content. I expect answers and thank you for your attention.
What you are doing is targeting the HTML attribute <input> with the type of type="checkbox". Therefore, any of those you have on the page will be targeted. However, you can override input[type="checkbox"] with an ID. To define an ID, simply put, on your HTML page. On each HTML tag like <input> or <p> or even <textarea> you can add an ID like this <input id="thisIsWhateverYouWant" type="checkbox"> same goes for the others. To write it in CSS, inside the <style> tags you can write something like this #thisIsWhateverYouWant {height: 100%; width: 100%} Those styles will override what you have.
Here is a reference to what overrides what. Check this out
Related
Until recently youtube used a tag named "paper-toggle-button". My script used that but since youtube removed it I've had to settle with a normal boring checkbox.
I don't just want an answer. I want to learn how it works. It bugs me that this advanced css doesn't click for me yet.
I've been trying to replicate it via tutorials that show in various ways how to make a sliding toggle button. But I'm not satisfied with the look. I want it to look as close to the youtube's toggle button as possible. At least one thing I've learned. The code below doesn't need any pictures which is good.
This requires advanced knowledge of css which I don't have.
Here's an example and it looks ugly. Because it must for instance manually put the label in the correct place. See .labelterm. I gave up when I couldn't use this tutorial code to add a checkmark.
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1">
<title>Awesome checkbox</title>
<style type="text/css">
.mylabel {
position: relative;
display: block;
width: 60px;
height: 30px;
margin-bottom: 15px;
}
.mylabel input {
display: none;
}
.slidinggroove {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: #ababab;
/*background: rgba(0, 0, 0, 0.1);*/
border-radius: 20px;
transition: all 0.3s ease;
}
.slidinggroove:after {
position: absolute;
content: "";
width: 28px;
height: 28px;
border-radius: 50%;
background: #fff;
/*background: rgba(255, 255, 255, 0.2);*/
top: 1px;
left: 1px;
transition: all 0.3s ease;
}
input:checked + .slidinggroove {
background: #5fcf80;
}
input:checked + .slidinggroove:after {
transform: translateX(30px);
}
.labelterm {
margin-left: 65px;
font-size: 16px;
color: #222;
font-family: "Roboto", sans-serif;
position: relative;
top: 5px;
}
</style>
</head>
<body>
<div class="mylabel">
<input type="checkbox" id="coding">
<div class="slidinggroove"></div>
<label class="mylabel" for="coding" name="skills"><p class="labelterm">Test</p></label>
</div>
</body>
</html>
Here's how I would tackle this to achieve the look of the picture shown:
.slidinggroove::before{
position: absolute;
left: 7px;
top: 50%;
transform: translateY(-7px);
width: 20px;
height: 20px;
font-family: "Font Awesome 5 Free";
font-weight: 900;
content: "\f00c";
display: block;
color: #fff;
}
You must import a library for the icon as a font, I recommend using FontAwesome as shown in the working snippet here :
.mylabel {
position: relative;
display: block;
width: 60px;
height: 30px;
margin-bottom: 15px;
}
.mylabel input {
display: none;
}
.slidinggroove {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: #ababab;
/*background: rgba(0, 0, 0, 0.1);*/
border-radius: 20px;
transition: all 0.3s ease;
}
.slidinggroove:after {
position: absolute;
content: "";
width: 28px;
height: 28px;
border-radius: 50%;
background: #fff;
/*background: rgba(255, 255, 255, 0.2);*/
top: 1px;
left: 1px;
transition: all 0.3s ease;
}
.slidinggroove:before {
position: absolute;
left: 7px;
top: 50%;
transform: translateY(-7px);
width: 20px;
height: 20px;
font-family: "Font Awesome 5 Free";
font-weight: 900;
content: "\f00c";
display: block;
color: #fff;
}
input:checked+.slidinggroove {
background: #5fcf80;
}
input:checked+.slidinggroove:after {
transform: translateX(30px);
}
.labelterm {
margin-left: 65px;
font-size: 16px;
color: #222;
font-family: "Roboto", sans-serif;
position: relative;
top: 5px;
}
<html>
<head>
<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1">
<title>Awesome checkbox</title>
</head>
<body>
<div class="mylabel">
<input type="checkbox" id="coding">
<div class="slidinggroove"></div>
<label class="mylabel" for="coding" name="skills"><p class="labelterm">Test</p></label>
</div>
</body>
</html>
The way it works is as follow, you create a pseudo-element that will have as content the unicode of the fontawesome icon. Then you have a complete control hover it (font-size, color, ...).
Appreciate your eagerness of leaning. Let’s break the problem down and tackle each of them then merge them together to have a complete solution. Apparently, we need several things:
An oval shape background for the toggle to move. Lets call it pit
A circle that can be move to left or right. Lets call it knob.
The control need to have its own state, for checked and unchecked. We can make use of an native checkbox
The color of the control will change along the state.
Animation
We start with the basic first. To draw the toggle with html:
<div class="toggle">
<input class="toggle__input" type="checkbox"> <span class="toggle__pit"> <span class="toggle__knob"></span>
</span>
</div>
the toggle serve as the container of the whole toggle. Inside there is a a native checkbox element, toggle__pit and a toggle__knob. Without css they are just white square. Lets style it according to the sample image.
.toggle__pit {
/* set the size */
width: 36px;
height: 16px;
/* set the color */
background-color: #bababa;
border: 1px solid #bababa;
/* set the oval shape, value = height /2 */
border-radius: 8px;
/* border width is part of the element size */
box-sizing: border-box;
/* display span as a inline block element */
/* span is a inline element which cant assign width and height*/
display: inline-block;
}
.toggle__knob {
/* set the size */
width: 14px;
height: 14px;
/* make it circle */
border-radius: 50%;
background-color: white;
display: inline-block;
}
.toggle .toggle__input {
/* keep the checkbox hidden */
display: none;
}
<div class="toggle">
<input class="toggle__input" type="checkbox"> <span class="toggle__pit">
<span class="toggle__knob"></span>
</span>
</div>
Now the toggle looks like the sample image, but it is in unchecked state. We need to make it interactive so it responses to user action. Since the checkbox is hidden, user cannot interactive with it. We can use <label> to associate with the input. In this way even the input is hidden we can toggle it. We need to modify the html a bit, to have the input wrap by the label.
.toggle__pit {
/* set the size */
width: 36px;
height: 16px;
/* set the color */
background-color: #bababa;
border: 1px solid #bababa;
/* set the oval shape, value = height /2 */
border-radius: 8px;
/* border width is part of the element size */
box-sizing: border-box;
/* display span as a inline block element */
/* span is a inline element which cant assign width and height*/
display: inline-block;
}
.toggle .toggle__pit::after {
/* if the checkbox is checked, move knob to right */
content: 'L';
font-family: Helvetica, Arial, Sans-Serif;
color: white;
font-size: 12px;
display: inline-block;
transform: translateX(4px) translateY(-3px) scaleX(-1) rotate(-40deg);
transform-origin: 50% 50%;
}
.toggle__knob {
/* set the size */
width: 14px;
height: 14px;
/* make it circle */
border-radius: 50%;
background-color: white;
display: inline-block;
/* for position it to left or right */
position: absolute;
}
.toggle__label {
/* act as a reference point for knob positioning */
position: relative;
line-height: 16px;
}
.toggle .toggle__input {
/* keep the checkbox hidden */
display: none;
}
.toggle .toggle__input:checked+.toggle__pit {
/* if the checkbox is checked, change pit color */
background-color: #167ac6;
border: 1px solid #167ac6;
}
.toggle .toggle__input:checked+.toggle__pit .toggle__knob {
/* if the checkbox is checked, move knob to right */
left: 21px;
}
<div class="toggle">
<label class="toggle__label"> <input class="toggle__input" type="checkbox"> <span class="toggle__pit">
<span class="toggle__knob"></span>
</span>
</label>
</div>
Finally, to smoothen the state transition, add some transition to toogle__knob and toggle__pit.
.toggle__pit {
/* set the size */
width: 36px;
height: 16px;
/* set the color */
background-color: #bababa;
border: 1px solid #bababa;
/* set the oval shape, value = height /2 */
border-radius: 8px;
/* border width is part of the element size */
box-sizing: border-box;
/* display span as a inline block element */
/* span is a inline element which cant assign width and height*/
display: inline-block;
transition: background-color 0.5s linear, border 0.5s linear;
}
.toggle .toggle__pit::after {
/* use L to mock a tick*/
content: 'L';
font-family: Helvetica, Arial, Sans-Serif;
color: white;
font-size: 12px;
/* pseudo-element is inline by defauly, which you can't apply transform*/
display: inline-block;
/* flip L horizontally and rotate it */
transform: translateX(4px) translateY(-3px) scaleX(-1) rotate(-40deg);
transform-origin: 50% 50%;
}
.toggle__knob {
/* set the size */
width: 14px;
height: 14px;
/* make it circle */
border-radius: 50%;
background-color: white;
display: inline-block;
/* for position it to left or right */
position: absolute;
left: 1px;
/* enable animation */
transition: all 0.5s linear;
}
.toggle__label {
/* act as a reference point for knob positioning */
position: relative;
line-height: 16px;
}
.toggle .toggle__input {
/* keep the checkbox hidden */
display: none;
}
.toggle .toggle__input:checked+.toggle__pit {
/* if the checkbox is checked, change pit color */
background-color: #167ac6;
border: 1px solid #167ac6;
}
.toggle .toggle__input:checked+.toggle__pit .toggle__knob {
/* if the checkbox is checked, move knob to right */
left: 21px;
}
<div class="toggle">
<label class="toggle__label"> <input class="toggle__input" type="checkbox"> <span class="toggle__pit">
<span class="toggle__knob"></span>
</span>
</label>
</div>
im currently stuck with this formating issue in my angular application.
As you can see in the attached images, there is a problem with the clickable area around my checkboxes.
I basically want to adjust the clickable area to the size of the checkbox.
In the 2nd image I highlighted the current area with a background-color: aquamarine to make it more clear.
My SCSS code looks like this. I have a feeling, that this should be an easy task, but I'm somewhat missing something in detail.
.date-checkbox {
display: inline;
float: right;
margin-right: -24px;
margin-top: -23px;
-webkit-transform: scale(2);
}
.disable-date {
opacity: 0.5;
pointer-events: none;
}
.enum-checkbox {
display: inline;
float: right;
margin-right: -24px;
margin-top: -23px;
-webkit-transform: scale(2);
}
/* The container */
.container {
cursor: pointer;
}
/* Hide the browser's default checkbox */
.container input {
opacity: 0;
cursor: pointer;
height: 0;
width: 0;
}
/* Create a custom checkbox */
.checkmark {
position: absolute;
top: 4px;
left: 25px;
height: 15px;
width: 15px;
background-color: blue;
}
/* On mouse-over, add a grey background color */
.container:hover input ~ .checkmark {
background-color: blue;
}
/* When the checkbox is checked, add a blue background */
.container input:checked ~ .checkmark {
background-color: blue;
}
/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
content: "";
position: absolute;
display: none;
}
/* Show the checkmark when checked */
.container input:checked ~ .checkmark:after {
display: block;
}
/* Style the checkmark/indicator */
.container .checkmark:after {
left: 5px;
top: 2px;
width: 5px;
height: 10px;
border: solid white;
border-width: 0 3px 3px 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
}
My HTML code looks like this:
<div class="enum-checkbox">
<label class="container">
<input type="checkbox" (click)="IncludeExcludeProp(groupobject[g.PROPS.title], $event)"
title="Include in search" />
<span class="checkmark"></span>
</label>
</div>
Since clicking the label essentially clicks the checkbox, I would add padding or width to the label surrounding the checkbox so that it extends as far as you need your clickable area to be.
Note that, for accessibility, I've added aria-label to the checkbox. The title attribute is ignored by screen readers and the label would otherwise contain no helpful information about the checkbox.
.lbl-checkbox {
display: inline-block;
padding: 5em;
background: #eee;
}
<label class="lbl-checkbox">
<input aria-label="an appropriate label" type="checkbox">
</label>
I have a checkbox. It looks like this.
It works fine... except that you can check the box by clicking the label. This is problematic for two reasons:
I don't like it
I need the user to be able to click the blue link. Right now, it just checks the box
Here is my current HTML:
<label className="container">I have read and do accept <a href={props.link}>{props.topic}</a>
<input type="checkbox" onChange={event => props.onChange(event)}/>
<span className="checkmark"></span>
</label>
Here is my css, which came (roughly) from here: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_custom_checkbox
/* Hide the browser's default checkbox */
.container input {
position: absolute;
opacity: 0;
cursor: pointer;
}
/* Create a custom checkbox */
.checkmark {
position: absolute;
top: -2px;
left: 0;
height: 21px;
width: 21px;
background-color: #eee;
}
/* On mouse-over, add a grey background color */
.container:hover input ~ .checkmark {
background-color: #ccc;
}
/* When the checkbox is checked, add a blue background */
.container input:checked ~ .checkmark {
background-color: rgb(29, 29, 29);
}
/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
content: "";
position: absolute;
display: none;
}
/* Show the checkmark when checked */
.container input:checked ~ .checkmark:after {
display: block;
}
/* Style the checkmark/indicator */
.container .checkmark:after {
left: 8px;
top: 4px;
width: 4px;
height: 9px;
border: solid white;
border-width: 0 2px 2px 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
}
Any thoughts? You can actually play with it in the W3C pen I provided.
.container {
pointer-events: none;
}
.checkmark {
pointer-events: auto;
}
.container a {
pointer-events: auto;
}
First, you can pull the checkbox into it's own container, then, if you want the label to semantically pertain to that specific input, you have to assign it a for attribute, and assign a corresponding id attribute to the input field. Now, you have the best of both worlds. Link is clickable, while the rest of the label checks the checkbox.
<div class="checkbox-container">
<input type="checkbox" id="checkbox">
<span className="checkmark"></span>
</div>
<label class="container" for="checkbox">
I have read and do accept the terms and conditions
</label>
Looks like you've figured out the custom checkbox UI part already, so I'll leave that to you.
I have an issue with my radio buttons, I try to put a border color when it is checked, nothing happens. I tried to read other topics about it, even tried to paste the answers I've found but it still doesn't change the border.
It's probably some silly mistake that I made but I just can't find it, does anyone have the answer?
Thanks a lot.
input[type="radio"]:checked:before {
background: green;
}
input[type="radio"]:checked {
border-color: orange;
}
<div id="radio">
<label>
<input type="radio" name="sexe" value="Homme" id="homme">
Homme
</label>
<label>
<input type="radio" name="sexe" value="Femme" id="femme">
Femme
</label>
</div>
You can not really change the style of basic radio button.
You have to create a custom radio button css.
Try this css:
input[type='radio'] {
-webkit-appearance: none;
width: 20px;
height: 20px;
border-radius: 50%;
outline: none;
border: 3px solid gray;
}
input[type='radio']:before {
content: '';
display: block;
width: 60%;
height: 60%;
margin: 20% auto;
border-radius: 50%;
}
input[type="radio"]:checked:before {
background: green;
}
input[type="radio"]:checked {
border-color: orange;
}
It works for me. I hope I can help.
Apparently, browsers don't allow much custom styling on checkboxes/radio buttons. - Jeremy Thille's comment
You could however, create your own radio button through css, an example of this can be found in this JsFiddle
What happens here:
We hide the borswer's radio input
We style create a custom radio button through css .checkmark
We show / hide a custom checked indicator using :checked, :after and the ~ General sibling combinator
Lastly, we style the checked indicator
Example found here
NOTE, as this is an example, it may be more than you require
The code
/* The container */
.container {
display: block;
position: relative;
padding-left: 35px;
margin-bottom: 12px;
cursor: pointer;
font-size: 22px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* Hide the browser's default radio button */
.container input {
position: absolute;
opacity: 0;
cursor: pointer;
}
/* Create a custom radio button */
.checkmark {
position: absolute;
top: 0;
left: 0;
height: 25px;
width: 25px;
background-color: #eee;
border-radius: 50%;
}
/* On mouse-over, add a grey background color */
.container:hover input ~ .checkmark {
background-color: #ccc;
}
/* When the radio button is checked, add a blue background */
.container input:checked ~ .checkmark {
background-color: #2196F3;
}
/* Create the indicator (the dot/circle - hidden when not checked) */
.checkmark:after {
content: "";
position: absolute;
display: none;
}
/* Show the indicator (dot/circle) when checked */
.container input:checked ~ .checkmark:after {
display: block;
}
/* Style the indicator (dot/circle) */
.container .checkmark:after {
top: 9px;
left: 9px;
width: 8px;
height: 8px;
border-radius: 50%;
background: white;
}
<label class="container">Homme
<input type="radio" checked="checked" name="sexe">
<span class="checkmark"></span>
</label>
<label class="container">Femme
<input type="radio" checked="checked" name="sexe">
<span class="checkmark"></span>
</label>
Hope this helps getting to your desired result
I have a css only menu, a back background must slide with animation to selected menu item:
.wrapper {
font: 0/0 a;
position: relative;
white-space: nowrap;
}
.radio {
-webkit-appearance: none;
color: rgb(0, 0, 0);
margin: 0 10px 0 0;
outline: 0;
padding-bottom: 10px;
padding-top: 10px;
position: relative;
text-align: center;
transition: color .5s linear;
z-index: 2;
}
.radio:after {
content: attr(data-text);
}
.radio:checked {
color: rgb(255, 255, 255);
}
.bg {
background: black;
border-radius: 10px;
bottom: 0;
position: absolute;
top: 0;
transition: left .25s linear;
z-index: 1;
}
.radio,
.bg {
width: 100px;
}
.radio:checked + .radio + .radio + .radio + .radio + .bg {
left: 0;
}
.radio + .radio:checked + .radio + .radio + .radio + .bg {
left: 110px;
}
.radio + .radio + .radio:checked + .radio + .radio + .bg {
left: 220px;
}
.radio + .radio + .radio + .radio:checked + .radio + .bg {
left: 330px;
}
.radio + .radio + .radio + .radio + .radio:checked + .bg {
left: 440px;
}
<div class="wrapper">
<input type="radio" class='radio' name="r" data-text="Save" checked>
<input type="radio" class='radio' name="r" data-text="Fork">
<input type="radio" class='radio' name="r" data-text="Info">
<input type="radio" class='radio' name="r" data-text="Share">
<input type="radio" class='radio' name="r" data-text="Edit">
<i class="bg"></i>
</div>
I used radio controls and its checked state in selectors and render the text by pseudo-elements of radio. For radio I used -webkit-appearance: none and fixed width.
The main point of this menu - stay css only. And my problems are:
How can I make menu more cross browser compatibility? (now works for webkit only)
I don't want to use fixed width of navigation elements
Code source for more experiments.
How about adding the appearance css für the other browsers?
.thing {
-webkit-appearance: value;
-moz-appearance: value;
appearance: value;
}
Code from here
This is a neat affect, but I'm afraid a flexible solution (one that allowed an infinite number of these elements with varying widths) would require some javascript.
At the moment you can't.
apperance is not supported in all major browsers. (You can add -moz-apperance to your style to work in FF, but thats only FF.)
Bigger problem is, according to W3C Recommendation :after pseudo-element should not work with inputs. :after is targeting the element's document tree content, what inputs don't have. You had 'luck' with chrome, they are not following specs, but can't expect other browsers ignoring them the same way.