Splitting a list of accordions into two columns with CSS and React - a step-by-step guide!

I am looking for a way to showcase a list of Accordions in a two-column layout. Each Accordion consists of both a Summary and Details section, with the Details being visible only when the Summary is clicked. Unfortunately, I won't delve into the implementation logic here in order to keep things concise.

Below is the React code snippet:


<div class="container">
  {accordions.map(acc => (
    <div class="accordion">
      <AccordionSummary text={acc.summary} />
      <AccordionDetails text={acc.details} />
    </div>
  ))}

CSS styling:

.container {
  display: grid;
  grid-template-columns: repeat(2,1fr);
}
 

The issue with this implementation is that clicking on an accordion in one column causes the other column to expand as well, since they are located within the same row.

An optimal solution would be to have two separate columns, each with a grid display to prevent this behavior. However, distributing accordions evenly across these two columns poses its own challenge.

If anyone has suggestions on how to modify the existing React code to avoid expanding another column, please share your insights!

Answer №1

The issue arises from utilizing the same map function to loop through the acc object, causing both the details and summary of one acc object to be treated as a single entity for the on click method. Due to this limitation, achieving your desired outcome with the current react code may not be feasible.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Building a GWT webpage from scratch: A step-by-step guide

My project involves creating a website that features various Java tasks and informational content. Users can navigate through different pages by clicking on links from the homepage. Specifically, on the Java page, users will be able to complete tasks. Th ...

Guidelines for accessing a particular profile post-login

Hi there! I'm currently working on developing a straightforward Login and Profile viewing application. The aim is for users to be able to register, login, and then access their profile where the information provided during registration will be display ...

Ensure advertisements are easily accessible and not visible to screen-reading tools

I oversee the management of numerous ads on a website. My goal is to enhance their accessibility, and though I have been doing research, there seems to be limited information available on how to achieve this effectively. While I continue my investigation, ...

ReactJS: A single digit input may result in the display of numerous '0's

My goal is to have a small box that only allows one digit, but it seems to work fine until I try to input multiple '0's. Then the box displays multiple 0000 persistently. https://i.sstatic.net/Ouot4.png https://i.sstatic.net/MMKjm.png H ...

What is the reason for the immediate application of the set function in a useState hook within asynchronous functions?

While working with multiple set functions of a useState hook, I noticed different behaviors when calling them in sync and async functions. function Test() { console.log('app rendering starts.'); const [a, setA] = useState(1); const [b ...

Collapse the hamburger menu upon clicking on a link in the mobile menu

Whenever I click on a menu item, the page loads but the menu remains open. This JSX code represents the Mobile Menu: const MobileMenu = () => { const navigation = [ { link: '/applications', text: 'Applications' }, { link ...

Switching the color of links when they are focused, without changing it when they are clicked by either a mouse or

Is there a way to change the color of a link only when a user navigates to it using the TAB key and not when they click on it? The simple CSS solution I found was: a:focus { color: red; } However, this also changes the color when the link is activated by ...

Outputting HTML from a function

As a beginner, I have a question about best practices. Is it more effective to return HTML from a function like the following: function displayHtml($userName) { $htmlMsg = " <html> <head> <title>Displaying HTML</title&g ...

Taking out the modal element from the document object model, causing the animation

I am currently working on a project using Next.js, Typescript, and Tailwind CSS. Within this project, I have implemented a modal component with some animations. However, I encountered an issue where the modal does not get removed from the DOM when closed, ...

Struggling to pass a function argument as a string for use within the function

Although the title may seem unclear, I will clarify. I have a collection of images on the left side and I have implemented an onclick function on each one to display the image and some information on the right side. It's like having thumbnails on the ...

The state in Redux-toolkit is failing to update

While working on a login functionality, I encountered an issue. After setting the state of the user in the .then function, I redirected the page to the next one through code. However, I found that I couldn't access the updated state using the useSelec ...

Employing jQuery to add an element as a sibling rather than a child node

I'm having trouble finding the specific functionality I need. My goal is to add sibling DOM elements to a disconnected node. From what I gather, it should be possible with either .after() or .add(), but for some reason both methods are not working as ...

Transitioning from feelings to sewing: incorporate a duo of properties within variations and breakpoints

I was thinking about how to convert this styled-emotion code into stitches. Essentially, I need to handle the onlyShowOnDesktop and breakpoint props. The definition of breakpoint is as follows: const breakpoint = isTopNavigationBreakPoint ? theme.defaultB ...

What is the process for allowing visitors to upload videos directly from the website to my Vimeo account?

I am currently working with Next.js version 13 on Vercel hosting platform, and I am looking for a way to allow users to upload videos directly from the frontend to my Vimeo account. While I know how to upload videos to their accounts, I am facing an issue ...

Do you think there is a more efficient way to solve this issue?

const [active, setActive] = React.useState(["active", "", "", "", ""]);``your unique text`` const hrefs = React.useMemo( () => ["/", "/about", "/skills", "/projects", "/contact"], [] ); React.useEffect(() => { setInterval(() => { ...

Echoing PHP code is being output as HTML comments

I recently encountered a strange issue while working with some PHP code in a file: <?php include_once('includes/connection.php'); include_once('includes/article.php'); $article = new Article; $articles = $artic ...

Guide to centering a list on a webpage using Bootstrap 3

Looking to create a centered list using the latest version of Bootstrap 3. Any tips? The desired list should be positioned in the middle of the page. ___________________________________________ | | | ...

Error: The `window` object is not defined within Next.js

There seems to be an error with the react-flip-page-library that I am using. Despite not utilizing node, I am encountering the following ReferenceError: window is not defined at Object.<anonymous> (D:\ZZZ\my_app\node_modules\react ...

Permit the use of the "&" character in mailto href links

When including an email mailto href link and using a & character in the subject, it can cause issues with code rendering. For example, if the subject is "Oil & Gas," only "Oil" may show up. In most cases, you could simply replace the & with th ...

Having trouble with input event listeners in JavaScript?

I've been attempting to trigger the keyup event using EventListener for an input tag, but it doesn't seem to be working and I'm unsure why. Here is the code I have: document.getElementById("ajax").addEventListener("keyup", function() { ...