I want to hide a checkbox
.
But also want that, when I click on label associated with corresponding checkbox
, the checkbox
should
Elements that are not being rendered (be it through visibility: hidden
, display: none
, opacity: 0.0
, whatever) will not indicate focus. The browser will not draw a focus border around nothing.
If you want the text to be focusable, that's completely doable. You can wrap the whole thing in an element that can receive focus (for example, a hyperlink), or allow another tag to have focus using the tabindex
property:
<label tabindex="0" class="checkbox">
<input type="checkbox" value="valueofcheckbox" style="display:none" checked="checked" />Option Text
</label>
Fiddle
In this case, the <label>
tag above is actually receiving focus and everything within it will have a focus border when it's in focus.
I do question what your goal is. If you're using a hidden checkbox to internally track some sort of state, you might be better off using a <input type="hidden" />
tag instead.
This two classes are borrowed from the HTML Boilerplate main.css. Although the invisible checkbox will be focused and not the label.
/*
* Hide only visually, but have it available for screenreaders: h5bp.com/v
*/
.visuallyhidden {
border: 0;
clip: rect(0 0 0 0);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
}
/*
* Extends the .visuallyhidden class to allow the element to be focusable
* when navigated to via the keyboard: h5bp.com/p
*/
.visuallyhidden.focusable:active,
.visuallyhidden.focusable:focus {
clip: auto;
height: auto;
margin: 0;
overflow: visible;
position: static;
width: auto;
}
You need to add the element type to the class, otherwise it will not work.
.hide-checkbox { display: none } /* This will not work! */
input.hide-checkbox { display: none } /* But this will. */
<input class="hide-checkbox" id="checkbox" />
<label for="checkbox">Checkbox</label>
It looks too simple, but try it out!
if you want your check box to keep its height and width but only be invisible:
.hiddenCheckBox{
visibility: hidden;
}
if you want your check box to be invisible without any with and height:
.hiddenCheckBox{
display: none;
}
input
may have a hidden attribute:input:checked + span::before {
content: 'un';
}
<label>
<input type='checkbox' hidden/>
<span>check</span>
<label>
Try setting the checkbox's opacity to 0. If you want the checkbox to be out of flow try position:absolute
and offset the checkbox by a large number.
HTML
<label class="checkbox"><input type="checkbox" value="valueofcheckbox" checked="checked" style="opacity:0; position:absolute; left:9999px;">Option Text</label>