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

FAQ accordion - HTML, CSS, JavaScript

accessibility
P
Aydan•680
@AydanKara
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?

✅ Proud of:

  • Accessibility and Semantic Markup: I made sure to use semantic HTML elements (like <section>, <article>, <h2>, and <button>) along with ARIA attributes to create an inclusive experience.
  • Interactive and Smooth Transitions: Implementing the CSS transition for expanding and collapsing the FAQ sections really enhanced the user experience.
  • Dynamic Icon Updates: Updating the icon (switching from plus to minus) with descriptive alt text provided clear visual and accessible feedback on the accordion's state.

🔄 What would I do differently next time?:

  • Dynamic Height Calculation: Instead of using a fixed maximum height for the accordion content transition, I would implement a dynamic calculation using JavaScript (e.g., setting max-height based on scrollHeight). This would make the solution more robust for varying content lengths.
What challenges did you encounter, and how did you overcome them?

🏗️ Challenges:

  • Balancing Accessibility with Custom Interactions: Ensuring that the interactive elements (especially custom accordion buttons) remained accessible with clear ARIA attributes and keyboard support was a key challenge.

  • Smooth Transitions for Collapsible Content: Animating the height of an element whose content is dynamic (using height: auto) is inherently tricky, as CSS transitions don't work directly on auto values.

🎉 How I Overcame Them:

  • ✅ Accessibility: I leveraged native elements (like <button>) to handle keyboard interactions and added ARIA properties to communicate the current state to assistive technologies. This minimized the need for custom keyboard event handling.
  • ✅ Transition Issue: I used a CSS trick with max-height and a predefined large value to simulate smooth transitions. Although this approach works, it highlighted the need for dynamic calculations, which is something to explore further in future iterations.
What specific areas of your project would you like help with?
  • Dynamic Height Calculation for Transitions: I would appreciate suggestions on how to implement a dynamic solution that calculates the actual height of the accordion content (using properties like scrollHeight) to create smoother and more adaptive transitions.

  • Code Refactoring: Feedback on structuring the JavaScript code in a more modular or scalable way would be beneficial. For instance, should I encapsulate the accordion behavior in a class or use a particular design pattern?

  • Cross-Browser Compatibility: Any insights or testing recommendations on ensuring that the CSS transitions and ARIA attributes work seamlessly across different browsers and devices would be valuable.

Code
Select a file

Please log in to post a comment

Log in with GitHub

Community feedback

  • Josh Javier•930
    @joshjavier
    Posted 2 months ago

    Hi Aydan!

    Good job on using semantic HTML elements and making accessibility a priority in your solution. 🤓👍

    Here are some ideas you could apply in this solution or in future ones:

    • Setting a max-height cuts the content when viewed on mobile. In my case, I can only see four lines of text inside each accordion. You can fix this by implementing dynamic height transitions using CSS Grid. Check out Kevin Powell's video on the topic - it will blow your mind 🤯

    • In addition to toggling aria-expanded when the accordion button is pressed, you may also add aria-hidden to the .accordion-content container so that a closed accordion's content is not only hidden visually, but also to screen readers, thus providing a consistent user experience to everyone. You can read more about this topic here.

    • For your icons, I suggest setting aria-hidden="true" and alt="". Why? Since the accordion button already contains text (the questions) then that effectively becomes the label of the button. Screen readers will also see the aria-expanded attribute and let users know that they can expand or collapse the button, so no need to indicate this manually in the icon. For example, when I tested using VoiceOver in my iPhone, the second item reads:

    Is Frontend Mentor free? Expand section. Button. Heading level 2 article. Landmark. Collapsed. Double-tap to expand.

    Setting the icon to aria-hidden="true" and alt="" will remove the "Expand section" phrase, leading to a more concise description.

    You can also look into inlining SVGs for your icons if you want to add simple transitions like animating a plus into a minus and vice versa. Save this article on accessible icon buttons and this video on inline SVGs for future reference!

    Hope that helps. Happy coding!

    Marked as helpful

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
Frontend Mentor logo

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 1st-party linked stylesheets, and styles within <style> tags.

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.

Oops! 😬

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

Log in with GitHub