So I am writing a small program for a website where I want the blocks to grow to almost the size of the window when clicked on so they can reveal text. However, when I use checkbox, for some reason all the boxes grow. And when I use Radio, all the boxes grow and I cant click to make it disappear.
.games {
display: flex;
flex-wrap: wrap;
align-items: center;
/* background-color: rgba(0, 0, 47,0.3); */
justify-content: space-evenly;
}
.games:has(input:checked) .game-list {
width: 50vw;
height: 100vh;
margin: 50px;
}
.game-list {
margin: 30px;
;
padding: 20px;
height: 275px;
width: 325px;
display: flex;
flex-direction: column;
align-items: center;
border: 1px solid gray;
background: rgba(255, 75, 255, 0.1);
box-shadow: 0 8px 40px 0 rgba(45, 57, 231, 0.37);
backdrop-filter: blur( 4px);
-webkit-backdrop-filter: blur( 4px);
border-radius: 10px;
border: 1px solid rgba( 255, 255, 255, 0.18);
}
.game-list:hover {
transform: scale(1.05);
}
<div class="games">
<label for="Chapter 1">
<div class="game-list">
<input type="checkbox" hidden id="Chapter 1">
<img src="https://rideawavedesign.com/wp-content/uploads/2021/06/dnd-dice-level-up-birthday-card-1.jpg" style="width:140px; height: 140px;"/ >
<h1 style=" color:#FFFFFF">Chapter 1: </h1>
<p style=" color:#FFFFFF; font-size: 20px">Step-by-Step</p>
</div>
</label>
<label for="Chapter 2">
<div class="game-list">
<input type="checkbox" hidden id="Chapter 2">
<img src="https://miro.medium.com/v2/0*ggxUIJLB6dBwLHKo.jpg" style="width:140px; height: 140px;"/>
<h1 style=" color:#FFFFFF">Chapter 2: </h1>
<p style=" color:#FFFFFF; font-size: 20px">Races</p>
</div>
</label>
<label for="Chapter 3">
<div class="game-list">
<input type="checkbox" hidden id="Chapter 3">
<img src="https://miro.medium.com/v2/resize:fit:1080/1*Z_eV_OUsPV359E-ZUb-dbw.jpeg" style="width:140px; height: 140px;"/>
<h1 style=" color:#FFFFFF">Chapter 3: </h3>
<p style=" color:#FFFFFF; font-size: 20px">Classes</p>
</div>
</label>
<label for="Chapter 4">
<div class="game-list">
<input type="checkbox" hidden id="Chapter 4">
<img src="https://www.greenmangaming.com/blog/wp-content/uploads/2020/01/DnD-Alignments.png" style="width:140px; height: 140px;"/>
<h1 style=" color:#FFFFFF">Chapter 4: </h1>
<p style=" color:#FFFFFF; font-size: 20px">Personality and Background</p>
</div>
</label>
<label for="Chapter 5">
<div class="game-list">
<input type="checkbox" hidden id="Chapter 5">
<img src="https://i.pinimg.com/736x/da/bb/c5/dabbc5d29cb413808c9bd61fbbef822d.jpg" style="width:140px; height: 140px;"/>
<h1 style=" color:#FFFFFF">Chapter 5: </h1>
<p style=" color:#FFFFFF; font-size: 20px">Equipment</p>
</div>
</label>
<label for="Chapter 6">
<div class="game-list">
<input type="checkbox" hidden id="Chapter 6">
<img src="https://i.postimg.cc/TP6zYYcx/Multiclass.webp" style="width:140px; height: 140px;"/>
<h1 style=" color:#FFFFFF">Chapter 6: </h1>
<p style=" color:#FFFFFF; font-size: 20px">Customization options</p>
</div>
</label>
I’ve tried having different hidden ID and label for=. But no matter what it seems that all of the boxes will grow.
2
Answers
Actualy you still have a solution to make the clicked image grow and the previously clicked one unchecked(back to normal size) is to use radio input and adding the same name attibute to them (here name=chapter). Like this you can only have one input selected. Note that I also used as previously advised game-list:has(input:checked){…}
Still ahrd for me to guess what youa ctually trying to achieve. But according to your code maybe soemthing like this.
This is a cleaner version with a valid markup. To know if a markup is valid you can always use the W3C Markup Validator.
There are a few things to go through as such the answer will be long.
<p>
) to a sub-headline (h2
) as the name of a chapter is not a pargraph (block fo flow text) but a headline.aria-expanded="false"
which indicates a collapsed state,tabindex="0"
to make it focusable by using the tab key, and `role="button" to inform assistive technology that you can click on that element.:focus
pseudo-selector to[aria-expanded="true"]
to select the cards that are currently expanded.position: fixed; inset: 0;
which will the element consuem the entire screen with the intended padding/margin. To allow lonegr etxt that would not fit the screen I added an overflwo rule:overflow-y: auto
.Now to the JavaScript part. I will not explain everythin ind etail or go 100% technical correct to consfuse you less. You can look it up once you got more familiar with CSS and HTML and actually starting with JS. AS of now I think that you still far away from that. PS: never believe anybody that tells you that you learned everything about HTML and CSS that you can learn about. especially if you go indepth on modern topics such as responsive web design, accesibility, user experience, SEO ratings, performance, and such you will realize how little you know (counts for most develoeprs that are not long time seniors).
Here you have an
addEventListener
that waits for a specific event to execute an anomynous function (a function without a name that only runs within this event listener and therefor can not be reference outside of it).DOMContentLoaded
is the event we are waiting for. We wait until the browser loaded the entire DOM tree. You need to do that as the HTML document is read from top to bottom. Within that function we refrence elements that the browser would not know at the time that the script runs which would cause aReferenceError: Element is null
error. Waiting until the browser knwo all elements will prevent this error.This declares a constant with the Name
Wrapper
.documentQuerySelectorAll
will return a Node List which basically is just a list of all elements that you referenced with the "CSS selector". In thsi case it will return a list with 6 elements.forEach
is one of the loopign emthods. As we have a List we need to loop through the list to select every single element in that list and assign them for this loop through a temporary variable so we can "do stuff" with every element. In this case we use aneventListener
on every element to wait for aclick
event (somebody clicks on that element) to execute another anomynous function.With
let
I declare a variable.this.closest('element')
I select the cloest element that fits the "css rule". I do that so that I always get the "wrapper" even if I click on another element within the wrapper and not the wrapper itself.element.ariaExpanded == 'true'
is a little bit tricky as JS is not as logic soemtimes as you think. While thearia-expanded
attribute contains aboolean
(true/false), JS will not return aboolean
but astring
! Therefor I cannot just check for true/false but need to check for a string. and then get a boolean of true/false from it as my condition is either true or false.With
element.ariaExpanded = !boolean
will toggle the boolean. The attention mark negates the boolean:If you would use SASS, an "advanced CSS" (Preprocessor) youc an also nest your CSS code and make it shorter: