Everybody knows the trick with toggling by click between 2 styles of <label> via <input type="checkbox">.
input {
display: none;
}
input:checked+label {
background: green;
}
label {
background: yellow;
cursor: pointer;
display: block;
height: 100px;
width: 100px;
}
<input type="checkbox" id="toggle">
<label for="toggle"></label>
But what if you want to toggle through a chain of styles?
For example: click 1 => CSS 1, click 2 => CSS 2, click 3 => CSS 3, click 4 => CSS 1... etc.
Is it possible with HTML + CSS only?
UPD: Yes, it is. My approach is below. But is it possible to make the solution simpler?
input, label {
display: none;
}
div {
height: 50px;
width: 50px;
}
#toggle1:checked~div {
background: red;
}
#toggle2:checked~div {
background: yellow;
}
#toggle3:checked~div {
background: blue;
}
#toggle1:checked~div [for=toggle2],
#toggle2:checked~div [for=toggle3],
#toggle3:checked~div [for=toggle1] {
cursor: pointer;
display: block;
height: 100%;
}
<input type="radio" name="toggle" id="toggle1" checked>
<input type="radio" name="toggle" id="toggle2">
<input type="radio" name="toggle" id="toggle3">
<div>
<label for="toggle1"></label>
<label for="toggle2"></label>
<label for="toggle3"></label>
</div>
Related
I have labels with images nested inside that is acting as a checkbox. What I can't figure out is how to change the background behind the image when that specific checkbox is selected (label clicked).
It appears as if one of the checkboxes is getting the background to show, but not both and the height element is not working.
Does anyone see what I am doing wrong?
.notificationImgs {
width: 70px;
height: auto;
margin: 0 20px;
cursor: pointer;
}
.notifCheck {
display: none;
}
.notifCheck:checked + label {
background: #CCC;
}
.notifCheck:checked .notificationImgs {
background-color: #CCC;
width: 100%;
height: 70px;
}
<label for="checkText">
<img src="https://images.pexels.com/photos/67636/rose-blue-flower-rose-blooms-67636.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="Text Notifications" class="notificationImgs">
</label>
<input type="checkbox" name="checkText" class="notifCheck" id="checkText" value="text">
<label for="checkEmail">
<img src="https://images.pexels.com/photos/67636/rose-blue-flower-rose-blooms-67636.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="Email Notifications" class="notificationImgs">
</label>
Its not adding background on correct label because of the ordering of input and label. Put related input (which is hidden checkbox) before the related label. So .notifCheck:checked + label can find the correct label. Also you can add display:inline-block to all label style. So it will resolve the height issue.
See the Snippet below:
.notificationImgs {
width: 70px;
height: auto;
margin: 0 20px;
cursor: pointer;
}
.notifCheck {
display: none;
}
label{
display:inline-block;
padding:10px;
}
.notifCheck:checked + label {
background: #CCC;
}
.notifCheck:checked .notificationImgs {
background-color: #CCC;
width: 100%;
height: 70px;
}
<input type="checkbox" name="checkText" class="notifCheck" id="checkText" value="text">
<label for="checkText">
<img src="https://images.pexels.com/photos/67636/rose-blue-flower-rose-blooms-67636.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="Text Notifications" class="notificationImgs">
</label>
<input type="checkbox" name="checkEmail" class="notifCheck" id="checkEmail" value="text">
<label for="checkEmail">
<img src="https://images.pexels.com/photos/67636/rose-blue-flower-rose-blooms-67636.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="Email Notifications" class="notificationImgs">
</label>
Here's the altered HTML/code for you. You were missing checkbox and your CSS is + selector which is looking for checked + label so for first checkbox it was targeting second label.
.notificationImgs {
width: 70px;
height: 70px;
margin: 0 20px;
cursor: pointer;
}
.notifCheck {
display: none;
}
.notifCheck:checked+label {
background: #CCC;
}
.notifCheck:checked .notificationImgs {
background-color: #CCC;
width: 100%;
height: 70px;
}
label{
display:inline-block;
}
<input type="checkbox" name="checkText" class="notifCheck" id="checkText" value="text">
<label for="checkText">
<img src="https://images.pexels.com/photos/67636/rose-blue-flower-rose-blooms-67636.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="Text Notifications" class="notificationImgs"/>
</label>
<input type="checkbox" name="checkText" class="notifCheck" id="checkEmail" value="text">
<label for="checkEmail">
<img src="https://images.pexels.com/photos/67636/rose-blue-flower-rose-blooms-67636.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="Email Notifications" class="notificationImgs"/>
</label>
Label should come after checkbox in your case.
label{
display:inline-block;
padding:10px;
background: #eee;
}
.notificationImgs {
width: 70px;
height: auto;
margin: 0 20px;
cursor: pointer;
}
.notifCheck {
display: none;
}
.notifCheck:checked + label {
background: #aaa;
}
.notifCheck:checked .notificationImgs {
background-color: #aaa;
width: 100%;
height: 70px;
}
<input type="checkbox" name="checkText" class="notifCheck" id="checkText" value="text">
<label for="checkText">
<img src="https://images.pexels.com/photos/67636/rose-blue-flower-rose-blooms-67636.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="Text Notifications" class="notificationImgs">
</label>
<input type="checkbox" name="checkEmail" class="notifCheck" id="checkEmail" value="text">
<label for="checkEmail">
<img src="https://images.pexels.com/photos/67636/rose-blue-flower-rose-blooms-67636.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="Email Notifications" class="notificationImgs">
</label>
In your example code, you only have one checkbox input present. The 2nd label indicates you most likely meant to include a 2nd input checkbox with the id of #checkEmail.
The space (descendant selector) only selects all elements that are 'descendants of a specified element.
So, .notifCheck:checked .notificationImgs does not compute because notificationImgs is not a descendant of notifCheck.
The '+' (adjacent sibling selector) only selects elements that are are adjacent siblings.
So, notifCheck:checked + label is only selecting the 2nd label element, as this element immediately follows the .notifCheck element.
I am trying to make a collapsible button with pure HTML and CSS. Here is what I have:
#hidden {
display: none;
height: 100px;
background: red;
}
:checked+#hidden {
display: block;
}
<input type="checkbox" id="my_checkbox" style="display:none;">
<div id="hidden"></div>
<label for="my_checkbox">Show/hide</label>
This works. However, I want the hidden div to come after the button instead of before. When I move the div to after the checkbox label, it does not work.
How can I fix this ?
Thanks!
You want to use a different CSS selector. The below uses the General sibling combinator to target the div no matter its order with respect to the input element (so long as it follows it).
#hidden {
display: none;
height: 100px;
background: red;
}
:checked ~ #hidden {
display: block;
}
<input type="checkbox" id="my_checkbox" style="display:none;">
<label for="my_checkbox">Show/hide</label>
<div id="hidden"></div>
use negation instead of +, so that it will select all divs related to that class name
#hidden {
display: none;
height: 100px;
background: red;
}
:checked~#hidden {
display: block;
}
<input type="checkbox" id="my_checkbox" style="display:none;">
<label for="my_checkbox">Show/hide</label>
<div id="hidden"></div>
I want to both style a label and reveal a div when a checkbox is checked. I have found ways to do one or the other, but not both, and I don't understand why. Note the CSS is the exact same in both examples and is:
input {
display: none
}
.layerpic {
height: 100px;
width: 100px;
background: red;
opacity: 1;
float: left;
}
label {
clear: none;
float: left;
}
.xyz input:checked + .layerpic{
opacity: 0.5;
}
.xyz input:checked + label {
font-weight: bold;
}
Now the HTML. This example allows the div to be affected, but not the label. Note the only difference is in the html where the label comes before the input:
<div class ="xyz">
<label class="ribs" for="hider2">Hide layer</label>
<input type="checkbox" id="hider2">
<div class="layerpic"> hi
</div>
</div>
This example allows the label to be affected, but not the div:
<div class ="xyz">
<input type="checkbox" id="hider2">
<label class="ribs" for="hider2">Hide layer</label>
<div class="layerpic"> hi
</div>
</div>
Can anyone explain why different elements are affected just by flipping which HTML comes first, and is it possible to have both elements affected when the checkbox is checked?
You need to rearrange your HTML, then use + and ~ selectors
input {
display: none
}
.layerpic {
height: 100px;
width: 100px;
background: red;
opacity: 1;
float: left;
}
label {
clear: none;
float: left;
}
.xyz input:checked+.layerpic {
opacity: 0.5;
}
.xyz input:checked~label {
font-weight: bold;
}
<div class="xyz">
<input type="checkbox" id="hider2">
<div class="layerpic"> hi</div>
<label class="ribs" for="hider2">Hide layer</label>
</div>
For those not familiar, the checked attribute for a checkbox will accept any input as a sign to check the box. in fact, it doesnt need any text. so all these will check the box
<input type="checkbox" checked />
<input type="checkbox" checked="false">
<input type="checkbox" checked="">
<input type="checkbox" checked="0">
all those WILL check the box.
My problem is i am being handed a checked box, and need to uncheck it. I cant just change its value - that still makes it checked. i need to nuke it from orbit. This is incredibly easy to do with javascript or jQuery, but the site does not allow any of that in my CSS.
I read a list of about 100 attributes and how to reset them - auto, normal, 0, inherit, et cetera, but 'checked' was not on the list, and i tried all of those and anything i could think of, and this checkmark wont die.
The simple answer is NO, CSS cannot help you uncheck the checkbox..
BUT
You can use CSS to detect whether the input element is checked or not by using :checked and :not(:checked) ..
Test Case : Demo
HTML
<ul>
<li>
<input type="checkbox" checked />
<label for="">Checked</label>
</li>
<li>
<input type="checkbox">
<label for="">Unchecked</label>
</li>
<li>
<input type="checkbox" checked>
<label for="">Checked Again</label>
</li>
</ul>
CSS
input:checked + label {
color: green;
}
input:not(:checked) + label {
color: red;
}
CSS is not for dom manipulation, its for dom styling and arrangements, You can not set dom attributes from css but you can check for css conditions and set styles. :)
Its possible to check/uncheck checkbox using jquery. Here is the code:
$('#textCheckbox').attr('checked', true); // Enable CheckBox
$('#textCheckbox').attr('checked', false); // Disable CheckBox
After alteration, following will be the output of your input field
<input type="checkbox" id="textCheckbox" checked="checked" /> <!-- Checked -->
<input type="checkbox" id="textCheckbox" /> <!-- Unchecked -->
Fill free to mark correct answer if you think this is what you are looking at..
Question was asked 6 years ago but it should be noted you can give the appearance of an unchecked checkbox with CSS:
* {
margin: 0;
padding: 0;
font-family: Arial;
text-transform: capitalize;
}
html, body { padding: 1rem; }
input { display: none; }
label, label::after, hr, hr::after {
display: block;
overflow: visible;
position: relative;
transform: scale( 0.8 );
width: 1rem;
height: 1rem;
border-style: none;
border-radius: 0.125rem;
box-shadow: 0rem 0rem 0rem 0.0625rem #666;
content: '';
}
label::after, hr, hr::after {
transform: scale( 1.15 );
background-color: #07f;
box-shadow: none;
}
label::after { display: none; }
label:hover { box-shadow: 0rem 0rem 0rem 0.0625rem #222; }
label:hover::after { background-color: #06e; }
label:active::after { background-color: #18f; }
<style>
hr, hr::after {
position: absolute;
top: 0; left: 0.5rem;
z-index: 1;
transform: rotate( 40deg );
width: 0.225rem;
height: 0.9rem;
background-color: #fff;
border-radius: 0;
}
hr::after {
top: 0.6rem; left: -0.17rem;
transform: rotate( -90deg );
height: 0.4rem;
}
#one:checked ~ [ for='one' ]::after { display: block; }
[ for='two' ]::after { display: block; }
#one:checked ~ [ for='two' ]::after { display: none; }
</style>
<input type='checkbox' id='one'>
<input type='checkbox' id='two' checked>
<p>check me</p>
<label for='one'> <hr> </label>
<br>
<p>to uncheck me</p>
<label for='two'> <hr> </label>
Is there any way to make a radio button bigger using CSS?
If not, how else can I do it?
Try this code:
input[type='radio'] {
transform: scale(2);
}
You can easily able to set it's height and width as with any element.
Here is the fiddle with code
JSFIDDLE BIG RADIO BUTTON
HTML
<input id="r1" type="radio" name="group1" class="radio1" />
<label for="r1">label 1 text</label>
<input id="r2" type="radio" name="group1" class="radio2" />
<label for="r2">label 2 text</label>
<input id="r3" type="radio" name="group1" class="radio3" />
<label for="r3">label 3 text</label>
<input id="r4" type="radio" name="group1" class="radio4" />
<label for="r4">label 4 text</label>
CSS
input[type=radio] {
display: none;
}
input[type=radio] + label::before {
content: '';
display: inline-block;
border: 1px solid #000;
border-radius: 50%;
margin: 0 0.5em;
}
input[type=radio]:checked + label::before {
background-color: #ffa;
}
.radio1 + label::before {
width: 0.5em;
height: 0.5em;
}
.radio2 + label::before {
width: 0.75em;
height: 0.75em;
}
.radio3 + label::before {
width: 1em;
height: 1em;
}
.radio4 + label::before {
width: 1.5em;
height: 1.5em;
}
Styling radio button is not easy.
Form elements in general are either problematic or impossible to style using CSS alone.
Just go through this link for your own style with bigger size for radio buttons..
Also look at this link...
Bigger radio buttons
Don't use transform: scale(1.3), it really looks horrible. Just use this:
input[type='radio'] {
height: 15px;
width: 15px;
vertical-align: middle;
}
<input type="radio">Select this item
You can do it using CSS but browser and OS also impact on this. Look at following article.
Styling radio buttons with CSS
Try this:
HTML
<label>
<input type="radio" value="1">
<div></div>
</label>
CSS
input[type="radio"] {
display: none;
}
input[type="radio"] + div {
height: 20px;
width: 20px;
display: inline-block;
cursor: pointer;
vertical-align: middle;
background: #FFF;
border: 1px solid #d2d2d2;
border-radius: 100%;
}
input[type="radio"] + div:hover {
border-color: #c2c2c2;
}
input[type="radio"]:checked + div {
background:gray;
}
DEMO: http://jsfiddle.net/nuzhysgg/
There might be some quirky <span> tricks inside radio elements but I imagine using them across different browsers would be annoying to debug.
I've used this script in the past but not recently.
CSS3 transform scale is blurry. Setting height & width does not work with FF (even the newest 66 does not support, 2020). The only cross-browser solution is custom HTML markup + CSS, which unfortunatelly is not the easiest way. See helpful tutorial custom radios & checkboxes.