What makes HTML element animate on click event without JavaScript? - html

My question may be stupid but please I need an explanation. I found both html and CSS code to implement ON and OFF switcher but I really don't understand how the label animate is possible without javaScript using a click event.
can someone explain to me the trick in this code.
I have never seen this before
HTML:
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked>
<label class="onoffswitch-label" for="myonoffswitch">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
CSS:
<style>
.onoffswitch {
position: relative; width: 109px;
-webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
}
.onoffswitch-checkbox {
display: none;
}
.onoffswitch-label {
display: block; overflow: hidden; cursor: pointer;
height: 30px; padding: 0; line-height: 16px;
border: 2px solid #999999; border-radius: 3px;
background-color: #EEEEEE;
transition: background-color 0.3s ease-in;
}
.onoffswitch-label:before {
content: "OFF";
display: block; width: 16px; margin: 0px;
background: #FFFFFF;
position: absolute; top: 0; bottom: 0;
right: 91px;
border: 2px solid #999999; border-radius: 5px;
padding:5px;
width:30px;
text-align:center;
transition: all 0.3s ease-in 0s;
}
.onoffswitch-checkbox:checked + .onoffswitch-label {
background-color: #00CED1;
}
.onoffswitch-checkbox:checked + .onoffswitch-label, .onoffswitch-checkbox:checked + .onoffswitch-label:before {
border-color: #00CED1;
}
.onoffswitch-checkbox:checked + .onoffswitch-label:before {
right: 0px;
content: "ON";
padding:5px;
width:30px;
text-align:center;
}
</style>
Thank you.

When a label element is clicked, it activates its labelled element. In this case the labelled element is a checkbox.
This causes the checkbox to toggle its checked property (not attribute*).
The :checked pseudo-selector will match input[type="checkbox"] or input[type="radio"] elements that have their checked property in the true state.
The change in the :checked status allows the new CSS properties to be applied to the label because the entire selector chain subsequently matches (or no longer matches, depending on whether the checkbox is now checked or unchecked).
The CSS transition rule then animates the changed properties from one state to the other.
Here's a simple demo of a form that can be used to test the functionality of labels on various different field types. Note that the submit button won't work due to the form being in a sandboxed <iframe>.
<form action="http://example.com" target="_blank" method="get">
<p>
<label for="text">text</label>
<input type="text" name="text" id="text">
</p>
<p>
<label for="select">select</label>
<select name="select" id="select">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
</p>
<p>
<label for="textarea">textarea</label>
<textarea name="textarea" id="textarea" cols="30" rows="10"></textarea>
</p>
<p>
<label for="checkbox">checkbox</label>
<input type="checkbox" name="checkbox" id="checkbox" value="checked">
</p>
<fieldset>
<legend>radio buttons</legend>
<p>
<label for="radio-1">radio 1</label>
<input type="radio" name="radio" id="radio-1" value="1">
</p>
<p>
<label for="radio-2">radio 2</label>
<input type="radio" name="radio" id="radio-2" value="2">
</p>
</fieldset>
<p>
<label for="reset">reset</label>
<input type="reset" id="reset" value="Reset">
</p>
<p>
<label for="button">button</label>
<button id="button" type="submit">Submit</button>
</p>
</form>
* so why is it important that the property is changed but not the attribute? When you use a reset button (input[type="reset"], or button[type="reset"]), the form will be reset to whatever the values of the HTML attributes (or innerHTML for <textarea>) are for each field. You can test this by using JavaScript to modify the value attribute of a text field and then resetting the form to see that no change happens.

Related

I'm trying to put a <button> inside an <input type="radio">'s <label>

Edit: The original answer did not work for mobile devices. Go here for a solution that does.
I already know how to make an <input type="radio">'s <label> look like the button of any specific browser's default <button> by playing with the CSS until it looks identical to the <button> I'm trying to replicate. But this will look out of place whenever someone views it from a browser that has a different default <button>. (Even if I could replicate every default <button> for every browser, a new one will probably be invented tomorrow.)
Therefore, I want to use an actual button to get the default styling appropriate to the browser.
A recreation of the code so far:
<h1>Choose A or B</h1>
<label><button type="button"><input type="radio" name="choice" value="A">A</button></label>
<label><button type="button"><input type="radio" name="choice" value="B">B</button></label>
Later I'll change <input type="radio" name="choice" value="A"> to <input type="radio" name="choice" value="A" hidden> and use some other styling to show if it's checked or not, but for now I'm leaving it in for diagnostic reasons.
If you run the snippet, you'll notice that clicking the <input type="radio"> works as intended, but clicking the <button>itself does nothing.
Here is another failed attempt:
<h1>Choose A or B</h1>
<input type="radio" name="choice" value="A" id="a"><label for="a"><button type="button">A</button></label>
<input type="radio" name="choice" value="B" id="b"><label for="b"><button type="button">B</button></label>
I even tried adding the disabled attribute. Nothing is working as intended. Should I give up and style the <label> manually, or is there a way to access <button>'s default appearance anyway?
(Yes, I already know I could use javascript to simulate <input type="radio">'s behaviour on a <button> but I'm going to have lots of buttons in lots of groups throughout the website. So I'll just style <label> manually if it means the website will be easier to maintain. Likewise for installing an entire library just for this one problem.)
An idea is to add pointer-events:none; to the button but you won't have the styles of the :focus,:active and :hover state.
button {
pointer-events:none;
}
<h1>Choose A or B</h1>
<input type="radio" name="choice" value="A" id="a"><label for="a"><button type="button">A</button></label>
<input type="radio" name="choice" value="B" id="b"><label for="b"><button type="button">B</button></label>
You can, however, add your own custom :focus :active :hover and :checked state with:
input[type="radio"]:focus + label button{
/*add checked style here*/
label:hover > button {
/*add hover style here*/
}
label:active > button {
/*add active style here*/
}
input[type="radio"]:checked + label button{
/*add checked style here*/
It will not work, as you are doing. You need to do it like this.
[type="radio"]:checked,
[type="radio"]:not(:checked) {
position: absolute;
left: -9999px;
}
[type="radio"]:checked + label,
[type="radio"]:not(:checked) + label
{
position: relative;
padding-left: 28px;
cursor: pointer;
line-height: 20px;
display: inline-block;
color: #666;
}
[type="radio"]:checked + label:before,
[type="radio"]:not(:checked) + label:before {
content: '';
position: absolute;
left: 0;
top: 0;
width: 100px;
height: 30px;
border: 1px solid #ddd;
border-radius: 5px;
background: #fff;
z-index: -1;
}
[type="radio"]:checked + label:after,
[type="radio"]:not(:checked) + label:after {
content: '';
width: 94px;
height: 24px;
background: #F87DA9;
position: absolute;
top: 4px;
left: 4px;
border-radius: 5px;
-webkit-transition: all 0.2s ease;
transition: all 0.2s ease;
z-index: -1;
}
[type="radio"]:not(:checked) + label:after {
opacity: 0;
-webkit-transform: scale(0);
transform: scale(0);
}
[type="radio"]:checked + label:after {
opacity: 1;
-webkit-transform: scale(1);
transform: scale(1);
}
<form action="#">
<p>
<input type="radio" id="test1" name="radio-group" checked>
<label for="test1">Apple</label>
</p>
<p>
<input type="radio" id="test2" name="radio-group">
<label for="test2">Peach</label>
</p>
<p>
<input type="radio" id="test3" name="radio-group">
<label for="test3">Orange</label>
</p>
</form>
You can do it using CSS :before and :after pseudo-classes without using any button. You can modify the above example as per your requirements.
Code is copied from https://codepen.io/manabox/pen/raQmpL and is used after modifications in the above example.

How to modify the styles of the values of the radio buttons in CSS?

I am completely new to programming and I am trying to make a simple survey page to start. I am only using CSS and HTML. I have made radio buttons but I am not sure how to 'select' them in CSS.
Below is my HTML code. I would like to style the questions that are in element <p> but I want to do them all differently. I know I can select p {'how I want font, etc.. styled here} and then style in CSS but I want them all slightly different colors. When I try .survey-question-1 p {'how I want font styled here'} nothing happens.
I really don't know what selectors to use to call the elements I want to change.
<div class='survey-name'>
First name: <input type='text' id= 'firstname' name='FirstName'><br>
Last name: <input type='text' name='LastName'><br>
</div>
<div class='survey-question-1'>
<p>Are you a Front-End or Back-End Developer?</p>
<input type='radio' name='developer' value='Front-End'> Front-End<br>
<input type='radio' name='developer' value='Back-End'> Back-End<br>
</div>
<div class='survey-question-2'>
<p>How many years of experience do you have?</p>
<input type='radio' name='years' value='less than 1'> less than 1<br>
<input type='radio' name='years' value='1-2'> 1-2<br>
<input type='radio' name='years' value='2-3'> 2-3<br>
<input type='radio' name='years' value='3-4'> 3-4<br>
<input type='radio' name='years' value='4-5'> 4-5<br>
<input type='radio' name='years' vale='more than 5'> more than 5<br>
</div>
A good practice is to label your radio buttons. (See MDN page for labels). So I assume you will change your markup accordingly.
Secondly you probably want to use the attribute selector to target the radio buttons. You can also use the :checked pseudo selector for styling the checked radio button.
And thirdly, to style radio buttons you might need to apply appearance: none.
.survey-question-1 input[type="radio"] {
appearance: none;
-moz-appearance: none;
-webkit-appearance: none;
background: pink;
border: 0.5ex solid pink;
border-radius: 100%;
width: 1em;
height: 1em;
}
.survey-question-1 input[type="radio"]:checked {
background: rebeccapurple;
}
<form>
<fieldset class='survey-question-1'>
<legend>
Are you a Front-End or Back-End Developer?
</legend>
<label>
<input type='radio' name='developer' value='Front-End'>
Front-End
</label>
<label>
<input type='radio' name='developer' value='Back-End'>
Back-End
</label>
</fieldset>
</form>
Bear in mind, this is a hideous design, but it will show you how to change the color/styling of every single component on your page.
Let me know if there is specific styling you were after or if something is unclear.
/* Style Survey Name section */
.survey-name {
color: green;
}
.survey-name input {
border: 1px solid green;
}
/* Style Survey Q1 section */
.survey-question-1,
.survey-question-1 p {
color: red;
}
.survey-question-1 input[type='radio']:after {
width: 15px;
height: 15px;
border-radius: 15px;
top: -2px;
left: -1px;
position: relative;
background-color: white;
content: '';
display: inline-block;
border: 2px solid gray;
}
.survey-question-1 input[type='radio']:checked:after {
width: 15px;
height: 15px;
border-radius: 15px;
top: -2px;
left: -1px;
position: relative;
background-color: red;
content: '';
display: inline-block;
visibility: visible;
border: 2px solid gray;
}
/* Style Survey Q2 section */
.survey-question-2,
.survey-question-2 p {
color: blue;
}
.survey-question-2 input[type='radio']:after {
width: 15px;
height: 15px;
border-radius: 15px;
top: -2px;
left: -1px;
position: relative;
background-color: white;
content: '';
display: inline-block;
border: 2px solid gray;
}
.survey-question-2 input[type='radio']:checked:after {
width: 15px;
height: 15px;
border-radius: 15px;
top: -2px;
left: -1px;
position: relative;
background-color: blue;
content: '';
display: inline-block;
visibility: visible;
border: 2px solid gray;
}
<div class='survey-name'>
First name: <input type='text' id='firstname' name='FirstName' /><br />
Last name: <input type='text' name='LastName' /><br />
</div>
<div class='survey-question-1'>
<p>Are you a Front-End or Back-End Developer?</p>
<input type='radio' name='developer' value='Front-End' /> Front-End<br>
<input type='radio' name='developer' value='Back-End' /> Back-End<br>
</div>
<div class='survey-question-2'>
<p>How many years of experience do you have?</p>
<input type='radio' name='years' value='less than 1' /> less than 1<br>
<input type='radio' name='years' value='1-2' /> 1-2<br>
<input type='radio' name='years' value='2-3' /> 2-3<br>
<input type='radio' name='years' value='3-4' /> 3-4<br>
<input type='radio' name='years' value='4-5' /> 4-5<br>
<input type='radio' name='years' vale='more than 5'> more than 5<br>
</div>
See also JSFiddle
Resources I used:
Radio Button Styling
:After CSS

displaying a different background when a radio button is checked css and knockout

I've seen some tricks to change the background color (or other css attributes) on a group of radio buttons. Here is some html
<div class="myclass col-xs-3">
<input type="radio" name="mygroup" value="one" data-bind="checked: SelectedAttributeValueId" />
</div>
<div class="myclass col-xs-3">
<input type="radio" name="mygroup" value="two" data-bind="checked: SelectedAttributeValueId" />
</div>
<div class="myclass col-xs-3">
<input type="radio" name="mygroup" value="three" data-bind="checked: SelectedAttributeValueId" />
</div>
I've tried things like:
.myclass input[type="radio"]:checked{
background-color:#f2f2f2;
}
and
.myclass :checked{
background-color:#f2f2f2;
}
here is a fiddle link. I am using knockout, so maybe this is the tool I should use to style the <div> elements?
All input is appreciated, I would prefer not to use jquery or javscript here (although knockout is okay)
It is not possible to style the radio buttons circle.
However, you can use pseudo-elements (in this case :before) to render a box around the radio button, then style it in CSS.
input[type="radio"] {
display: inline-block;
position: relative;
width: 25%;
margin: 0;
}
input[type="radio"]:before {
content: '';
position: absolute;
z-index: -1;
top: -.5em;
right: 0;
bottom: -.5em;
left: 0;
border: 1px solid black;
background-color: #0073ae;
}
input[type="radio"]:checked:before {
background-color: #f2f2f2;
}
<input type="radio" name="mygroup" value="one" /><input
type="radio" name="mygroup" value="two" /><input
type="radio" name="mygroup" value="three"/>
Here's a solution via jquery.
$('[type=radio]').click(function(){
if($(this).val() == "one") {
$('.myclass').css("background-color", "yellow");
}
//...two...three
});

How to make (custom) radio buttons work in CSS3

I've been trying to make custom radio buttons work. I had been using check boxes but found that I needed to restrict the checked options to one. I've been looking at examples/tutorials that I found using Google and thought I understood enough for a simple set of 4 radio buttons but ...
They display correctly initially with the first button checked but checking on other buttons just displays the checked PNG: a previously checked button does not revert to unchecked state.
The buttons are arranged sequentially horizontally in their own div.
HTML
<div class='radio'>
<input id='B12' type='radio' class='radiobutton' checked>
<label id='lblB12' class='radiobutton-label' for='B12'>IR </label>
<input id='BBW' type='radio' class='radiobutton' >
<label id='lblBBW' class='radiobutton-label' for='BBW'>Wide</label>
<input id='B10' type='radio' class='radiobutton' >
<label id='lblB10' class='radiobutton-label' for='B10'>B10</label>
<input id='B8' type='radio' class='radiobutton' >
<label id='lblB8' class='radiobutton-label' for='B8'>B8 </label>
</div>
CSS3
.radiobutton-label {
display: inline-block;
cursor: pointer;
position: relative;
padding-left: 25px;
margin-right: 15px;
font-size: 15px;
}
input[type="radio"] {
display: none;
margin: 10px;
}
.radiobutton-label:before {
content:"";
display: inline-block;
width: 24px;
height: 24px;
margin-right: 10px;
position: absolute;
left: 0;
bottombottom: 1px;
background: url(resources/CheckBoxUnchecked.png) left top;
}
input[type=radio]: + label:before {
background: url(resources/CheckBoxUnchecked.png) left top;
}
input[type=radio]:checked + label:before {
background: url(resources/CheckBoxOK.png) left top;
}
This is the first web page that I have attempted.
Relevant Spec - 17 Forms / 17.2.1 Control types
Radio buttons are like checkboxes except that when several share the same control name, they are mutually exclusive: when one is switched "on", all others with the same name are switched "off".
Therefore if you want the radio elements to be mutually exclusive, just give them all the same name attribute. In this instance, I just used name="checkboxes".
Updated HTML EXAMPLE HERE
<div class='radio'>
<input id='B12' type='radio' class='radiobutton' name="checkboxes" checked="checked"/>
<label id='lblB12' class='radiobutton-label' for='B12'>IR </label>
<input id='BBW' type='radio' class='radiobutton' name="checkboxes"/>
<label id='lblBBW' class='radiobutton-label' for='BBW'>Wide</label>
<input id='B10' type='radio' class='radiobutton' name="checkboxes"/>
<label id='lblB10' class='radiobutton-label' for='B10'>B10</label>
<input id='B8' type='radio' class='radiobutton' name="checkboxes"/>
<label id='lblB8' class='radiobutton-label' for='B8'>B8 </label>
</div>
Base CSS:
input[type=radio] + label:before {
background: url('http://www.csscheckbox.com/checkboxes/vlad.png') 2px 4px no-repeat;
}
input[type=radio]:checked + label:before {
background: url('http://www.csscheckbox.com/checkboxes/vlad.png') 2px -18px no-repeat;
}

Why can't I tab into CSS3 checkboxes?

I've been following tutorial on styling checkboxes using only CSS3 and here's what I came up with:
DEMO:
http://cssdeck.com/labs/jaoe0azx
Checkboxes are styled just fine - but when I tab through form controls -> checkbox is being skipped. Any advice why?
HTML:
<form role="form" id="login_form" data-mode="login">
<div class="form-group">
<label for="ue">Username or email:</label>
<input type="email" class="form-control input-lg" name="ue" id="ue" placeholder="" />
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control input-lg" name="password" id="password" placeholder="" />
</div>
<div>
<input id="rememberme" type="checkbox" name="rememberme" value="1" class="checkbox_1" tabindex="0" />
<label for="rememberme" class="checkbox_1" tabindex="0">remember me</label>
</div>
<div id="auth_area_login_button">
<button class = "btn btn-lg btn-primary">
Login
</button>
</div>
</form>
CSS:
#import url('//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css');
#login_form{padding:20px;}
label.checkbox_1 {
display: inline-block;
cursor: pointer;
position: relative;
padding-left: 25px;
margin: 0px;
}
label.checkbox_1:before {
content: "";
display: inline-block;
width: 16px;
height: 16px;
margin-right: 10px;
position: absolute;
left: 0;
bottom: 1px;
background-color: #fff;
border: 1px solid #ccc;
border-radius: 0px;
}
label.checkbox_1:hover:before{border-color:#66afe9;}
input[type=checkbox].checkbox_1 {
display: none;
}
input[type=checkbox].checkbox_1:checked + label.checkbox_1:before {
content: "\2713";
font-size: 15px;
color: #A0A0A0;
text-align: center;
line-height: 15px;
}
EDIT 1:
seems to work in firefox, but not in chrome...
Input must be accessible to receive focus. It works in chrome/chromium if you add following lines.
input[type=checkbox].checkbox_1 {
opacity: 0;
}
input[type=checkbox].checkbox_1:focus + label.checkbox_1:before {
border: 1px solid #66afe9;
}
Since the real checkbox is hide with display:none you can't focus it but you can also don't hide the element just make it be under the :before of the label:
input[type=checkbox].checkbox_1 {
position: absolute;
width: 16px;
height: 16px;
margin: 0;
border: 1px solid transparent;
margin-top: 3px;
}
Check this http://cssdeck.com/labs/pl4ljry7
Tested in Chrome
Because, it is not a checkbox.
Look at the css:
input[type=checkbox].checkbox_1 {
display: none;
}
The checkbox is actually hidden. So, you will not be able to focus it. The stylized square and checkmark shown are through the :before pseudo element on label. Pseudo-elements can't be focused. Nor can the labels.
I know this is an old question, but I came up with a Jquery solution when the CSS solution didn't work for me, and thought others might find this helpful. I wrapped the input in a div with the desired tabindex value and the class "checkbox-add-tabindex". Then, using Jquery, I transferred the tabindex from the div to the input.
HTML:
<div class="checkbox-add-tabindex" tabindex="10">
<input id="rememberme" type="checkbox" name="rememberme" value="1" class="checkbox_1" tabindex="0" />
<label for="rememberme" class="checkbox_1" tabindex="0">remember me</label>
</div>
Jquery:
$(".checkbox-add-tabindex").focus(
function () {
var tabval = $(this).prop("tabindex");
$(this).removeAttr("tabindex");
$(this).children(":first").attr("tabindex", tabval);
$(this).children(":first").focus();
})