Custom radio button w3s example - css

There is a custom radio buttons example at the w3schools.com:
https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_custom_radio
I wonder, what's the reason to have separate .container .checkmark:after statement? Why these styles are not included directly to .checkmark:after statement?
Full code example:
/* 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;
}
/* 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;
}
<h1>Custom Radio Buttons</h1>
<label class="container">One
<input type="radio" checked="checked" name="radio">
<span class="checkmark"></span>
</label>
<label class="container">Two
<input type="radio" name="radio">
<span class="checkmark"></span>
</label>
<label class="container">Three
<input type="radio" name="radio">
<span class="checkmark"></span>
</label>
<label class="container">Four
<input type="radio" name="radio">
<span class="checkmark"></span>
</label>

There's no real reason why this happens, I suppose w3schools has the following motives:
The first rule with .checkmark:after doesn't really give any markup other than setting the content, position and display. This is merely a preparation for styling, which can be applied to every checkbox on the website without consequences if other styling is being changed.
In this example, there's only one area that contains styled inputs, but in a large website multiple input fields might be available. It's not a problem to apply an invisible pseudo element (modern browsers don't fully render display: none on page load anyway), but you can control which inputs are actually styled this way.
It's a matter of semantics, separating the actual styling and logic.

In the above example you can just omit ".container" because the only radio buttons present are also enclosed within the container class label so the same CSS styling is applied.
But what if the case is, you want to have different styles applied and the radio buttons are not placed within the same container class?
If you make use of "input:checked ~ .checkmark:after" without mentioning in which container specifically it is present, the styling would be just applied to all the elements of the type input and not just the ones inside the container class.

Related

How to change the checked colour of the checkbox?

I want to change the color of my checked checkbox to green.And the size should aloso change.But in my code sizes are changing but the color doesn't. I tried using :checked:after also
input[type='checkbox']{
width: 16px !important;
height: 16px !important;
border-radius: 2px;
border: 1px solid #7A7A9D;
box-sizing: border-box;
}
input[type='checkbox']:checked{
width: 100px !important;
height: 100px !important;
margin: 5px;
color: red;
}
Checkboxes are not able to be styled. You would need a third party js plugin there are many available.
If you want to do this yourself it basically involves hiding the checkbox creating an element and styling that as you want then binding its click event to two functions one to change its look and another to activate the click event of the checkbox.
The same problem will arise when trying to style that little down arrow on a drop-down select element.
Checkboxes are generally not considered stylable, but there are a lot of good ways to cheat. W3C's Custom Check Boxes and Radio Buttons is a good place to start. It also looks like a related SO Question has a number of useful links.
I would use the more advanced CSS "+" selector
Also, using !important in CSS is usually asking for trouble. there are a few rare instances where it is helpful, but overall it usually just causes trouble.
To directly answer your question:
input[type='checkbox'] {
display: none;
}
input[type='checkbox']+span::before {
content: "";
display: inline-block;
width: 16px;
height: 16px;
border-radius: 2px;
border: 1px solid #7A7A9D;
box-sizing: border-box;
}
input[type='checkbox']:checked+span::before {
width: 100px;
height: 100px;
margin: 5px;
background: green;
}
<label>
<input type="checkbox">
<span>Eggs</span>
</label>
<label>
<input type="checkbox">
<span>Cheese</span>
</label>
<label class="custom-checkbox">
<input type="checkbox">
<span >Bacon</span>
</label>
You can use this trick, i hope it's can solve your problem
span {
color: grey; /* text colour before klick */
}
label > input[type="checkbox"] {
display: none;
}
label > input[type="checkbox"] + *::before {
content: "";
display: inline-block;
vertical-align: bottom;
width: 1rem;
height: 1rem;
border-radius: 10%;
border-style: solid;
border-width: 0.1rem;
border-color: gray;
}
label > input[type="checkbox"]:checked + *::before {
content: "✓"; /* you can cange check logo here */
transform: scale(0.7); /* this is for check size */
color: green; /* check colour */
text-align: center;
background: white; /* check background colour */
border-color: white; /* check border colour */
}
label > input[type="checkbox"]:checked + * {
color: green;
}
<label>
<input type="checkbox" name="key" value="value" />
<span>I am a checkbox</span>
</label>

How do i make the last char red in input placeholder?

This is my input field , i want to change the last char '*' to be red instead of transparent black.
I have tried a lot of different approaches but none of them works, any help would be appreciated.
You can give the placeholder another color using the ::placeholder selector. However, to give only one letter another color is not possible.
You can achieve what you want by creating a custom input element, using HTML and CSS.
/**
* Just a basic idea of how you can achieve what you want using HTML and CSS only.
*/
.input {
background-color: #fff;
border-radius: 3px;
position: relative;
display: inline-block;
border: 1px solid #888;
}
input {
background-color: transparent;
position: relative;
padding: 5px;
z-index: 1;
border: none;
}
.placeholder {
position: absolute;
color: #ccc;
left: 5px;
top: 50%;
transform: translateY(-50%)
}
/* Add the asterix when the input is required */
input:required + .placeholder::after {
content: "*";
color: red;
}
/* Hide the placeholder when the user wants to fill the input */
input:focus + .placeholder {
visibility: hidden;
opacity: 0;
}
<div class="input">
<input type="text" name="name" required />
<span class="placeholder">Your name</span>
</div>
Remove the placeholder="" from your input and place the text inside the custom span element with the classname placeholder.
When the input is required the CSS will append a asterix.

Icons rendering is very slow in Edge browser

In our website, we are showing icons for stylized check boxes. The icons are rendered from SVG file. These icons are loaded very slowly in Edge browser (almost 10 seconds). It works fine in IE and Chrome. Please suggest some options to improve the image loading time for Edge. PFB the screenshot with check boxes.
Implementation details
The HTML elements corresponding to the checkbox are added dynamically while getting response from web service. Logic is implemented in pure JS and styles are done using SASS.
Since you are using a web service to populate the checkbox, at first we need to improve the performance, you could try to use F12 developer tool or set timer to check it. Besides, you could also optimize Image Usage, and try to use asynchronous methods to call the web service.
Besides, in my opinion(since you didn't post the related code), I suggest you could try to use CSS or Javascript to create the custom checkbox with check mark, instead of using SVG, because, by using the CSS style, when add the new checkbox, we could just add the related CSS style for it.
You could refer to the following code:
<script src="Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(function () {
$("#btnAdd").click(function () {
//call the web service to get the checkbox items.
//add new checkbox items with CSS style.
$("#dynamiccontent").append("<label class='container'>TwoTwo<input type = 'checkbox' > <span class='checkmark'></span> </label >");
});
});
</script>
Html code:
<input type="button" id="btnAdd" value="Add"/>
<div id="dynamiccontent">
<label class="container">
One
<input type="checkbox" checked="checked">
<span class="checkmark"></span>
</label>
<label class="container">
Two
<input type="checkbox">
<span class="checkmark"></span>
</label>
</div>
CSS style:
<style type="text/css">
/* Customize the label (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 checkbox */
.container input {
position: absolute;
opacity: 0;
cursor: pointer;
height: 0;
width: 0;
}
/* Create a custom checkbox */
.checkmark {
position: absolute;
top: 0;
left: 0;
height: 25px;
width: 25px;
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: #2196F3;
}
/* 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: 9px;
top: 5px;
width: 5px;
height: 10px;
border: solid white;
border-width: 0 3px 3px 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
}
</style>
The result like this:
Reference: How TO - Custom Checkbox

unclickable checkbox - css only

I have been working on customize a checkbox. I am not able to unclick the checkbox since I modified to be rounded.
What's wrong with the following codes?
In Html,
<div class="checkbox-circle">
<input id="checkbox-1" type="checkbox" checked />
<label for="checkbox-1">Checkbox 1</label>
</div>
In CSS,
.checkbox-circle input[type="checkbox"] {
display: none;
}
.checkbox-circle label {
display: inline-block;
cursor: pointer;
position: relative;
padding-left: 25px;
margin-right: 12px;
}
.checkbox-circle label:before {
content: '';
display: inline-block;
width: 15px;
height: 15px;
position: absolute;
left: 0;
border: 1px solid #cccccc;
border-radius: 3px;
}
.checkbox-circle input[type="checkbox"]:checked + label:before {
font-size: 13px;
color: #5bc0de;
text-align: center;
line-height: 11px;
font-family: 'Glyphicons Halflings';
content: "\e013";
}
It's showing as what I wanted but not clickable at all. Thanks in advance -css only.
The problem is caused by the fact you are using display:none to hide the checkbox and it doesn't work on all environments.
I suggest you a safer implementation with this code:
.checkbox-circle input[type="checkbox"] {
opacity: 0; // instead of display:none
position: absolute;
top: 0;
left: 0;
height: 20px;
width: 20px;
z-index: 2; // or above
}
the concept is having the user clicking a real -invisible- checkbox and propagating the style using the :checked selector as you did.
You didn't modify the checkbox; you hid the checkbox and added your own checkbox-like element. Because the checkbox is hidden you can't click on it, therefore the state won't change.
If you want to do it this way then you will need to add some Javascript to link the state of the hidden checkbox to your custom checkbox element and to change the state when your element is clicked on.
Edit: Actually I'm wrong about this, I forgot that clicking on the label can toggle the state of the checkbox, oops. It still might be easier to use Javascript than to hide the checkbox though.
It's very easy with CSS :
.noclick {pointer-events:none;}
<input class="noclick type="checkbox">

CSS Custom Radiobutton without text

Im searching the best way to style a custom radio button with pure CSS but without a label or label text.
At the moment I did something like that: (without the different states for checked, disabled etc)
input[type=radio] {
width : 28px;
margin : 0;
padding : 0;
opacity : 0;
& + label {
background: url("radio_unselected.png") no-repeat 0 2px;
padding-left : 28px;
line-height : 24px;
cursor: pointer;
}
That works perfectly as long as I got a label for my radios. As soon as I remove the label OR remove the label text, nothing is displayed. What is the best way to achieve my goal. Should I remove the background styles from the & + label and add it as background of the input itself and adjust the position so the radio would overlap the default radio style?
Ty for your help,
Andy
EDIT:
I added at least
&:before {
content:"";
display: inline-block;
}
Now I can actually add a label without text, but still not the perfect solution because a label tag is required.
I ran into the same dilemma. I want to use radio buttons as a pagination control for an image carousel. I came up with this:
input[type=radio] {
visibility: hidden;
position: relative;
margin-left: 5px;
width: 20px;
height: 20px;
}
input[type=radio]:before {
content: "";
visibility: visible;
position: absolute;
border: 2px solid #eb6864;
border-radius: 50%;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
input[type=radio]:checked:before {
background-color: #eb6864;
}
This works without any extra markup. Here's the example I'm using for my carousel:
<input type='radio' name='test'>
<input type='radio' name='test' checked>
<input type='radio' name='test'>
<input type='radio' name='test'>
<input type='radio' name='test'>
See this fiddle. I've only tested this in a recent build of Chrome; I have no idea how compatible it is.
Try this:
HTML
<label>
<input type="radio" value="1">
<div></div>
</label>
CSS
input[type="radio"] {
display: none;
}
input[type="radio"] + div {
height: 20px;
width: 20px;
display: inline-block;
cursor: pointer;
vertical-align: middle;
background: #FFF;
border: 1px solid #d2d2d2;
border-radius: 100%;
}
input[type="radio"] + div:hover {
border-color: #c2c2c2;
}
input[type="radio"]:checked + div {
background:gray;
}
DEMO: http://jsfiddle.net/nuzhysgg/
It seems like there is no perfect solution. You actually need to have an element like the label or a div/etc tag to change the radio style. One possibility is to build your radios with a label around the input field:
<label>
<input type="radio"....../>
</label>
Then you can actually try to add the styling through css. Not the best solution in my opinion, but it makes the styling a lot easier.

Resources