Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted 5 months ago

FAQ Accordion with vanilla css and Js

Bhavik Thakur•320
@bhavikthakur
A solution to the FAQ accordion challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

Solution retrospective


What are you most proud of, and what would you do differently next time?

None!

What challenges did you encounter, and how did you overcome them?

Since this was my first js challenge, I struggled a lot. I thought this is the easiest js challenge per FM filter, but I don't think so, at least in my case. I wasn't able to add a smooth transition. I used display:none at first but couldn't add a transition, so I had to use the height option with the help of Google (no lie), so I managed to do it on my own. Also, I've noticed my answers expanding, but with that, my questions would push upwards, which made it look weird for some reason, so I added some margin. I still don't know if my solution is good enough. I am planning to add keyboard navigation and better height handling soon but didn't wanna get stuck so I submitted the solution.

What specific areas of your project would you like help with?

Please help me, guys.

  1. I don't want my card to shrink or grow and would prefer it to be fixed size even if I collapse my first expanded answer, which is a default as per design.
  2. I need help with Accordion's most preferred approach in the sense that I don't know what the most preferred approach when it comes to accordion
  3. I couldn't fix my card exactly in the middle as per design so I did it ugly way by adding padding to the body
  4. I need a permanent fix or solution to avoid questions shifting upwards when we expand the answers. I was able to do it here using margin but I don't know if its right way to do it.
Code
Select a file

Please log in to post a comment

Log in with GitHub

Community feedback

  • P
    Andrey•4,640
    @dar-ju
    Posted 5 months ago

    Hi, Bhavik!

    You've got it mostly right!

    • To avoid adding padding for the body, you can make an additional container and give it padding on top. The card will rest against its padding and when expanded, it will only move down.

    • I see margin-bottom for paragraphs, even for those that are collapsed. This issue is easy to solve - just add margin-bottom in js and only if the card is expanded (answer.style.marginBottom = '1em'). Zero it when collapsing.

    • The smoothness of expanding/collapsing by height will be given to you by the height control of the paragraph - collapsed height 0, expanded height 100%. And add smoothness with standard transition. You can also use keyframes

    • Use semantics, all this faq is ul, the items in it are li. Leave alt empty for decorative (non-content) images.

    • Add gap for card__faq-header, on mobile the title text is too close to the + buttons

    Marked as helpful
  • Bhavik Thakur•320
    @bhavikthakur
    Posted 5 months ago

    Perfect! I will make the changes soon. Thanks for reviewing the code.

Join our Discord community

Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!

Join our Discord

Stay up to datewith new challenges, featured solutions, selected articles, and our latest news

Frontend Mentor

  • Unlock Pro
  • Contact us
  • FAQs
  • Become a partner

Explore

  • Learning paths
  • Challenges
  • Solutions
  • Articles

Community

  • Discord
  • Guidelines

For companies

  • Hire developers
  • Train developers
© Frontend Mentor 2019 - 2025
  • Terms
  • Cookie Policy
  • Privacy Policy
  • License

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

How does the accessibility report work?

When a solution is submitted, we use axe-core to run an automated audit of your code.

This picks out common accessibility issues like not using semantic HTML and not having proper heading hierarchies, among others.

This automated audit is fairly surface level, so we encourage to you review the project and code in more detail with accessibility best practices in mind.

How does the CSS report work?

When a solution is submitted, we use stylelint to run an automated check on the CSS code.

We've added some of our own linting rules based on recommended best practices. These rules are prefixed with frontend-mentor/ which you'll see at the top of each issue in the report.

The report will audit all CSS, SCSS and Less files in your repository.

How does the HTML validation report work?

When a solution is submitted, we use html-validate to run an automated check on the HTML code.

The report picks out common HTML issues such as not using headings within section elements and incorrect nesting of elements, among others.

Note that the report can pick up “invalid” attributes, which some frameworks automatically add to the HTML. These attributes are crucial for how the frameworks function, although they’re technically not valid HTML. As such, some projects can show up with many HTML validation errors, which are benign and are a necessary part of the framework.

How does the JavaScript validation report work?

When a solution is submitted, we use eslint to run an automated check on the JavaScript code.

The report picks out common JavaScript issues such as not using semicolons and using var instead of let or const, among others.

The report will audit all JS and JSX files in your repository. We currently do not support Typescript or other frontend frameworks.

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub