Prevent row expansion when using Flexbox for expandable items

I am facing an issue with my flexbox layout where the items, which are expandable like dropdowns, expand along with the row when clicked. I want only the item to expand without affecting the rest of the row.

Current behavior: https://i.sstatic.net/60wBh.png

Expected behavior: https://i.sstatic.net/p1oRj.png

This is the code for the columns:

.list {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  margin: 80px auto 0;
  max-width: 1096px;

  @include mediaQuery(max, 1080px) {
    width: 940px;
  }

  @include mediaQuery(max, $tablet) {
    width: 526px;
  }

  @include mediaQuery(max, $mobile) {
    width: 335px;
  }

  @include mediaQuery(max, 320px) {
    width: 285px;
  }
}

I have searched for similar issues on Stack Overflow but haven't found a solution yet.

Any assistance would be greatly appreciated! Thank you!

Additional code related to the question:

<div className="analyzed-genes-list">
  {
   data.map((item, index) => (
    <GeneCard key={index} gene={item.gene} cancers={item.cancers} positives={this.state.posGenes} traits={this.state.traits} />
   ))
  }
 </div>

Code snippet from GeneCard.js:

<div className={this.state.toggleBody ? "gene-card-active" : "gene-card"}>
  <div className="gene-card-title" onClick={this.showBody} style={{backgroundImage: this.state.toggleBody ? `url(${ChevronUp})` : `url(${ChevronDown})`}}>
    <span className="gene-name">{this.props.gene}</span>
      {
       this.state.pos ?
        <span className="pos-variant">{this.state.variants} variant
         { this.state.variants > 1 ? 's' : null }
           &nbsp;detected</span>
           :
           <span className="variant">Variant not detected</span>
         }
        </div>
        {
          this.state.toggleBody ?
          <div className="gene-card-body">
            {
              this.props.cancers.map((cancer, index) => (
                <span key={cancer} className="cancer">
                  {cancer}
                  {
                    index === this.props.cancers.length - 1 ?
                    null
                    :
                    ','
                  }
                  &nbsp;
                </span>
              ))
            }
          </div>
          :
          null
        }
      </div>

Answer №1

Check out this CSS solution that utilizes the columns feature of CSS.

.wrapping-list {
  columns: 2;
}

.list-item {
  padding: 5px;
  margin-bottom: 10px;
  background: red;
  break-inside: avoid;
  -webkit-column-break-inside: avoid;
  page-break-inside: avoid;
}

.list-item-expansion {
  width: 100%;
  height: 50px;
  background: blue;
}
<div class="wrapping-list">
    <div class="list-item">
      example content
      <div class="list-item-expansion">
        expansion section
      </div>
    </div>
    <div class="list-item">test</div>
    <div class="list-item">test</div>
    <div class="list-item">test</div>
    <div class="list-item">test</div>
    <div class="list-item">test</div>
    <div class="list-item">test</div>
    <div class="list-item">test</div>
    <div class="list-item">test</div>
    <div class="list-item">test</div>
</div>

Answer №2

Here is a demonstration using CSS to achieve a specific layout.

By utilizing absolute positioning, the box maintains its position without affecting the other elements around it. Relative positioning ensures that the box is correctly positioned within its container.

function toggleExpansion() {
    const listItem = document.getElementById("listitem1");
    const expansion = document.getElementById("expansion1");
    expansion.classList.toggle("active");
    expansion.style.top = listItem.clientHeight + "px";
}
.container {
  margin-top: 10px;
  display: flex;
  width: 300px;
  height: 300px;
  flex-direction: column;
}

.listitem {
  flex: 1;
  margin-top: 10px;
  background-color: red;
}

.listitem-expanded {
  display: none;
  position: absolute;
  top: 0px;
  width: 100%;
  height: 50px;
  background-color: blue;
}

.listitem-expanded.active {
    display: block;
}
<div class="container">
  <div id="listitem1" class="listitem" onclick="toggleExpansion()">
    <div style="position: relative">
      <div id="expansion1" class="listitem-expanded">expansion</div>
    </div>
    test
  </div>
  <div class="listitem">test</div>
  <div class="listitem">test</div>
  <div class="listitem">test</div>
  <div class="listitem">test</div>
  <div class="listitem">test</div>
</div>

Answer №3

Here is one possible approach...not specifically a CSS solution...

The concept involves splitting the incoming data in half to render two separate arrays in React.

Data Splitting:

componentDidMount(){
  let first = data.slice(0, Math.round(data.length / 2));

  let second = data.slice(Math.round(data.length / 2), data.length);

   this.setState({
      firstHalfData: first,
      lastHalfData: second
    });
}

Rendering with JSX:

<div className="analyzed-genes-list">
   <div className="analyzed-genes-list-col">
      {
        this.state.firstHalfData.map((item, index) => (
         <GeneCard key={index} gene={item.gene} cancers={item.cancers} positives={this.state.posGenes} traits={this.state.traits} />
           ))
         }
       </div>
     <div className="analyzed-genes-list-col">
        {
          this.state.lastHalfData.map((item, index) => (
           <GeneCard key={index} gene={item.gene} cancers={item.cancers} positives={this.state.posGenes} traits={this.state.traits} />
            ))
         }
       </div>
     </div>

CSS Styling:

.analyzed-genes-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  margin: 80px auto 0;
  max-width: 1096px;
  width: 100%;

  @include mediaQuery(max, 1080px) {
    width: 940px;
  }

  @include mediaQuery(max, $tablet) {
    width: 526px;
  }

  @include mediaQuery(max, $mobile) {
    width: 335px;
  }

  @include mediaQuery(max, 320px) {
    width: 285px;
  }
}

.analyzed-genes-list-col {
  display: flex;
  flex-direction: column;
  max-width: 526px;

  @include mediaQuery(min, 1081px) {
    width: 508px;
  }

  @include mediaQuery(max, 1080px) {
    width: 440px;
  }

  @include mediaQuery(max, $tablet) {
    width: 100%;
  }
}

If anyone has a CSS-based alternative, I am open to suggestions!

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

How can you generate an HTML table row without using the <tr>

Greetings everyone! I am currently working on creating a table using HTML, Bootstrap 5, and CSS to display various article details. Each article may have one or multiple variants, and I would like these variants to be displayed as rows within the parent ar ...

"Is there a way to implement a sequence of fadeIn() followed by fadeOut() and then insertAfter

I have created a simple div fade cycle. Some of the divs are within an outer div, which is then placed in another outer div. I've written a script that cycles through these divs by fading them in and out. However, there seems to be a problem - the div ...

Is it possible to apply border-radius to selections, similar to how it is done in vscode

It was reminiscent of this... Visual Studio Code ::selection{ border-radius: 5px; } I attempted something similar to this, however it was not successful... ...

What are the steps to resolving an issue with importing a css file in next.js?

Error: ./node_modules/quill-emoji/dist/quill-emoji.css ModuleParseError: Module parse failed: Unexpected character '�' (1:0) You may need a suitable loader for handling this file type, as there are currently no configured loaders to process it. ...

What is the method for activating a hook after a state change in a functional component?

I'm currently working on a custom pagination hook that interacts with an API to fetch data similar to React Query. The concept is straightforward. I aim to invoke this hook whenever a specific state (referred to as cursor) undergoes a change. Below i ...

What challenges come with including non-page files in the pages directory of a Next.js project?

When it comes to Next.js, I've heard the suggestion to only include page files within the pages folder. However, what are the potential drawbacks of adding non-page files in this directory? For instance, if custom hooks and test files were placed ins ...

Incorporate information into a React component

I'm currently working on my initial react component and facing a challenge while adding items to the parent element through an external click event. The user needs to select from a list of search results, and I intend for these selections to be incorp ...

Locking mat-toolbar and mat-tabs to the top in Angular Material 2

As I work on my website, my goal is to fix the < Mat-Toolbar > at the top of the screen and then directly underneath that, lock the < Mat-Tabs >. The challenge I'm facing is that the position: fixed in CSS is not working as expected. When ...

I am interested in creating a table that can toggle between show/hide mode with a plus/minus feature

$(document).ready(function() { $("#t1").hide(); // hide table by default $("#close").hide(); //hide the minus button as well if you only want one button to display at a time $('#sp1').on('click', function() { //when p ...

When hovering over the image, it enlarges and gradually becomes transparent, unveiling the hidden text beneath

I've been struggling to achieve the desired effect for quite some time now, putting in hours of effort without success. Essentially, I am aiming to create a div that showcases an image. When the mouse hovers over the image, it should zoom in and fade ...

Unable to get AppBar component from material-ui package to function properly in my React JS project

As I embark on creating my inaugural React Application (WebApp), I am encountering a particular issue. In order to implement a Navigation Bar, I have opted to utilize the AppBar component available in the material-ui library. Following the example of the S ...

I require assistance on how to properly arrange images in a div so that they stack

I'm having an issue with arranging a series of images within a div where they stack on top of each other. The div has a CSS width of 450px, and the top image always matches this width. Subsequent images vary in size and need to be aligned flush right ...

Issue with displaying Font Awesome icons in Bootstrap 4 navbar brand

Recently, I've been setting up a local Laravel website to explore the latest features and changes in Laravel 5.6. However, I've encountered an issue while trying to integrate a Font Awesome icon into the navbar-brand of Bootstrap 4. The icon does ...

Error: The page you are trying to access does not have a valid default export. The provided type is not acceptable

Hello, I am a newcomer to the world of react and NextJS. Currently, I am working on a small project using NextJS 13 where I am attempting to display products stored in a JSON file (which will later be moved to a database). The application runs correctly, ...

Previewing Printed Forms in JSF

I am working with a JSF form on a view *.jspx page that contains approximately 20 fields. The form includes a print preview button that opens a new browser window and displays the form in read-only mode. To achieve this, I have utilized a workaround invol ...

Troubleshooting Problems with CSS Media Queries on Various 1280 screens

I'm facing an issue with creating a responsive layout using CSS media queries to adapt to different screen resolutions. Even though I've set up Media Queries for specific resolutions like: /*1280 x 1024*/ /*1280 x 960*/ /*1280 x 800*/ /*1280 x 7 ...

Discover the magic of Multer for effortless file uploading and storage. Learn how to seamlessly bring these files back to the front-end

I am currently working on a React/Node application where I am implementing the functionality to upload video files on the frontend. These video files are then sent to the backend using Multer for storage. Now, I am trying to figure out the best and corre ...

Guide to Presenting HTML data stored in an SQL database on an ASP.NET webform

I need help figuring out how to dynamically display a blog page from my database. The content I'm pulling contains HTML tags, so right now it's showing the raw data with tags included. What I really want is for it to display like a proper page. ...

What is the best way to compare two CSS files and selectively replicate just the discrepancies?

Is there a way to compare and copy different information from CSS files into another file? For instance, if I have two CSS files named a.css and b.css, is there a method to automatically select and copy only the differences between the two files? Thank y ...

Issue with Html CSS search bar: the icon is unresponsive when clicked

I've encountered an issue with a website template that includes a search form with an icon in the text field. https://i.sstatic.net/F0gLs.png When users enter a value and press 'Enter', the form functions as expected. However, clicking on ...