This is a solution to the FAQ accordion card challenge on Frontend Mentor. Frontend Mentor challenges help you improve your coding skills by building realistic projects.
Users should be able to:
- View the optimal layout for the component depending on their device's screen size
- See hover states for all interactive elements on the page
- Hide/Show the answer to a question when the question is clicked
- Live Site URL: https://cholis04.github.io/faq-accordion-card-main/
- Mobile-first workflow
- Flexbox
- CSS Grid
- HTML Radio Button
I decided not to use javascript in this challenge. So I need to use :checked state on css. Yes, checkboxes allow for that, but to create an accordion element, only one element is open the others will be closed.
This is why I use radio buttons instead of checkboxes. because the radio button can be used to select only one option.
However, other problems persist. On the checkbox we can uncheck / deselect the options, this allowing the accordion to be open or closed.But on radio buttons. once we select , we can not deselect alias Uncheck. This causes the accordion element to continue to open.
Yes, maybe you all have an even better way to make writing code much simpler. Feel free to let me know that.
Creating accordion elements using radio buttons
<div class="question-box">
<input
type="radio"
id="faq-1"
name="question"
value="Open Faq 1"
class="radio-button"
/>
<label for="faq-1">
<span>How many team members can I invite?</span>
<img
src="./images/icon-arrow-down.svg"
alt=""
title="Show"
class="icon-arrow"
/>
</label>
<p>
You can invite up to 2 additional users on the Free plan. There is no limit
on team members for the Premium plan.
</p>
</div>
.box-faq > .faq > .question-box > p {
height: 0;
overflow: hidden;
transition: all 0.2s ease-in-out;
}
.box-faq > .faq > .question-box > input[type='radio'].radio-button:checked ~ p {
height: auto;
margin-bottom: 16px;
}
Add position on background image in css,
.box-faq {
background-position: -580px -296px;
/* background-position: center top; */
}
So far, the radio button can't be unchecked. However it can be done if using checkbox , but all accordion elements don't close automatically. It is very complicated. It may be necessary to have Javascript to manipulate accordion elements.
- Website - https://cholis04.github.io
- Frontend Mentor - @cholis04
- Dribbble - cholis04
- Instagram - @cholis04
- Codepen - cholis04