Label to control checkbox - html

I've been playing around with the CSS hack to use <label for="".. to control the toggle for a checkbox. Here's a codepen.
When I add another <input>, it disallows the toggle for the checkbox. (When I remove the hidden input everything works fine)..
Are my css selectors accommodating for this hidden input? I may be missing something simple.
.checkbox-on-off {
position: relative;
display: inline-block;
width: 35px;
padding-right: 2px;
overflow: hidden;
vertical-align: middle;
margin-top: 10px;
}
/* this positions the check box label over the text box */
.checkbox-on-off input[type=checkbox] {
position: absolute;
opacity: 0;
}
/* makes the background blue */
.checkbox-on-off input[type=checkbox]:checked+label {
background-color: #167ac6;
}
/* this is the grey background check mark */
.checkbox-on-off label {
display: inline-block;
border: 1px solid transparent;
height: 15px;
width: 100%;
background: #b8b8b8;
cursor: pointer;
border-radius: 20px;
}
/* this adds / positions the check mark */
.checkbox-on-off input[type=checkbox]:checked+label .checked {
display: inline-block;
margin-top: 3px;
margin-left: 6px;
background-size: auto;
width: 10px;
height: 10px;
}
/* if you click the checkbox, it sometimes has a grey square */
.checkbox-on-off label .checked {
display: none;
}
.checkbox-on-off input[type=checkbox]:checked+label .unchecked {
display: none;
}
.checkbox-on-off label .unchecked {
display: inline-block;
float: right;
padding-right: 3px;
}
#autoplay-checkbox-label
.checkbox-on-off label {
display: inline-block;
border: 1px solid transparent;
height: 13px;
width: 100%;
background: #b8b8b8;
cursor: pointer;
border-radius: 20px;
}
/* this positions the white dot */
.checkbox-on-off input[type=checkbox]:checked+label .toggle {
float: right;
}
/* this is the actual white dot */
.checkbox-on-off label .toggle {
float: left;
background: #fbfbfb;
height: 15px;
width: 13px;
border-radius: 20px;
}
<span class="checkbox-on-off ">
<input id="autoplay-checkbox" class="" type="checkbox" checked="">
<input name="" type="hidden" value="false">
<label for="autoplay-checkbox" id="autoplay-checkbox-label">
<span class="checked"> </span>
<span class="unchecked"></span>
<span class="toggle"> </span>
</label>
</span>

The problem comes from this selector : input[type=checkbox]:checked+label
It means you are targetting label which is immediately after your input (element+element).
Problem is your hidden input is between checkbox and label.
Everything works if you place your hidden input :
before your checkbox,
after your label,
inside your label.
Live Demo with 3 hidden inputs
Update 1 :
In case you cannot modify HTML markup, you can move elements by jquery by adding $("input[type=hidden]").prependTo("#autoplay-checkbox");
This will move the hidden input before your checkbox
Updated exemple

Related

Why won't the Background color for radio button after checked not change?

I've searched around and checked various answers, but I'm having trouble with the following:
There are a couple caveats
Can't use Javascript or Jquery.
has to be pure CSS.
I want the background color of the label to change from Blue to Orange after the input is selected. It does not seem to work and I've checked around and read several answers from people, but none of them have worked for me and I'm not sure what I'm doing wrong.
The CSS:
.header-tabs-container {
position: relative;
float: center;
left: 25%;
clear: both;
z-index: 2;
border-collapse: collapse;
white-space: normal;
border: unset;
}
/* tabs names */
.header-tabs-container .header-label {
position: relative;
padding: clamp(-1.5rem, -3.2rem + 8.8889vw, 3rem);
font-size: clamp(0.95rem, -0.925rem + 8.333vw, 3rem);
background-color: blue;
color: #fff;
cursor: pointer;
user-select: none;
text-align: center;
z-index: 1;
margin: 0px;
border: white 1px solid;
white-space: nowrap;
border-radius: 40px 40px 0px 0px;
}
/* Hover effect on tabs names */
.header-tabs-container .header-label:hover {
background: orange;
color: blue;
transition: 0.2s;
}
/* Content area for tabs */
.header-tab-content {
position: relative;
background: #eee;
margin-top: -10px;
width: 100%;
min-height: 100vh;
padding: 0px;
float: left;
box-sizing: border-box;
z-index: 2;
display: none;
white-space: nowrap;
border: 1px solid blue;
}
.header-tab-content:after {
content: "";
clear: both;
}
/* Hide input radio from users */
input[name="header-tab"] {
display: none;
}
/* Show tab when input checked */
input[name="header-tab"]:checked + .header-tab-content {
display: block;
transition: 0.5s ease-out;
}
input[name="header-tab"]::after + .header-label {
background-color: orange;
}
The HTML
<section class="header-tabs-container">
<label class="header-label" for="header-tab1">Tab1</label><!--
--><label class="header-label" for="header-tab2">Tab2</label>
</section>
<input name="header-tab" id="header-tab1" type="radio" checked/>
<section class="header-tab-content">
<h3>Test</h3>
Content
</section>
<input name="header-tab" id="header-tab2" type="radio" />
<section class="header-tab-content">
<h3> test</h3>
content
</section>
</section>
Basically everything works as expected... Except I cannot, for the life of me, get the following to work at all.
input[name="header-tab"]:checked + header-label {
background-color: orange;
}
Any ideas or advice would be greatly appreciated.
Thank you in advance.
In CSS you cannot select previous siblings, therefore you'll need move input above your tabs and use ~ for sibling selections for the content:
.header-tabs-container {
position: relative;
float: center;
/* left: 25%;*/
clear: both;
z-index: 2;
border-collapse: collapse;
white-space: normal;
border: unset;
}
/* tabs names */
.header-tabs-container .header-label {
position: relative;
padding: clamp(-1.5rem, -3.2rem + 8.8889vw, 3rem);
font-size: clamp(0.95rem, -0.925rem + 8.333vw, 3rem);
background-color: blue;
color: #fff;
cursor: pointer;
user-select: none;
text-align: center;
z-index: 1;
margin: 0px;
border: white 1px solid;
white-space: nowrap;
border-radius: 40px 40px 0px 0px;
}
/* Hover effect on tabs names */
.header-tabs-container .header-label:hover {
background: orange;
color: blue;
transition: 0.2s;
}
/* Content area for tabs */
.header-tab-content {
position: relative;
background: #eee;
margin-top: -10px;
width: 100%;
min-height: 100vh;
padding: 0px;
float: left;
box-sizing: border-box;
z-index: 2;
display: none;
white-space: nowrap;
border: 1px solid blue;
}
.header-tab-content:after {
content: "";
clear: both;
}
/* Hide input radio from users */
input[name="header-tab"] {
display: none;
}
/* Show tab when input checked */
input[name="header-tab"]:nth-of-type(1):checked ~ .header-tab-content:nth-of-type(1),
input[name="header-tab"]:nth-of-type(2):checked ~ .header-tab-content:nth-of-type(2) {
display: block;
transition: 0.5s ease-out;
}
input[name="header-tab"]:checked + .header-label {
background-color: orange;
}
<section class="header-tabs-container">
<input name="header-tab" id="header-tab1" type="radio" checked/>
<label class="header-label" for="header-tab1">Tab1</label>
<input name="header-tab" id="header-tab2" type="radio" />
<label class="header-label" for="header-tab2">Tab2</label>
<section class="header-tab-content">
<h3>Test</h3>
Content
</section>
<section class="header-tab-content">
<h3> test</h3>
content
</section>
</section>
The + combinator matches the second element only if it immediately
follows the first element.
CSS_Selectors
Move the labels and inputs inside the same section and then try ~ or +.
input must be first.

How to strike through input text when checkbox is ticked?

Thanks to another user's question here I was able to strike through the text when the checkbox next to it is ticked, thanks to the following HTML and CSS:
<style>
/* Customize the label (the container) */
.container {
position: relative;
padding-left: 50px;
margin-bottom: 12px;
cursor: pointer;
color: #333;
font-size: 18px;
}
/* Hide the browser's default checkbox */
.container input {
display: none
}
/* Create a custom checkbox - using ::before */
.checkmark::before {
content: "";
height: 25px;
width: 25px;
background-color: #fff;
border: solid 2px #194263;
position: absolute;
left:0;
top:0;
margin-right: 10px;
}
/* Show the checkmark when checked */
.container input:checked~.checkmark:after {
display: block;
left: 9px;
top: 5px;
width: 8px;
height: 14px;
border: solid #194263;
border-width: 0 3px 3px 0;
transform: rotate(45deg);
content: "";
position: absolute;
margin-right: 10px;
}
/* strike through the text */
.container input:checked~.checkmark {
text-decoration: line-through
}
</style>
<label class="container">
<input type="checkbox">
<span class="checkmark"></span><br>
</label>
Now, I'd like to let a user add their own text, and still strike through it when the checkbox is ticked. Adding an input field within the span tag as follows does not work.
<label class="container">
<input type="checkbox">
<span class="checkmark"><input type="text" minlength="1" maxlength="100" size="60%" placeholder="Add an item"></span><br>
</label>
Why does this not work? What to do instead?
Your code has the following issue. When you write these lines of css:
.container input:checked~.checkmark {
text-decoration: line-through
}
You're not adding the text-decoration: line-through css property to the text input element you want to strike, but to the checkmark instead. Therefore, the text input element is not receiving any strike-through styles.
What I did to solve your problem was adding the styles to the text input. I did this by doing some small changes to your HTML and CSS, this is the code:
/* Customize the label (the container) */
.container {
position: relative;
padding-left: 50px;
margin-bottom: 12px;
cursor: pointer;
color: #333;
font-size: 18px;
}
/* Hide the browser's default checkbox */
.container input[type="checkbox"] {
display: none
}
/* Create a custom checkbox - using ::before */
.checkmark::before {
content: "";
height: 25px;
width: 25px;
background-color: #fff;
border: solid 2px #194263;
position: absolute;
left:0;
top:0;
margin-right: 10px;
}
/* Show the checkmark when checked */
.container input:checked~.checkmark:after {
display: block;
left: 9px;
top: 5px;
width: 8px;
height: 14px;
border: solid #194263;
border-width: 0 3px 3px 0;
transform: rotate(45deg);
content: "";
position: absolute;
margin-right: 10px;
}
/* strike through the text */
.container input:checked ~ input {
text-decoration: line-through
}
<label class="container">
<input type="checkbox">
<input type="text" minlength="1" maxlength="100" size="60%" placeholder="Add an item">
<span class="checkmark"></span>
<br>
</label>
And this is the result:

Replacing a checkbox element with icons

I want to customize the browser checkbox. When the status is checked, the icon checked.svg should be displayed instead of checkbox and the icon unchecked.svg otherwise.
Here is my code.
HTML:
<div class="checkbox-custom">
<input type="checkbox"/>
<div class="checkmark">
<img src="#/assets/images/icons/checkbox/unchecked.svg" class="unchecked"/>
<img src="#/assets/images/icons/checkbox/checked.svg" class="checked"/>
</div>
</div>
SASS:
.checkbox-custom {
position: absolute;
width: 28px;
height: 28px;
left: 0;
top: 0;
// Hide default browser checkbox
input[type=checkbox] {
display: none;
}
input[type=checkbox] + .checkmark {
position: absolute;
left: 3.5px;
top: 3.5px;
right: 3.5px;
bottom: 3.5px;
display: inline-block;
cursor: pointer;
img {
width: 21px;
height: 21px;
}
.checked {
display: none;
}
}
input[type=checkbox]:checked + .checkmark {
.checked {
display: block;
}
.unchecked {
display: none;
}
}
}
When i click on the checkbox, nothing happens. What could be the error?
This can be accomplished without any javascript. Clicking the label element toggles the checkbox inside of it, so with some clever css, we can change the display based on the input's checked state.
input[type=checkbox] {
display: none;
}
.label {
border: 1px solid #000;
display: inline-block;
padding: 3px;
/* background: url("unchecked.png") no-repeat left center; */
/* padding-left: 15px; */
}
input[type=checkbox]:checked + .label {
background: #f00;
color: #fff;
/* background-image: url("checked.png"); */
}
<label><input type="checkbox"><span class="label">Check me</span></label>
Change the .label styles to use background-image of your choice (and position it to the left of your text).
http://jsfiddle.net/pKM3x/
CSS:
.checkbox{
width: 23px;
height: 21px;
background: transparent url(http://i.stack.imgur.com/S4p2R.png ) no-repeat 0 50%
}
.checked{
background: transparent url(http://i.stack.imgur.com/S4p2R.png ) no-repeat 80% 50%
}
HTML:
<div class="checkbox">
</div>
JQUERY:
$(".checkbox").click(function(){
$(this).toggleClass('checked')
});
This also possible to do with pure JS.

How do i place my label over my text box input?

I have the below code to make a login form but i cant get the checkbox label to be like always against the edge of the text area. I always sits to the right of the text area. I cant get it to be dependant on the div it is in. On inspection it sits outside the div.
Different things i have tried have included giving the label a left value but this messes it up when the screen size changes.
I want something like this
Here is a jsfiddle if this is easier
function showHidePassword() {
var x = document.getElementById("pass");
if (x.type === "password") {
x.type = "text";
} else {
x.type = "password";
}
}
body {
background-color: #ffffff;
}
* {
box-sizing: border-box;
}
input[type=text],
select,
textarea {
width: 100%;
padding: 12px;
border: 1px solid #ccc;
border-radius: 4px;
resize: vertical;
}
input[type=password],
select,
textarea {
width: 100%;
padding: 12px;
border: 1px solid #ccc;
border-radius: 4px;
resize: vertical;
}
label {
padding: 12px 12px 12px 0;
display: inline-block;
}
input[type=submit] {
background-color: #4CAF50;
color: white;
padding: 12px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
float: right;
}
input[type=submit]:hover {
background-color: #45a049;
}
.container1 {
border-radius: 25px;
background-color: #f2f2f2;
padding: 40px;
position: center;
margin: 15% 30%;
}
.signup {
border-radius: 25px;
background-color: #f2f2f2;
padding: 40px;
position: center;
opacity: 0.96;
}
.container1 .new-body {
background: #f2f2f2;
}
.signup .new-body {
background: #f2f2f2;
}
.signup .row {
padding-top: 5px;
}
.col-25 {
float: left;
width: 25%;
margin-top: 6px;
}
.col-75 {
float: left;
width: 65%;
margin-top: 6px;
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
/* Responsive layout - when the screen is less than 600px wide, make the two columns stack on top of each other instead of next to each other */
#media screen and (max-width: 600px) {
.col-25,
.col-75,
input[type=submit] {
width: 100%;
margin-top: 0;
}
.col-70,
input[type=submit] {
width: 95%;
margin-top: 0;
}
}
.passw {
cursor: pointer;
width: 30px;
height: 20px;
}
.col-75 label {
padding-top: 16px;
position: absolute;
z-index: 100;
}
<form>
<div class="row">
<div class="col-25">
<label for="pass">Password</label>
</div>
<div class="col-75">
<input type="password" id="pass" name="password" minlength="5" pattern="[A-Za-z][A-Za-z0-9]*[0-9][A-Za-z0-9]*" placeholder="Password" title="A valid password is a set of 5 characters, each consisting of an
upper or lower-case letter, or a digit. The password must begin with a letter and contain at least one digit" autocomplete="current-password" required>
<label for="passShowIcon" id="showHide"><input name="passShowIcon" type="checkbox" class="passw" onclick="showHidePassword();">
<span class=" "></span></label>
</div>
</div>
<div class="row">
<input type="submit" value="Submit">
</div>
</form>
If you wanted to make sure the checkbox appears inside the text input. You could wrap both input fields with a relative class, and then apply absolute positioning to the checkbox.
Like so:
https://jsfiddle.net/x0o46g7a/2/
.wrapper {
position: relative;
}
.text {
width: 100%;
height: 100%;
}
.checkbox {
position: absolute;
top: -8px;
right: -8px;
}
Something to note:
I would recommend adding some padding-right to your text input, to make sure it's text does not overlap/underlap the absolute positioned checkbox.
Based on your code, add the following rules in your css.
float: right to .col-75 instead of float left
right: 0 to .col-75 label
those will ensure that checkbox will remain inside the input field.

Custom radio button is not selecting on click

/* radio buttons */
.radio-container {
display: block;
position: relative;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
padding-left: 1.5em;
margin-bottom: 0.75em;
}
.radio-container input {
position: absolute;
opacity: 0;
cursor: pointer;
}
.radio-container input:checked .radio:after {
display: block;
}
.radio-container:hover .radio {
background: gray;
}
/* custom radio button */
.radio {
position: absolute;
top: 0;
left: 0;
height: 1em;
width: 1em;
background-color: transparent;
border: 1px solid gray;
border-radius: 50%;
}
.radio:after {
content: "";
position: absolute;
display: none;
top: 0;
left: 0;
width: 0.25em;
height: 0.25em;
border-radius: 50%;
background: white;
}
<form class="recharge">
<div>
<label class="radio-container" for="subscribe">
<input type="radio" id="one-time" name="recharge">
<span class="radio"></span>
Subscribe & Save 10%
</label>
</div>
<div>
<label class="radio-container" for="one-time">
<input type="radio" id="one-time" name="recharge">
<span class="radio"></span>
One Time Purchase
</label>
</div>
</form>
I have added custom styles to radio buttons on my website to give them a custom style. My HTML and CSS code is attached in the above snippet. However, now when I click on an input it does not select. I would ideally like to have this working without a JS component.
Please find the solution to your problem:
Codepen link to the solution
One of the issue I find was, you have not specified the color after the radio button is clicked and also the sibling selector was missing. I have added these lines specifically:
.radio-container input:checked ~ .radio {
background-color: #2196F3;
}
Hope it helps!! Thanks.