This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Is there a "previous sibling" selector?
(30 answers)
Closed 4 years ago.
Is this impossible? I've created a responsive menu system, but the :checked event of my checkbox doesn't get caught in my css
CSS
#sectiona:checked + #asection {
display: block;
}
HTML
<li >
<label for="sectiona" class="showhide">Search for a term</label>
<input type="checkbox" id="sectiona" value="button" style="display:none;"/>
</li>
I've see something like this before. If this isn't possible, I'll have to redesign my menus. It has to be HTML and CSS. No Javascript is allowed...
If undersood correctly you trying to show/hide checkbox input based on label click.
the visibility could do the trick.
Css:
input#sectiona:checked {
visibility: visible;
}
input#sectiona {
visibility: hidden;
}
<li >
<label for="sectiona" class="showhide">Search for a term</label>
<input type="checkbox" id="sectiona" value="button" />
</li>
in that case you can also add display: block;
input#sectiona:checked {
visibility: visible;
display: block;
}
Related
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)
Closed 2 years ago.
I have label and email input like this:
#emaillabel {
color: red;
visibility: hidden;
}
#email:focus~#emaillabel {
visibility: visible;
}
<label id="emaillabel">Email</label>
<input id="email" type="email" required placeholder="Email">
The problem is that label is shown in expected behaviour only when i put it html code after input.
In other words when i click on input i want see label that are before input.
I can use only css
Given your current HTML structure, what you want to do CSS-wise is not possible, because you can only select forward.
That is, CSS has no previousSibling or parent/ancestor selector.
As a sidenote, your label is missing the for-attribute which works as the connector between the two.
What you can do is wrap both in a div (if it isn't already wrapped in another element, in that case just use that element) and use :focus-within pseudo class on the parent. Browser support for :focus-within is very broad, with IE being the usual exception. To make sure only browsers that support :focus-within hide the label, wrap the CSS declarations in a
#supports selector(:focus-within)
block:
.input-label-group label {
color: red;
}
#supports selector(:focus-within) {
.input-label-group label {
visibility: hidden;
}
.input-label-group:focus-within label {
visibility: visible;
}
}
<div class="input-label-group">
<label id="emaillabel" for="email">Email</label>
<input id="email" type="text" required placeholder="Email">
</div>
To make things a little more intuitive, you could - instead of switching visibility - also apply fadein/out using opacity and transition:
.input-label-group label {
color: red;
}
#supports selector(:focus-within) {
.input-label-group label {
opacity: 0;
transition: opacity 0.2s ease-in-out;
}
.input-label-group:focus-within label {
opacity: 1;
}
}
<div class="input-label-group">
<label id="emaillabel" for="email">Email</label>
<input id="email" type="text" required placeholder="Email">
</div>
This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Closed 3 years ago.
How to target the sibling of the parent from a child? I'm trying to make a pure css hamburger menu and i have this checkbox input inside a label where it's :before content is a trigram.
on desktop view the ul list is displayed block and none on mobile. Now the show and hide is working when the ul is a direct sibling of the input box, the tilde selector works.
However what i wanted to do is take out the ul inside the label to be it's own sibling so the input is the only child left of the label. With the ul out, the show and hide doesn't work anymore.
how do i target the ul when the input is checked?
HTML:
<nav>
<label class="nav-toggler">
<input type="checkbox" />
</label>
<ul class="nav-links">
<li>link</li>
<li>link</li>
<li>link</li>
</ul>
</nav>
SCSS:
.nav-toggler{
&:before{
content:'\2630';
}
input{
opacity: 0;
width: 0;
height: 0;
&:checked ~ .nav-links{
display: block;
}
}
}
You should use the label and input with an id and a for attribute
looking for a parent selector is a wrong method and impossible in CSS see Is there a CSS parent selector?
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label
Associating a <label> with an <input> element offers some major advantages:
The label text is not only visually associated with its corresponding text input; it is programmatically associated with it too. This means that, for example, a screenreader will read out the label when the user is focused on the form input, making it easier for an assistive technology user to understand what data should be entered.
You can click the associated label to focus/activate the input, as well as the input itself. This increased hit area provides an advantage to anyone trying to activate the input, including those using a touch-screen device.
Your HTML can become
<nav>
<input type="checkbox" id="myId" />
<label class="nav-toggler" for="myId">
</label>
<ul class="nav-links">
<li>link</li>
<li>link</li>
<li>link</li>
</ul>
</nav>
and the (S)CSS rules
.nav-toggler{
&:before{
content:'\2630';
cursor:pointer;
}
}
input{
opacity: 0;
width: 0;
height: 0;
&:checked ~ .nav-links{
display: block;
}
}
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 3 years ago.
When I'm nesting my checkboxes in a <div> element my code stops working. I need images to display upon checking a certain checkbox. Code works without nesting the checkboxes in a <div>.
I want my checkboxes to be nested in a <div> so that it changes the display value from none to inline-block in the <div> with the images upon selecting a certain checkbox.
I've tried everything but I think I just can't get my head around this problem.
<body>
<style>
input[type=checkbox]:checked.ivo ~ div #fotoivo {
display: inline-block;
}
input[type=checkbox]:checked.ian ~ div #fotoian {
display: inline-block;
}
input[type=checkbox]:checked.non ~ div #fotonon {
display: inline-block;
}
#fotoivo {
width: 200px;
display: none;
}
#fotoian {
width: 200px;
display: none;
}
#fotonon {
width: 200px;
display: none;
}
</style>
<!--Checkboxes (works without the <div> wrapped around)-->
<!--<div>-->
Ivo: <input type="checkbox" name="ivoian" class="ivo">
Ian: <input type="checkbox" name="ivoian" class="ian">
Non-binair: <input type="checkbox" name="ivoian" class="non">
<!--</div>-->
<!--Images that should change from display: none to inline-block.-->
<div>
<img id="fotoivo" src="ivo.jpg" alt="#">
<img id="fotoian" src="ian.jpg" alt="#">
<img id="fotonon" src="non.jpg" alt="#">
</div>
</body>
Tilda (~) is sibling selector. When you nest the inputs, the div wrapping images isn't their sibling anymore. As CSS doesn't have parent selector, you aren't able to do it with only css. But of course, you can do it with simple JS.
You can do something like this: https://stackoverflow.com/a/7691692/11404579
Edit: also, there is little bit hacky way to do it with just CSS, but you will lose the native inputs. You can place the inputs in root element, so you can select the images with sibling selector and hide the inputs. And in some nested element place labels for those hidden inputs. This way, when you click on (nested) label, you check the input a can control the content.
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)
Closed 3 years ago.
I want to change property of sibling element when input is focused, but it doesn't work. What am I doing wrong?
.sibling {
width: 100px;
height: 100px;
background-color: blue;
}
input:focus ~ .sibling {
background-color: red;
}
<div class="sibling"></div>
<input type="text">
Thanks in advance!
Just change your html order
<input type="text">
<div class="sibling"></div>
This question already has answers here:
Show child element on parent hover in CSS
(4 answers)
Closed 4 years ago.
I need to show a checkbox (which is hidden by default) when hovering over a div.
This is my html:
<div class="todoLeftImportance">
<input type="checkbox" id="todoCheckbox">
</div>
When I hover over the 'todoLeftImportance' div I would like to show the 'todoCheckbox' that is inside of it.
I've played around with "display: block" and "display: none" but I don't know how to link the div and checkbox together via css.
Css for your reference, hope it help:
#todoCheckbox{
display: none;
}
.todoLeftImportance:hover #todoCheckbox{
display:block;
}
Here you go.
<html>
<head>
<style>
.todoLeftImportance{
opacity: 0.0;
transition: all 300ms linear;
}
.todoLeftImportance:hover{
opacity: 1.0;
}
</head>
</style>
<body>
<div class="todoLeftImportance">
<input type="checkbox" id="todoCheckbox">
</div>
</body>
</html>
Add this CSS
#todoCheckbox{ visibility: hidden; }
.todoLeftImportance:hover #todoCheckbox{ visibility: visible; }