Show div when checkbox id checked but under the all checkboxes - html

I have a form containing 20 checkboxes.
I want to show the content of a div when a checkbox is checked.
I got a result like that
#toggle-content1 {
display: none;
}
#mycheckbox1:checked~#toggle-content1 {
display: block;
height: 100px;
}
#toggle-content2 {
display: none;
}
#mycheckbox2:checked~#toggle-content2 {
display: block;
height: 100px;
}
<input type="checkbox" name="mycheckbox" id="mycheckbox1" value="0" />
<div id="toggle-content1">
This content should appear when the checkbox is checked
</div>
<input type="checkbox" name="mycheckbox" id="mycheckbox2" value="0" />
<div id="toggle-content2">
This content should appear when the checkbox is checked
</div>
But if you give it a try the code snippet, you'll notice that the first checkbox move the second one(when opened the div), is there a way to leave the checkbox in one row and display the divs on the others?
The css world is new to me, there is a way to write that code for 20 checkbox without write the css for every div?

You can try something like this:
.content {
display: none;
width: 100%;
}
/*Use + to target only one .content*/
input[type=checkbox]:checked + .content {
display: block;
height: 50px;
}
.container {
display: flex;
flex-wrap: wrap;
}
input[type=checkbox] {
order: -1; /*make all the input on the top*/
}
<div class="container">
<input type="checkbox" name="mycheckbox" value="0">
<div class="content">
0)This content should appear when the checkbox is checked
</div>
<input type="checkbox" name="mycheckbox" value="1">
<div class="content">
1)This content should appear when the checkbox is checked
</div>
<input type="checkbox" name="mycheckbox" value="2">
<div class="content">
2)This content should appear when the checkbox is checked
</div>
<input type="checkbox" name="mycheckbox" value="3">
<div class="content">
3)This content should appear when the checkbox is checked
</div>
</div>

I simply reordered your tags, input first then divs. But i'm not really sure that's what you want
#toggle-content1 {
display: none;
}
#mycheckbox1:checked~#toggle-content1 {
display: block;
height: 100px;
}
#toggle-content2 {
display: none;
}
#mycheckbox2:checked~#toggle-content2 {
display: block;
height: 100px;
}
<input type="checkbox" name="mycheckbox" id="mycheckbox1" value="0" />
<input type="checkbox" name="mycheckbox" id="mycheckbox2" value="0" />
<div id="toggle-content1">
This content should appear when the checkbox1 is checked
</div>
<div id="toggle-content2">
This content should appear when the checkbox2 is checked
</div>
http://jsfiddle.net/ScnQT/5539/

please have a look below updated code, hope you will find the solutions.
.chkBox{
display:inline-block;
position:relative;
}
.chkTxt{
background:#fff;
padding:10px;
border:1px solid #ccc;
width:500px;
height:100px;
position:absolute;
top:100%;
left:0;
display:none;
}
.chkBox input[type="checkbox"]:checked + .chkTxt{
display:block;
}
<div class="chkBox">
<input type="checkbox" name="mycheckbox" id="mycheckbox1" value="0" />
<div class="chkTxt">This content should appear when the checkbox1 is checked </div>
</div>
<div class="chkBox">
<input type="checkbox" name="mycheckbox" id="mycheckbox2" value="0" />
<div class="chkTxt">This content should appear when the checkbox2 is checked </div>
</div>
<div class="chkBox">
<input type="checkbox" name="mycheckbox" id="mycheckbox3" value="0" />
<div class="chkTxt">This content should appear when the checkbox3 is checked </div>
</div>
<div class="chkBox">
<input type="checkbox" name="mycheckbox" id="mycheckbox4" value="0" />
<div class="chkTxt">This content should appear when the checkbox4 is checked </div>
</div>
<div class="chkBox">
<input type="checkbox" name="mycheckbox" id="mycheckbox5" value="0" />
<div class="chkTxt">This content should appear when the checkbox5 is checked </div>
</div>

I would use a script for this...and in a real application, I would use a model + templating engine.
(Vue engine sample)
This way you can put your output wherever you want and format however you like it. This way you can also load a new datamodel from the server(store your checkbox questions/values/answers in db somwhere) and it would redraw/update your entire page without needing any updates from you.
// HTML
<input type="checkbox" name="mycheckbox" id="mycheckbox1" value="1" />
<input type="checkbox" name="mycheckbox" id="mycheckbox2" value="2" />
<input type="checkbox" name="mycheckbox" id="mycheckbox3" value="3" />
<input type="checkbox" name="mycheckbox" id="mycheckbox4" value="4" />
<div id="activeCheckboxes"></div><!-- you can put this anywhere on the page -->
// JavaScript
const checkboxes = {
1: {checked: false, message: 'First checkbox checked'},
2: {checked: false, message: 'Second checkbox checked'},
3: {checked: false, message: 'Third checkbox checked'},
4: {checked: false, message: 'Fourth checkbox checked'}
};
const activeCheckboxes = document.getElementById('activeCheckboxes');
const updateCheckboxes = () => {
activeCheckboxes.innerHTML = '';
Object.keys(checkboxes).forEach((val) => {
let checkbox = checkboxes[val];
if (checkbox.checked) {
activeCheckboxes.innerHTML += '<div>'+checkbox.message+'</div><hr>';
}
});
};
$(document).ready(() => {
$('input[type]="checkbox"').click((evt) => {
checkboxes[evt.target.value].checked = evt.target.checked;
updateCheckboxes();
});
});
Sample Fiddle

Related

Show/hide content on checkbox

i want to hide all content, i want them to show when checked
like show content when checkbox checked
hide when unchecked so that when i check others they show up too
here is my try but its not working
<style>
#myBike:not(:checked) +#bike {
display: block !important;
}
#myCar:not(:checked) +#car {
display: block !important;
}
</style>
<input type="checkbox" id="myBike">
<label for="vehicle1"> I have a bike</label><br>
<input type="checkbox" id="myCar">
<label for="vehicle2"> I have a car</label><br>
<div class="row" id="bike">
Something for bike
</div>
<div class="row" id="car">
Something for car
</div>
Please check the code below.
#bike {
display: none;
}
#myBike:checked~#bike {
display: block;
}
#car {
display: none;
}
#myCar:checked~#car {
display: block;
}
<input type="checkbox" id="myBike" />
<label for="vehicle1">I have a bike</label>
<input type="checkbox" id="myCar" />
<label for="vehicle2">I have a car</label>
<div class="row" id="bike">Something for bike</div>
<div class="row" id="car">Something for car</div>
Expanation:
You have used a wrong syntax ~ vs +
By default all div are set as display:block. You should set display:none as its initial.

How do I get label with checkbox to work?

I made a label because I wanted to apply css to the checkbox.
However, the label is not working except for the checkbox whose id is 'checkall'.
<div class="divCenter cartDiv">
<ul>
<li class="selectallforcart">
<div class="hanadiv">
<input type="checkbox" id="checkall" class="check" value="0"/>
<label for="checkall"></label> selelct All
</div>
</li>
</ul>
<% ArrayList<LikePdVO> likeList = (ArrayList<LikePdVO>)(request.getAttribute("likeList"));
for(LikePdVO vo : likeList) {
String arr[] = vo.getPhoto().split("\\*");
%>
<div class="cartProduct" id="<%=vo.getPd_id()%>">
<div class="fl cartCheck">
<input type="checkbox" class="check" name="<%=vo.getName()%>"
value="<%=vo.getOrder_price()%>" id="<%=vo.getPd_id()%>">
<label for="<%=vo.getPd_id()%>">
</label>
</div>
this is my code.
.cartDiv input[type=checkbox] + label {
display: inline-block;
width: 20px;
height: 20px;
border: 2px solid #bcbcbc;
cursor: pointer;
}
.cartDiv input[type=checkbox]:checked + label {
background-color: #866744;
}
.cartDiv input[type=checkbox] {
display: none;
}
And this is CSS.
When you mean that the label is not working, I assume you mean that clicking on it doesn't cause the checkbox to be checked?
Either way, the label's text needs to be within the element, so it should be:
<label for="checkall">Select all</label>
And your other labels contain no text, so the label itself is probably quite small, despite the CSS, and therefore not clickable.
You need to set label text in the label tag not out side of it.
<div class="divCenter cartDiv">
<ul>
<li class="selectallforcart">
<div class="hanadiv">
<input type="checkbox" id="checkall" class="check" value="0" />
<label for="checkall">selelct All</label>
</div>
</li>
</ul>
</div>

Add '*' to the label of input fields with the 'required' attribute with CSS

I am wondering if there is a way using only CSS to add an asterisk to a label element when the label's for attribute is for an input element with a required html attribute. Alternatively, we could use logic around if the label is directly followed by an input element which has the required attribute.
What does work is something like this:
input[required] + label:after {
content: '*';
color: red;
}
<form>
<div>
<input for="name" type="text" required />
<label id="name">Name</label>
</div>
<div>
<input for="age" type="text" />
<label id="age">Age</label>
</div>
</form>
But what I'm talking about is if the label and input are swapped around like the example below, which is more common. The CSS sibling selector + doesn't work for this case. Is there any way to do some like above when the label element is first and the input is second?
/* ??? */
<form>
<div>
<label for="name">Name</label>
<input id="name" type="text" required />
</div>
<div>
<label for="age">Age</label>
<input id="age" type="text" />
</div>
</form>
Thanks
I suggest you keep old html and use css flex to order position
input[required] + label:after {
content: '*';
color: red;
}
div {
display: flex;
text-align: center;
align-items: flex-start;
}
div input {
width: 100px;
height: 20px;
order: 2;
}
div label {
width: 100px;
height: 20px;
order: 1;
}
<form>
<div>
<input for="name" type="text" required />
<label id="name">Name</label>
</div>
<div>
<input for="age" type="text" />
<label id="age">Age</label>
</div>
</form>
See the codepen
You can use float for label to positining label at left like below:
label {
float: left;
}
input[required] + label:after {
content: '*';
color: red;
}
}
<form>
<div>
<input for="name" type="text" required />
<label id="name">Name</label>
</div>
<div>
<input for="age" type="text" />
<label id="age">Age</label>
</div>
</form>
There is no way for CSS to "render backwards" the way you want it too (not without SASS/SCSS).
One solution would be to add a "required" class to the labels that correspond to required fields.
<style>
label.required:after { content: '*';color:red; }
</style>
<form>
<div>
<label for="name" class="required">Name</label>
<input id="name" type="text" required />
</div>
<div>
<label for="age">Age</label>
<input id="age" type="text" />
</div>
</form>
Another option is the use of CSS3 Orders property, but the parent element would need to be set to "display:flex".
<style>
div { display:-webkit-flex;display:flex; }
div label { order:1; }
div input { order:2; }
div input[required] + label:after { content: '*';color:red; }
</style>
<form>
<div>
<input id="name" type="text" required />
<label for="name">Name</label>
</div>
<div>
<input id="age" type="text" />
<label for="age">Age</label>
</div>
</form>
EDIT 6/25/2019 SASS/SCSS Example
div {
display:-webkit-flex;
display:flex;
label {
order:1;
}
input {
order:2;
&[required] + label:after {
content: '*';color:red;
}
}
}
More information about Parent Selectors in SCSS.

Pure CSS Way to style radio button group when none of the radio buttons are checked?

Is there a pure CSS way to style a group of radio buttons such that they look a certain way when none of them are checked? This would be the default state (for legal reasons) of the radio buttons for a couple questions.
Overall, what I'm trying to do is have two conditions:
If none of the radio buttons are checked, show all three radio buttons in full color/opacity
If one of the radio buttons is checked, show that button in full color/opacity, but the other two slightly dulled/grayed.
It is easy to do condition #2 using the :checked selector. But that by itself will leave all three dulled/grayed in their default state. See the snippet for a very basic example.
I realize this can be done with javascript, and if I need to go that route I will. Just thought I'd see if there was a pure CSS way to accomplish it.
input[type=radio]:not(:checked),
input[type=radio]:not(:checked)+label {
opacity: 0.2;
}
<form>
<input type="radio" name="Question" value="Answer1" /> <label>Answer 1</label>
<input type="radio" name="Question" value="Answer2" /> <label>Answer 2</label>
<input type="radio" name="Question" value="Answer3" /> <label>Answer 3</label>
</form>
I think you could do this in JavaScript. Alone, I don't believe you could do this in CSS because what you need is for it to check if the radio-buttons are selected and you can't have that kind of logic in CSS.
Here is some code I have pulled together in JQuery (the easiest way to do it):
$(function() {
$("#radio1").click(function() {
$("#radio2Text, #radio3Text, #radio2, #radio3").css("opacity", "0.2");
$("#radio1, #radio1Text").css("opacity", "1");
});
$("#radio2").click(function() {
$("#radio1Text, #radio3Text, #radio1, #radio3").css("opacity", "0.2");
$("#radio2, #radio2Text").css("opacity", "1");
});
$("#radio3").click(function() {
$("#radio1Text, #radio2Text, #radio1, #radio2").css("opacity", "0.2");
$("#radio3, #radio3Text").css("opacity", "1");
});
});
#radio1Text, #radio2Text, #radio3Text {
opacity: 1;;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input type="radio" name="Question" value="Answer1" id = "radio1"> <label id = "radio1Text">Answer 1</label>
<input type="radio" name="Question" value="Answer2" id = "radio2"> <label id = "radio2Text">Answer 2</label>
<input type="radio" name="Question" value="Answer3" id = "radio3"> <label id = "radio3Text">Answer 3</label>
</form>
This isn't quite what you're looking for, because while this styling differentiates between checked and unchecked states, it does so not by opacity but by using different colours. Perhaps you can adapt it to suit your needs.
Hope it helps. here's a fiddle
[name=radio] {
display: none;
}
[for^=radio] {
display: inline-block;
vertical-align: top!important;
position: relative;
margin: 10px 15px 0px 15px;
width: 120px;
}
[for^=radio]:before {
display: inline-block;
content: '';
position: relative;
margin: 0px 5px -10px 5px;
width: 32px;
height: 32px;
background: red;
}
[type=radio]:checked + [for^=radio]:before {
background: green;
}
<input id=radio-1 type=radio name=radio />
<label for=radio-1>Answer 1</label>
<input id=radio-2 type=radio name=radio />
<label for=radio-2>Answer 2</label>
<input id=radio-3 type=radio name=radio />
<label for=radio-3>Answer 3</label>
I gave a try but I was only partially successful. This is not the correct answer, but it is in pure CSS.
input[type='radio']:not(:checked) + label
{
opacity:1;
}
input[type="radio"]:checked + label
{
opacity:1;
}
input[type="radio"]:checked ~ input[type="radio"] + label
{
opacity:0.5;
}
<form>
<div class="input-group">
<input type="radio" name="Question" value="Answer1" id="one" />
<label for="one">Answer 1</label>
<input type="radio" name="Question" value="Answer2" id="two" />
<label for="two">Answer 2</label>
<input type="radio" name="Question" value="Answer3" id="three" />
<label for="three">Answer 3</label>
</div>
</form>
A code pen of it is here.

Vertically responsive panel with overflow scroll

Could anyone give new ideas how to realize the following? If it generally possible).
The content of Left panel will be changed dynamically with Angular. So, we can have several items or, for example, 50 items on the panel. In accordance with that, the height of panel will be shorter or overflow hidden will be displayed.
Here is fiddle draft https://jsfiddle.net/b9on9gup/7/
First of all the div class="filter-title" should fill 100% height.
The second, title container shouldn't be in scrolling area. Scroll should be inside div class="radio-container". You could add class .shown on
div class="main-container" to display bottom panel.
Additional condition is good displaying with and without scroll (different quantity of items, different screen resolutions etc).
in fiddle I was trying different ways, so some css properties can be odd.
<body>
<div class = "main-container">
<div class="left-panel">
<div class="filter-container">
<div class="table">
<div class="table-row">
<div class="radio-container">
<div class="overflow">
<div>
<label>
<input type="radio" name="filterFieldsForm"/>
radio button
</label>
</div>
<div>
<label>
<input type="radio" name="filterFieldsForm"/>
radio button
</label>
</div>
<div>
<label>
<input type="radio" name="filterFieldsForm" />
radio button
</label>
</div>
<div>
<label>
<input type="radio" name="filterFieldsForm" />
radio button
</label>
</div>
<div>
<label>
<input type="radio" name="filterFieldsForm"/>
radio button
</label>
</div>
<div>
<label>
<input type="radio" name="filterFieldsForm" />
radio button
</label>
</div>
<div>
<label>
<input type="radio" name="filterFieldsForm"/>
radio button
</label>
</div>
<div>
<label>
<input type="radio" name="filterFieldsForm" />
radio button
</label>
</div>
<div>
<label>
<input type="radio" name="filterFieldsForm"/>
radio button
</label>
</div>
<div>
<label>
<input type="radio" name="filterFieldsForm"/>
radio button
</label>
</div>
<div>
<label>
<input type="radio" name="filterFieldsForm"/>
radio button
</label>
</div>
<div>
<label>
<input type="radio" name="filterFieldsForm"/>
radio button
</label>
</div>
<div>
<label>
<input type="radio" name="filterFieldsForm"/>
radio button
</label>
</div>
<div>
<label>
<input type="radio" name="filterFieldsForm"/>
radio button
</label>
</div>
<div>
<label>
<input type="radio" name="filterFieldsForm"/>
radio button
</label>
</div>
<div>
<label>
<input type="radio" name="filterFieldsForm"/>
radio button
</label>
</div>
<div>
<label>
<input type="radio" name="filterFieldsForm"/>
radio button
</label>
</div>
<div>
<label>
<input type="radio" name="filterFieldsForm"/>
radio button
</label>
</div>
<div>
<label>
<input type="radio" name="filterFieldsForm"/>
radio button
</label>
</div>
</div>
</div>
<div class="filter-title">
<span>
Filter title
</span>
</div>
</div>
</div>
</div>
</div>
<div class="bottom-panel"></div>
</div>
</body>
html, body {
height: 100%;
padding: 0;
position: relative;
margin: 0;
}
.main-container {
position: relative;
overflow: hidden;
height: 100%;
width: 100%;
.left-panel {
top: 0;
left: 0;
bottom: 0;
width: 300px;
background: #fff;
position: absolute;
transition: bottom 0.5s ease;
.filter-container {
position: absolute;
background: #F6F6F6;
top: 50%;
transform: translateY(-50%);
width: 100%;
.table {
display: table;
width: 100%;
.table-row {
display: table-row;
height: 100%;
.radio-container {
display: table-cell;
padding: 25px 25px;
display: table-cell;
vertical-align: middle;
.overflow {
overflow-y: scroll;
max-height: 100%;
}
}
}
.filter-title {
display: table-cell;
width: 20px;
background: #539ACC;
vertical-align: middle;
span {
-webkit-writing-mode: vertical-lr;
white-space: nowrap;
}
}
}
}
}
.bottom-panel {
height: 200px;
position: absolute;
bottom: -200px;
background: #F6F6F1;
width: 80%;
left: 0;
right: 0;
margin: auto;
transition: bottom 0.5s ease;
}
&.shown {
.left-panel {
bottom: 200px;
}
.bottom-panel {
bottom: 0;
}
}
}
UPDATE
It's a simple piece of javascript that you can edit to better fill your needs...
it changes the title height if necessary (it actually changes the element's width since the it's rotated 90deg)
var ftitle = document.querySelector('.filter-title');
var radiocont = document.querySelector('.radio-container');
var w = ftitle.clientWidth;
var h = radiocont.clientHeight;
if (h > w) { ftitle.style.width = h + 'px';}
.left-panel {
position: relative;
width: 150px;
}
/*
.radio-container {
height: 100px;
overflow: auto;
}
*/
.radio-container label {
display: block;
}
.filter-title {
background: #ddd;
text-align: center;
position: absolute;
top: 0; left: 0;
transform: translateX(170px) rotate(90deg);
transform-origin: 0 0;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div class = "main-container">
<div class="left-panel">
<div class="radio-container">
<label>
<input type="radio" name="filterFieldsForm"/>
radio button1
</label>
<label>
<input type="radio" name="filterFieldsForm"/>
radio button2
</label>
<label>
<input type="radio" name="filterFieldsForm" />
radio button3
</label>
<label>
<input type="radio" name="filterFieldsForm" />
radio button4
</label>
<label>
<input type="radio" name="filterFieldsForm"/>
radio button5
</label>
<label>
<input type="radio" name="filterFieldsForm" />
radio button6
</label>
<label>
<input type="radio" name="filterFieldsForm" />
radio button7
</label>
<label>
<input type="radio" name="filterFieldsForm"/>
radio button8
</label>
<label>
<input type="radio" name="filterFieldsForm" />
radio button9
</label>
<label>
<input type="radio" name="filterFieldsForm" />
radio button10
</label>
<label>
<input type="radio" name="filterFieldsForm"/>
radio button11
</label>
<label>
<input type="radio" name="filterFieldsForm" />
radio button12
</label>
<label>
<input type="radio" name="filterFieldsForm" />
radio button13
</label>
<label>
<input type="radio" name="filterFieldsForm" />
radio button14
</label>
</div>
<div class="filter-title">
<span>Filter title</span>
</div>
</div>
</div>
</body>
</html>
http://jsbin.com/wiyuhu/edit?css,js,output
The best decision I've found in my case is using max-height for div class= "overflow" and media-queries min-height.
I noticed scroll is displayed if to set max-height for div class= "overflow". But max-height should be at least in 'px', not in '%'.
Also max-height should be different for different resolutions. I set some breakpoints for max-height using media queries. Something like this:
#media(min-height:520px) {
max-height: 170px;
}
#media(min-height:600px) {
max-height: 250px;
}
#media(min-height:768px) {
max-height: 400px;
}
#media(min-height:900px) {
max-height: 500px;
}
.....
It allows me having panel's height shorter than browser view's height in any resolutions and having or not having scroll inside panel (depends on quantity of items)
The same approach is applied to filter title + text-overflow
Here is video - http://take.ms/WBDcy
and here is code - http://plnkr.co/edit/SbMa9Ece2eOPJ2C0Lt5U?p=preview
When I was writing this post I've understood that using of max-height: 80vh maybe was even better than media queries. It should be tested.