Can I use selectors to control this checkbox hack across divs? - html

I have a very basic checkbox hack I'm using for selecting different items.
It does exactly what I want it to do: namely, clicking on the label allows me to select the corresponding figure:
input[type="checkbox"] { display:none; }
.wrap { width: 50%; }
.wrap label { display: inline-block;}
.checker {background: red; padding: 50px;}
.checker figure { margin: 10px; display: inline-block; position: relative;}
.wrap input { display: none; }
.wrap input:checked ~ .checker { display: none; }
.wrap input:checked + label { color: blue; }
.wrap #check1cont:checked ~ .check1 {display: inline-block;}
.wrap #check2cont:checked ~ .check2 {display: inline-block;}
<div class="wrap">
<input type="checkbox" name="cont" id="check1cont">
<label class="check1cont" for="check1cont">Check 1</label>
<input type="checkbox" name="controllers" id="check2cont">
<label class="check2cont" for="check2cont">Check 2</label>
<figure class="checker check1">CHECK 1</figure>
<figure class="checker check2">CHECK 2</figure>
</div>
But I want the controllers/labels to be aligned on the left side, with the figures on the right. Ideally, I'd like to be able to do this with flex, so the labels are housed in a responsive, left-aligned div.
I've tried adding the divs with different sibling and child selectors, but I'm new to all of this, and I don't think I've gotten the right combination (or else I'm doing something else wrong, or else it's impossible).
Can anyone recommend a means of doing this?
Thank you in advance.

The hidden checkbox tricks works because the checkboxes themselves can be anywhere in relation to the labels. So put the checkboxes first, and the labels in a separate container following them, and you can control both the labels and the figures with the :checked state of the checkboxes.
.wrap { display:flex; width:50%; vertical-align:top;}
.wrap aside {vertical-align:top;}
.wrap label {white-space:nowrap; display: block;}
.checker {background: red; padding: 50px; vertical-align:top;}
.checker {margin: 10px; display: inline-block; position: relative;}
.wrap input { display: none; }
input:checked ~ main .checker { display: none; }
#check1cont:checked ~ aside .check1cont,
#check2cont:checked ~ aside .check2cont { color: blue; }
#check1cont:checked ~ main .check1,
#check2cont:checked ~ main .check2 {display: inline-block;}
<div class="wrap">
<input type="checkbox" name="cont" id="check1cont">
<input type="checkbox" name="controllers" id="check2cont">
<aside>
<label class="check1cont" for="check1cont">Check 1</label>
<label class="check2cont" for="check2cont">Check 2</label>
</aside>
<main>
<figure class="checker check1">CHECK 1</figure>
<figure class="checker check2">CHECK 2</figure>
</main>
</div>
Note: as was remarked in the comments, the code is a simplification of the real code, so I had to clean up the css a bit. You probably have to un-clean it up to make it work for the real code again!

Related

Trying to create a basic dropdown menu with basic CSS, won't open up when clicked on

So far I've tried to use this code:
CSS Code:
/*global*/
html,
body {
width: 100;
margin: 10;
padding: 10;
font-family: Avenir, sans-serif;
}
/*functions*/
.multi-level,
.item ul,
.nav input[type="checkbox"] {
display: none;
}
#menu:checked ~ .multi-level,
.item input:checked ~ ul {
display: block;
}
HTML :
<body>
<p style="font-family: 'Avenir'; text-align: center">
"What is this website about?" Info about select Macs and frequently asked
questions.
</p>
<div class="nav">
<input type="checkbox" id="menu" />
<label for="menu">&#9776</label>
</div>
<div class="multi-level">
<div class="item">
<input type="checkbox" id="A" />
<label for="A">Mac Mini</label>
<ul>
<li>2005-2006 (PPC)</li>
<li>2006-2010 (Polycarbonate Mini*)</li>
<li>2010-2018 (Aluminium Mini*)</li>
<li>2020-Present (M1 Mini)</li>
</ul>
</div>
</div>
</body>
The dropdown menu isn't working for some reason, I've tried adding visibility: visible; and visibility: show; doesn't seem to do anything.
I've tried to use Firefox and Safari, doesn't work on either of them could it be a browser related issue?
Note: I'm on the latest versions of both of them.
Your problem is that this selector doesn't select anything:
#menu:checked ~ .multi-level {
display: block;
}
Your div.multi-level isn't a sibling of #menu, it's a sibling of .nav. But there's no CSS parent selector, so the only way to really fix this problem with a pure CSS solution is to restructure your HTML. You could get rid of div.nav, or you could move div.multi-level inside of div.nav right after label for="menu" or whatever else works for you.
Alternatively, you can use a JavaScript solution to toggle a class on one or more elements.

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>

close a div when a checkbox clicked with pure css

I am trying to close a div when a checkbox is clicked with css only not JQuery or Javascript but it seems not working properly. How can I adjust it?
div[id^="div-"] {
display: block;
}
div[id^="div-"]:target {
display: none;
}
<input type="checkbox" checked>
<div id="div-1">
Here is the content.
</div>
How can I link the <a> click and the checkbox?
I think the only way to do this with pure css would be to have the checkbox as a direct sibling to the div:
#div-1 {display:none}
#checkbox:checked + #div-1 {display:block;}
<input id="checkbox" type="checkbox" checked>
<div id="div-1">
Here is the content.
</div>
#text{
width:100px;
height:100px;
background:black;
}
input[type=checkbox]:checked ~ #text{
display:none;
}
<input type="checkbox" name="check" value="checked">Click here<br>
<div id="text"></div>
Using only CSS you can do something like this.
JSFiddle
The + is the adjacent sibling selector, more info at https://developer.mozilla.org/en/docs/Web/CSS/Adjacent_sibling_selectors
#close + #div-1 {
display: none;
}
#close:checked + #div-1 {
display: initial;
}
<input id="close" type="checkbox" checked />
<div id="div-1">Here is the content.</div>
First you should remove the anchor and just let the input element because this trick that i'm showing needs elements in the same level or the second element be in lower levels of html structure.
<input type="checkbox" checked>
<div id="div-1">
Here is the content.
</div
css
div[id^="div-"] {
display: block;
}
input:checked ~ div[id^="div-"] {
display: none;
}
jsfiddle

How to align checkboxes horizontally with CSS

I am trying to horizontally align the checkboxes that appear above the dropdowns on the yellow search bar on this page.
I thought that inline block CSS might be the right way to do this, but I can't seem to get it to work.
I'd be most grateful to know if you have any suggestion of the best way to do this?
Thank you!
A very modern and flexible solution would be to use flexbox. E.g.:
ul.categorychecklist {
list-style:none;
display: flex;
flex-wrap: nowrap;
flex-direction: row;
}
This is how we do it in 2016 :)
For more details see: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
ul.categorychecklist li {
display: inline-block;
}
Add this code in your CSS file to make the check-boxes inline.
Yes display inline-block is the right way to do this . but you have to add it at the right place
Please add this in your css
.categorychecklist li {
display: inline-block;
}
Try this: this code make your checkbox horizontally.
ul.categorychecklist {
list-style: none;
margin: 0;
padding: 0;
}
ul.categorychecklist li {
float: left;
padding-right: 10px;
}
Another option
.popular-category {
display: inline;
}
you may also want to add a padding-right: 15px; on the above
For ex. by doing:
ul.categorychecklist { // .categorychecklist - will apply only to this menu
list-style: none; // remove discs
}
ul.categorychecklist li {
display: inline-block; // order inline
}
Your code will go as follows
<style>
.row-cb {width: 285px;margin: auto; }
.row-cb label { float: left; }
.row-cb span { float: left; text-align: left; }
.clear-both {clear: both}
</style>
<div class="row-cb">
<span><input name="option" id="cb1" type="checkbox" /></span>
<label for="cb1">checkbox-001</label>
<div class="clear-both"></div>
</div>
<div class="row-cb">
<span><input name="option" id="cb2" type="checkbox" /></span>
<label for="cb2">checkbox-002</label>
<div class="clear-both"></div>
</div>
<div class="row-cb">
<span><input name="option" id="cb3" type="checkbox" /></span>
<label for="cb3">checkbox-003</label>
<div class="clear-both"></div>
</div>
<div class="row-cb">
<span><input name="option" id="cb4" type="checkbox" /></span>
<label for="cb4">checkbox-004</label>
<div class="clear-both"></div>
</div>
You can also make the trick using float, as you could see in this fiddle:
https://jsfiddle.net/v7thvxjj/2
Note: remember to remove empty ul from the HTML code

Display a div when an input div is focused

I want to display a div when an input field is focused(active?)
In the following code, I want to display .text when .input is focused.
I'm trying this:
HTML :
<div class="box">
<input class="input" type="text" value="" />
<div class="text">text</div>
</div>
CSS :
.text { display: none; }
.input:focus .text { display: block; }
Example :
JSFiddle
You need to use the sibling CSS selector:
.input:focus + .text{
display: block;
}
Updated Fiddle
You can also use general sibling selector ~ :
.input:focus ~ .text { display: block; }
JSFiddle