css rule for checkbox is being ignored by browser [duplicate] - html

This question already has answers here:
What does the "~" (tilde/squiggle/twiddle) CSS selector mean?
(3 answers)
Is there a CSS parent selector?
(33 answers)
Closed 6 days ago.
I have a div named "popup" which is used to show popup menu and has checkbox with label "more" which is used to get some more items for menu, it displays additional list.
Even if i change "#popup" or "overflow-y: scroll;" - nothing has changed, browser ignores this rule.
#popup {
... height: 13em;
overflow: hidden;
...
}
#popup_more {
visibility: hidden;
}
#popup_more_checkbox:checked~#popup_more {
visibility: visible;
}
#popup_more_checkbox:checked~#popup_more_checkbox_label {
visibility: hidden;
}
#popup_more_checkbox:checked~#popup {
overflow-y: scroll;
}
<div id="popup">
<ul id="popup_list">
...
</ul>
<input id="popup_more_checkbox" type="checkbox" style="display: none;" />
<label id="popup_more_checkbox_label" for="popup_more_checkbox">More</label>
<div id="popup_more">
<ul id="popup_more_list">
...
</ul>
</div>
</div>

As j08691 commented, ~ is the general sibling selector and #popup isn't a sibling of #popup_more_checkbox. For what you're trying to achieve use the new :has() selector in CSS:
#popup {
... height: 13em;
overflow: hidden;
...
}
#popup_more {
visibility: hidden;
}
#popup_more_checkbox:checked~#popup_more {
visibility: visible;
}
#popup_more_checkbox:checked~#popup_more_checkbox_label {
visibility: hidden;
}
#popup:has(#popup_more_checkbox:checked) {
overflow-y: scroll;
}
<div id="popup">
<ul id="popup_list">
...
</ul>
<input id="popup_more_checkbox" type="checkbox" style="display: none;" />
<label id="popup_more_checkbox_label" for="popup_more_checkbox">More</label>
<div id="popup_more">
<ul id="popup_more_list">
...
</ul>
</div>
</div>

From MDN:
General sibling combinator
If you want to select siblings of an element even if they are not directly adjacent, then you can use the
general sibling combinator (~). To select all <img> elements that
come anywhere after <p> elements, we'd do this: p ~ img
And that's not the case in #popup_more_checkbox:checked ~ #popup.

Related

Why Does The Selector Not Change The Element?

Following the selector rules, when checking whether the checkbox is checked, the ~ selector should apply display: none; in the span elements, but this is not happening.
both the input has the same parent element
and spans are preceded by input.
Because it does not work?
https://codepen.io/fx-hunter/pen/MWBaPBW?editors=1100
I tried this
.wrapper .content-input .botao:checked~.wrapper .content-input span {
display: none;
}
<div class="wrapper">
<div class="content-input">
<input class="botao" type="checkbox" />
<span>off</span>
<span>on</span>
</div>
</div>
This is happening because you are using ~ selector with intention of getting the ancestor but ~ selector is to get sibling of previous selector.
To fix this use
.wrapper .content-input .botao:checked ~ span {
display: none;
}
The two sequences share the same parent in the document tree, so there's no need to call the span by saying .wrapper .content-input span. Just saying span works fine!
.wrapper .content-input .botao:checked ~ span {
display: none;
}
Its working but you are targetting elements wrong. So, its causing the issue.
/*
.botao:checked {
display: none;
}
*/
.botao:checked ~ span {
display: none;
}
<div class="wrapper">
<div class="content-input">
<input class="botao" type="checkbox">
<span>off</span>
<span>on</span>
</div>
</div>

How can I show element next to image when image is hovered? [duplicate]

This question already has answers here:
How to affect other elements when one element is hovered
(9 answers)
Closed 9 months ago.
I want to show a message when hovering on an image using CSS. I have used the following code :
Code:
.message {
visibility: hidden;
}
.xyz:hover .message {
visibility: visible;
}
<div class="abc">
<img class="xyz">
<span class="message">Message</span>
</div>
But it's not working.
Next sibling style
.xyz:hover + .message
<div class="abc">
<img class="xyz">
<span class="message">Message</span>
</div>
<style>
.message {
visibility: hidden;
color: #000;
}
.xyz {
min-width: 20px;
min-height: 20px;
}
.xyz:hover+.message {
visibility: visible;
}
}
</style>
Use this way
.xyz:hover + .message{
visibility: visible;
}
.message {
visibility: hidden;
}
.xyz:hover+.message {
visibility: visible;
}
<div class="abc">
<img class="xyz" alt="xyz">
<span class="message">Message</span>
</div>

Using the CSS checkbox hack, how can I affect more than one element when :checked? [duplicate]

This question already has answers here:
Is there a "previous sibling" selector?
(30 answers)
What does the "~" (tilde/squiggle/twiddle) CSS selector mean?
(3 answers)
What does the "+" (plus sign) CSS selector mean?
(9 answers)
CSS transform doesn't work on inline elements
(2 answers)
Closed 1 year ago.
I currently have something like this
on jsfiddle
HTML:
.ln-header {
cursor: pointer;
}
.ln-header::after {
content: "+";
margin-left: 10px;
transition: 400ms;
}
.list-wrap {
display: none;
}
.checkbox1:checked + .list-wrap {
display: block;
}
.checkbox1:checked + .ln-header::after {
transform: rotate(45deg);
}
.checkbox1:checked + .ln-wrap::after {
transform: rotate(45deg);
}
.checkbox1 {
display: none;
}
<div>
<div>
<label for="list1" class="localnav-categories-label">
<span class="ln-header">categories</span>
</label>
<input type="checkbox" id="list1" class="checkbox1">
<nav class="list-wrap">
<ul>
<li>
one
</li>
<li>
two
</li>
</ul>
</nav>
</div>
</div>
I'm trying to get the plus sign (as an ::after element) to rotate, preferably with an animation, while a list pops up. Currently, I am only able to get the list to appear, while the plus sign remains static. I've heard about the checkbox only affecting its sibling element (may be wrong), but after playing around with the location, where I have it now seems to be the only place that it'll work for the list to appear.
How could I make it so that when I check the checkbox, both the list appears and the '+' rotates simultaneously?
Thanks!
First, you need to bring <input type="checkbox" id="list1" class="checkbox1"> to the front of anything you want to control the style of, since sibling selectors can only select forwards.
Second, you need to use ~ instead of + to select siblings that do not have to be right next to each other.
Third, you can't rotate an inline element(the default display of pseudo elements is inline), so you need to change the display of .ln-header::after to inline-block or inline-flex etc.
Finally, since .ln-header is a deeper element, you need to select it with a descendant selector .checkbox1:checked ~ * .ln-header::before
Then there you have it:
.ln-header {
cursor: pointer;
}
.ln-header::after {
content: "+";
margin-left: 10px;
transition: 400ms;
display: inline-block;
}
.list-wrap {
display: none;
}
.checkbox1:checked ~ .list-wrap {
display: block;
}
.checkbox1:checked ~ * .ln-header::after {
transform: rotate(45deg);
}
.checkbox1 {
display: none;
}
<div>
<div>
<input type="checkbox" id="list1" class="checkbox1">
<label for="list1" class="localnav-categories-label">
<span class="ln-header">categories</span>
</label>
<nav class="list-wrap">
<ul>
<li>
one
</li>
<li>
two
</li>
</ul>
</nav>
</div>
</div>

Problem loading a tooltip on an html page

I'm new with CSS, I'm trying to display an image with some text below when you hover over certain position on the page. The problem is, for some reason the image is not displaying, this is my code:
.foto-jb {
visibility: hidden;
}
.jb :hover .foto-jb {
visibility: visible;
opacity: 1;
}
<div class="member1">
<i class="name-person"></i>
<span class="jb">Name Person</span>
</div>
<div class="foto-jb">
<img src="./images/photo.png">
</div>
This line of code:
.jb :hover .foto-jb {
visibility: visible;
opacity: 1;
}
means, once .jb class is hovered look for .foto-jb class that is inside .jb class and make it visible.
So what you have to do actually is the following:
.member1:hover+.foto-jb
{
visibility: visible;
opacity: 1;
}
which means, once .member class is hovered look for .foto-jb class that is placed immediately after .member class and make it visible.
You may want to take a look at CSS Selectors it's really helpful.
Need to fix both HTML and CSS
.foto-jb {
visibility: hidden;
}
.jb:hover + .foto-jb {
visibility: visible;
}
<div class="member1">
<i class="name-person"></i>
<span class="jb">Name Person</span>
<div class="foto-jb">
<img src="https://dummyimage.com/300x150/000/fff">
</div>
</div>
Space is descendant selector and .jb :hover .foto-jb selects elements with .foto-jb class that are descendants of .jb class. Such elements don't exist in your case.
Move your <div class="foto-jb"> inside <div class="member1">, then use a combination of sibling (+) and descendant selector(space) to select the image, or only + to select the <div class="foto-jb">.
HTML:
<div class="member1">
<i class="name-person"></i>
<span class="jb">Name Person</span>
<div class="foto-jb">
<img src="./images/photo.png">
</div>
</div>
CSS:
.jb:hover + .foto-jb {
visibility: visible;
}
.jb:hover + .foto-jb selects the element that immediately follows the elements with .jb class (+ is the adjacent sibling selector).

HTML Collapsible Button - How to make div appear after button?

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>