This question already has answers here:
How to create a checkbox with a clickable label?
(12 answers)
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Once I check the checkmark box I am not able to uncheck it when I am on mobile view in chrome and when I change to desktop the checkmark does not even stay and I am not able to check the box at all.
What am I missing?
Code :
input[type=checkbox] {
display: none;
}
#following {
font-family: "Comic Sans MS", "Comic Sans", cursiveic;
text-align: center;
display: inline-block;
color: black;
cursor: pointer;
position: relative;
padding-left: 30px;
margin: -10px 10px;
top: -10px;
font-size: 15px;
}
label:before {
line-height: 20px;
content: "";
display: inline-block;
width: 12px;
height: 12px;
margin-right: 10px;
position: absolute;
left: 10px;
top: 5px;
border: 1px solid black;
}
input[type=checkbox]:checked + label:before,
label:hover:before {
content: "\2713";
font-size: 22px;
color: black;
line-height: 7px;
}
<div>
<input type="checkbox" name="check">
<label id="following" for="check">Following</label>
</div>
Add an id attribute to your checkbox. Label for will only work with input id
<div>
<input type="checkbox" name="check" id="check">
<label id="following" for="check">Following</label>
</div>
Remove label:hover:before from css. Otherwise it will not uncheck on click in mobile view
input[type=checkbox]:checked + label:before{
content: "\2713";
font-size: 22px;
color: black;
line-height: 7px;
}
I tried to replicate your problem and it worked for me when I updated your html to include an id for your checkbox.
<div>
<input type="checkbox" name="check" id="check">
<label id="following" for="check">Following</label>
</div>
The for attribute targets an ID.
input[type=checkbox] {
display: none;
}
#following {
font-family: "Comic Sans MS", "Comic Sans", cursiveic;
text-align: center;
display: inline-block;
color: black;
cursor: pointer;
position: relative;
padding-left: 30px;
margin: -10px 10px;
top: -10px;
font-size: 15px;
}
label:before {
line-height: 20px;
content: "";
display: inline-block;
width: 12px;
height: 12px;
margin-right: 10px;
position: absolute;
left: 10px;
top: 5px;
border: 1px solid black;
}
input[type=checkbox]:checked + label:before,
label:hover:before {
content: "\2713";
font-size: 22px;
color: black;
line-height: 7px;
}
input[type=checkbox]:not(:checked) + label:before {
content: "";
}
<div>
<input type="checkbox" id="check" name="check">
<label id="following" for="check">Following</label>
</div>
Related
I have a simple custom radio and I want to add a ticky when clicked,
I tried below code :
p.payment_module a {
display: block;
border: 0;
-moz-border-radius: 0;
-webkit-border-radius: 0;
border-radius: 0px;
line-height: 23px;
color: #000;
position: relative;
border: none !important;
white-space: nowrap;
text-transform: initial;
font-family: "futura-pt, sans-serif";
}
p.payment_module a span {
color: #777;
font-weight: normal !important;
}
p.payment_module a:after {
content: '';
background: #fff;
border: 1px solid #000;
display: inline-block;
vertical-align: text-top;
width: 25px;
height: 25px;
padding: 0px;
left: -16px !important;
top: 9px !important;
position: absolute;
display: inline-block;
margin-right: 30px;
text-align: center;
border-radius: 50%;
}
p.payment_module a:before{
content: '✔';
box-shadow: inset 0px 0px 0px 4px #fff;
}
<p class="payment_module">
<a href="http://localhost:8080/index.php?fc=module&module=bankwire&controller=payment&id_lang=1" title="Zapłać przelewem" class="button button-block payment-button">
<img src="/modules/bankwire/bankwire.jpg" alt="Zapłać przelewem" width="86" height="49">
Zapłać przelewem <span>(czas przetwarzania zamówienia będzie dłuższy)</span>
</a>
</p>
My custom radio does not display the tick when I click , What am I missing in my code?
Any help will be appreciated
The problem is that <a> css, Please check below code:
input[type="radio"] {
display: none;
}
input[type="radio"] + label {
display: inline-block;
cursor: pointer;
}
input[type="radio"] + label:before {
content: "";
display: inline-block;
position: relative;
border: 1px solid #000;
border-radius:50px;
width: 60px;
height: 60px;
margin:0;
}
input[type="radio"]:checked + label:before {
content: "✔";
font-family: 'lucida grande';
font-size: 45px;
line-height: 60px;
text-align: center;
}
<p class="payment_module">
<input id="session1" name="radio-group" type="radio" />
<label for="session1">
<img src="https://picsum.photos/86/49/?random" alt="Zapłać przelewem" width="86" height="49">
Zapłać przelewem <span>(czas przetwarzania zamówienia będzie dłuższy)</span>
</label>
</p>
<input type='checkbox' name='chkbox'/>
I want to change the checkbox height and width and also background color if possible
You can look at this piece of code for starters.
It uses the before element to create a custom shape and hide the default checkbox for two states 'checked' and 'unchecked'
Also give a label for that corresponding checkbox so that clicking this custom shape will actually trigger the hidden default checkbox.
body {
font-family: 'segoe ui';
background-color: white;
padding: 10px;
}
.space {
height: 10px;
}
.checkbox * {
box-sizing: border-box;
position: relative;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.checkbox {
display: inline-block;
}
.checkbox>input {
display: none;
}
.checkbox>label {
vertical-align: top;
font-size: 18px;
padding-left: 30px;
}
.checkbox>[type="checkbox"]+label:before {
color: #777;
content: '';
position: absolute;
left: 0px;
display: inline-block;
min-height: 20px;
height: 20px;
width: 20px;
border: 2px solid #777;
font-size: 15px;
vertical-align: top;
text-align: center;
transition: all 0.2s ease-in;
content: '';
}
.checkbox.radio-square>[type="checkbox"]+label:before {
border-radius: 0px;
}
.checkbox.radio-rounded>[type="checkbox"]+label:before {
border-radius: 25%;
}
.checkbox.radio-blue>[type="checkbox"]+label:before {
border: 2px solid #ccc;
}
.checkbox>[type="checkbox"]+label:hover:before {
border-color: lightgreen;
}
.checkbox>[type="checkbox"]:checked+label:before {
width: 8px;
height: 16px;
border-top: transparent;
border-left: transparent;
border-color: green;
border-width: 4px;
transform: rotate(45deg);
top: -4px;
left: 4px;
}
<div class="checkbox">
<input id="chkTest" type="checkbox" />
<label for="chkTest">Task one</label>
<div class="space">
</div>
<input id="chkTest1" type="checkbox" />
<label for="chkTest1">Task two</label>
</div>
I've created This toggle button, and i can't set it's size to percentage - so it could be responsive. i was manage to set it's text to viewport width and height - and it's working well, but i don't want the button in viewport measurements....
perhaps ::before and ::after cannot receive measurements in percentage?
Html:
<div class="cont">
<div class="box">
<div class="material-switch">
<input id="someSwitchOptionDefault" name="someSwitchOption001"
type="checkbox" />
<label for="someSwitchOptionDefault" class="label-default"><span
class="homme">Homme</span><span class="femme">Femme</span>
</label>
</div>
</div>
</div>
Css:
.cont {
padding: 50px;
background-color: #e9e9e9;
width: 100vw;
height: 100vh;
}
.box {
width: 100px;
height: 100px;
}
.material-switch > input[type="checkbox"] {
display: none;
}
.material-switch > label {
cursor: pointer;
height: 0px;
position: relative;
width: 40px;
}
.material-switch > input[type="checkbox"] + label::before {
content: "\f222";
font-family: FontAwesome;
font-style: normal;
font-weight: normal;
text-decoration: inherit;
color: #333333;
text-align: right;
border: 1px solid #dcdcdc;
padding-left: 10px;
padding-right: 10px;
}
.material-switch > label::before {
background-color: #f6f6f6;
border-radius: 16px;
height: 24px;
margin-top: -12px;
position: absolute;
width: 100px;
}
.material-switch > label::after {
/*--button---*/
content: "";
font-size: 18px;
background-color: #fff;
border-radius: 15px;
height: 24px;
left: -4px;
margin-top: -8px;
position: absolute;
top: -4px;
width: 73px;
border: 1px solid #dcdcdc;
}
.material-switch > input[type="checkbox"]:checked + label::before {
background-color: #f6f6f6;
content: "\f221";
color: "blue";
font-family: FontAwesome;
text-align: left;
}
.material-switch > input[type="checkbox"]:checked + label::after {
background-color: #fff;
left: 30px;
}
.femme {
display: none;
}
.material-switch > input[type="checkbox"]:checked + label>span.homme {
display: none;
}
.material-switch > input[type="checkbox"]:checked + label>span.femme {
display: block;
}
.homme {
font-family: 'Roboto Light', sans-serif;
font-style: normal;
font-weight: normal;
text-decoration: inherit;
color: #5d5d5d;
font-size: 1vw;
position: absolute;
top: -8px;
left: 9px;
z-index: 999;
}
.femme {
font-family: 'Roboto Light', sans-serif;
font-style: normal;
font-weight: normal;
text-decoration: inherit;
color: #5d5d5d;
font-size: 1vw;
position: absolute;
top: -8px;
z-index: 999;
}
.material-switch > input[type="checkbox"]:checked + label .femme {
left: 41px;
}
.homme::before {
content: "\f222";
font-family: FontAwesome;
font-style: normal;
font-weight: normal;
text-decoration: inherit;
color: #49c8c1;
margin-right: 5px;
}
.femme::before {
content: "\f221";
font-family: FontAwesome;
font-style: normal;
font-weight: normal;
text-decoration: inherit;
color: #49c8c1;
margin-right: 5px;
}
I see that in the css you have overridden lot of html DOM properties, like for <input> component. I would suggest you to use simple and elegant toggle buttons available with normal css.
You can check below toggle button examples:
https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_switch
https://codepen.io/mburnette/pen/LxNxNg
How i can make a css ckeckbox+lebel without id and for .
I use this way but it dosnt work.i can have a css ckeckbox whit id and for like this :
<div class="checkbox">
<input id="f" type="checkbox" name="hi" class="checkbox" />
<label for="f">Hello</label>
</div>
But i wanna to have this :
<label class="checkbox">
<input type="checkbox" value="1" name="files" >
</label>
Then i use This CSS (SCSS in fact):
label{
input[type=checkbox]{
display: none;
}
}
.checkbox{
position: relative;
display: inline-block;
margin-right: 7px;
cursor: pointer;
vertical-align: middle;
&:after{
content: ' ';
position: absolute;
border: 2px solid $black;
border-radius: 3px;
width: 20px;
height: 20px;
top: -5px;
left: -2px;
width: 14px;
height: 14px;
}
&:checked{
font-family: 'FontAwesome';
font-size: 2em;
content: "\f00c";
color: $blue;
border-radius: 3px;
}
}
/* It dosnt work */
.checkbox:after > input[type=checkbox]:checked {
font-family: 'FontAwesome';
font-size: 2em;
content: "\f00c";
color: $blue;
border-radius: 3px;
}
But Checked check box dost show ?
Please help :)
You must use span in your Label tag.
.checkbox:after > input[type=checkbox]:checked
This line not make sense because its need to be the opposite. So I'm change it to this:
input[type=checkbox]:checked ~ .checkbox:after
When you check ~ do something with the next element.
So, i add a span to complete your problem. Here we go:
Working Fiddle
$blue : blue;
$black : black;
label{
input[type=checkbox]{
opacity: 0;
}
}
.checkbox{
position: relative;
display: inline-block;
margin-right: 7px;
cursor: pointer;
vertical-align: middle;
&:after{
content: ' ';
position: absolute;
border: 2px solid $black;
border-radius: 3px;
width: 20px;
height: 20px;
top: -5px;
left: -2px;
width: 14px;
height: 14px;
}
&:checked{
font-family: 'FontAwesome';
font-size: 2em;
content: "\f00c";
color: $blue;
border-radius: 3px;
}
}
/* It dosnt work */
input[type=checkbox]:checked ~ .checkbox:after{
font-family: 'FontAwesome';
font-size: 1em;
content: "\f00c";
color: $blue;
border-radius: 3px;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet"/>
<label>
<input type="checkbox" value="1" name="files" >
<span class="checkbox"></span>
</label>
My Radio button's checked dot is not coming in center if radio option is checked.
Please find my fiddle below:
/* checkbox -*/
.radio-s label,
.radio-s input[type="radio"]+span,
.radio-s input[type="radio"]+span::after,
.checkboxe-s label,
.checkboxe-s input[type="checkbox"]+span,
.checkboxe-s input[type="checkbox"]+span::after {
display: inline-block;
vertical-align: middle
}
.radio-s,
.checkboxe-s {
position: relative
}
.radio-s label *,
.checkboxe-s label * {
cursor: pointer
}
.radio-s input[type="radio"],
.checkboxe-s input[type="checkbox"] {
position: absolute;
display: none
}
.radio-s input[type="radio"]+span,
.checkboxe-s input[type="checkbox"]+span {
color: #333
}
.radio-s label:hover span,
.checkboxe-s label:hover span {
color: #000
}
.radio-s input[type="radio"]+span::after,
.checkboxe-s input[type="checkbox"]+span::after {
margin: 0 auto 0 10px;
width: 13px;
height: 13px;
border: solid 2px #ccc;
background-size: 13px;
content: "";
text-align: center;
line-height: 17px;
border-radius: 50%;
/*display: block;*/
}
.checkboxe-s input[type="checkbox"]+span:hover::after {
border: solid 2px #5a5a5a
}
.radio-s input[type="radio"]:checked+span::after,
.checkboxe-s input[type="checkbox"]:checked+span::after {
width: 13px;
height: 13px;
border: 2px solid #01A982;
background-color: #FFFFFF;
color: #01A982;
line-height: 17px
}
.radio-s input[type="radio"]:disabled+span,
.checkboxe-s input[type="checkbox"]:disabled+span {
opacity: .4;
cursor: default
}
.checkboxe-s input[type="checkbox"]+span::after {
border-radius: inherit;
}
.radio-s input[type="radio"]:checked+span::after {
content: "\2022";
font-size: 24px
}
.checkboxe-s li {
list-style: none;
}
:root .checkboxe-s input[type="checkbox"]:checked+span::after {
content: "\2713";
font-family: arial;
font-size: 14px;
font-weight: bold;
}
.checkboxe-s input[type="checkbox"]:checked+span::after {
background-color: #FFFFFF;
border: solid 2px #01A982;
color: #01A982;
line-height: 14px;
}
.checkboxe-s input[type="checkbox"]:checked+span::after {
border: solid 2px #01A982;
}
<h1>Radio</h1>
<div class="radio-s">
<label for="radio1" class="radio">
<input type="radio" name="optionsRadios" id="radio1" />
<span class="comp-name">Radio 1</span>
</label>
</div>
<div class="radio-s">
<label for="radio2" class="radio">
<input type="radio" name="optionsRadios" id="radio2" />
<span class="comp-name">Radio 1</span>
</label>
</div>
line-height is the problem.
For example putting it to 14px might fix your problem.
.radio-s input[type="radio"]:checked + span::after {
line-height: 14px;
}
EDIT: I suggest that you convert your width and height of your radio circle to 14px and then put line-height to 14px. Cause it looks like it's off by 1px cause the number 13 is odd.