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:
Related
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.
/* 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.
Using only CSS, I was able to replace the normal checkbox with a custom image. The problem is, when seen on an iPad, it can look like this:
[] This is
some text
Instead of like this:
[] This
some text.
Here's what I've got now:
input[type="checkbox"] {
visibility: hidden;
position: absolute;
height: 0;
width: 0;
}
input[type="checkbox"] + label span {
display: inline-block;
width: 20px;
height: 20px;
margin: -2px 10px 0 0;
vertical-align: middle;
background: url(http://qa.walkup.audidriveusa.com/images/checkbox.png) no-repeat;
cursor: pointer;
}
input[type="checkbox"]:checked + label span {
background: url(http://qa.walkup.audidriveusa.com/images/checkbox-selected.png) no-repeat;
}
<div style="width:100px">
<input type="checkbox" class="SurveyQuestion" id="q6a" name="Question_Vehicle_More_Information" value="Audi_A3">
<label for="q6a"><span></span>Audi A3 with long text that will wrap</label>
</div>
Use flex on the parent. That will keep them on the same row, and the content will wrap in the individual flex children.
label {
display: flex;
}
input[type="checkbox"] {
visibility: hidden;
position: absolute;
height: 0;
width: 0;
}
input[type="checkbox"] + label span {
height: 20px;
margin: -2px 10px 0 0;
display: block;
background: url(http://qa.walkup.audidriveusa.com/images/checkbox.png) no-repeat;
cursor: pointer;
flex: 0 0 20px;
}
input[type="checkbox"]:checked + label span {
background: url(http://qa.walkup.audidriveusa.com/images/checkbox-selected.png) no-repeat;
}
<div style="width:100px">
<input type="checkbox" class="SurveyQuestion" id="q6a" name="Question_Vehicle_More_Information" value="Audi_A3">
<label for="q6a"><span></span>Audi A3 with long text that will wrap</label>
</div>
Integrate custom checkboxes as <li> of a <ul>
SNIPPET
.chx {
display: none;
}
ul {
list-style: none;
}
.lbl {
width: 20px;
height: 20px;
border: 2px inset red;
border-radius: 8px;
position: relative;
cursor: pointer;
display: inline-block;
float: left;
margin-right: 10px;
vertical-align: middle;
}
.lbl:hover {
background: #000;
}
#chx1:checked+.lbl::before {
content: '💀';
font-size: 16px;
position: absolute;
background: #000;
border-radius: 6px;
}
#chx2:checked+.lbl::before {
content: '👿';
font-size: 16px;
position: absolute;
background: #000;
border-radius: 6px;
}
#chx3:checked+.lbl::before {
content: '👽';
font-size: 16px;
position: absolute;
background: #000;
border-radius: 6px;
}
<ul>
<input id='chx1' class='chx' type='checkbox'>
<label for='chx1' class='lbl'></label>
<li>Text that's long and will wrap with an indentation like a list item with a bullet.</li>
<input id='chx2' class='chx' type='checkbox'>
<label for='chx2' class='lbl'></label>
<li>Text that's long and will wrap with an indentation like a list item with a bullet.</li>
<input id='chx3' class='chx' type='checkbox'>
<label for='chx3' class='lbl'></label>
<li>Text that's long and will wrap with an indentation like a list item with a bullet.</li>
</ul>
Updated code based on your updated code snippet:
I added inline-flex to the label, padding-left: 10px and min-width: 20px to the span.
label {
display: inline-flex;
}
input[type="checkbox"] + label span {
min-width: 20px;
padding-left: 20px;
}
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
Why does my cursor start top right in this example?
See when I click inside the field, it's top right then when I type it moves to the centre. Any ideas why?
http://jsfiddle.net/2Ltm5adw/
HTML:
<label class="input">
<span>Email address</span>
<input type="text" />
</label>
CSS:
input, select, textarea {
line-height:50px;
width:100%;
padding-left:20px;
display: block;
}
.input span {
position: absolute;
z-index: 1;
cursor: text;
pointer-events: none;
color: #999;
/* Input padding + input border */
padding: 7px;
/* Firefox does not respond well to different line heights. Use padding instead. */
line-height: 50px;
/* This gives a little gap between the cursor and the label */
margin-left: 2px;
}
.input input, .input textarea, .input select {
z-index: 0;
padding: 6px;
margin: 0;
font: inherit;
line-height: 50px;
}
Because of the line-height. Replace it with height:
input,
select,
textarea {
border: 2px solid $gray-lighter;
border-bottom: 1px solid $gray-lighter;
border-top: 1px solid $gray-lighter;
height: 50px;
/*replace with height*/
width: 100%;
padding-left: 20px;
background: $white;
display: block;
}
.input span {
position: absolute;
z-index: 1;
cursor: text;
pointer-events: none;
color: #999;
/* Input padding + input border */
padding: 7px;
/* Firefox does not respond well to different line heights. Use padding instead. */
line-height: 50px;
/* This gives a little gap between the cursor and the label */
margin-left: 2px;
}
.input input,
.input textarea,
.input select {
z-index: 0;
padding: 6px;
margin: 0;
font: inherit;
height: 50px;
/*replace height*/
}
<label class="input">
<span>Email address</span>
<input type="text" />
</label>
Demo
It's because of line-height. try height instead line-height.It works fine
.input input, .input textarea, .input select {
z-index: 0;
padding: 6px;
margin: 0;
font: inherit;
height: 50px;
}
Just remove line-height from inputs and replace them with padding: 20px 7px;
.input span {
position: absolute;
z-index: 1;
cursor: text;
pointer-events: none;
color: #999;
/* Input padding + input border */
padding: 7px;
/* Firefox does not respond well to different line heights. Use padding instead. */
line-height: 50px;
/* This gives a little gap between the cursor and the label */
margin-left: 2px;
}
.input input,
.input textarea,
.input select {
z-index: 0;
padding: 6px;
margin: 0;
font: inherit;
padding: 20px 7px;
}
<label class="input">
<span>Email address</span>
<input type="text" />
</label>
Whats wrong with this ? Saves mucking around
<label for="emailID">Email Address : </label>
<input type="email" id="emailID" placeholder="Your Email Address"/>
And stylable.
#emailID {width:300px;background-color:green;color:white;border-radius:5px;}
<label for="emailID">Email Address : </label>
<input type="email" id="emailID" placeholder="Your Email Address"/>
Here is your updated JSFiddle Link, Check out your issue has been solved or not !!
Changes Made:
.input span {
padding: 20px;
line-height: 25px;
}
.input input, .input textarea, .input select {
padding: 20px;
height: 25px;
}