The expansion animation for the Nextjs/React accordion box did not work as expected when utilizing tailwindcss

I am currently working on creating an animation for a collapsible box (accordion).

My goal is to have the child component initially hidden with display:none. When I hover over the parent component, the child should be revealed and the dimensions of the parent should adjust accordingly.

Unfortunately, the animation is not functioning as expected.

Below is a simple reproduction:

export default function Home() {
  return (
    <main className="flex w-[50vw] p-10 items-center justify-center">
      <div className="border-black group border-2 transition-all min-w-5 min-h-5 w-auto h-auto bg-red-300">
        <div className="hidden w-20 group-hover:block bg-gray-200">
          placeholder
        </div>
      </div>
    </main>
  );
}

I found that the animation works if I manually set the size of the parent like so:

export default function Home() {
  return (
    <main className="flex w-[50vw] p-10 items-center justify-center">
      <div className="border-black group border-2 transition-all w-5 h-5 hover:w-10 hover:h-10 bg-red-300">
        <div className="hidden w-20 group-hover:block bg-gray-200">
          placeholder
        </div>
      </div>
    </main>
  );
}

However, I do not want to use hardcoded values for the parent's size as shown in the second version of my code.

Is there a way to make the first version of my code work properly?

Thank you in advance.

Answer №1

Transitioning an element from being hidden with display:none to visible with block can be a challenge, as the browser struggles with displaying part of a block element. This makes creating a transition difficult.

To tackle this issue, consider using a different method such as incorporating an external library like Framer-motion or gsap. Another option is to follow the max-height technique recommended in @wongjn's comment.

Check out this codepen example using Tailwind CSS and the max-height approach.

I hope this information proves helpful!

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

Is Kendo Telerik grid capable of applying conditional formatting while filtering?

Is it feasible to modify the background color of rows in a Kendo Telerik grid when a filter is implemented within an ASP.NET MVC application? ...

Component wrapped in wrapper not displaying

I have been attempting to conditionally render a component using a wrapper function. However, I am facing an issue where the component does not render when the condition is met. App.js import { BrowserRouter, Routes, Route } from "react-router-dom&quo ...

There seems to be a problem with the text-to-speech API: It looks like there's a ReferenceError stating that

Currently, I am developing a program using the Quasar framework which is based on Vue and can be compiled for mobile using Cordova. However, I am encountering some issues when trying to run it on mobile. Below is the function causing problems: activat ...

Adding some extra space inside a flex container using Bootstrap's padding

When attempting to use padding within a d-flex container to separate elements that are placed inline due to the d-flex class, I encountered an issue where the padding was not being applied and did not change anything. If I do not utilize the d-flex class, ...

What is the best way to merge an array of objects into a single object?

Is there a way to dynamically convert object1 into object2, considering that the keys like 'apple' and 'water' inside the objects are not static? const object1 = { apple:[ {a:''}, {b:'&apos ...

How can I style my tables to have different border spacing between the table head and table body?

When it comes to tables, I want to customize the column spacing in a specific way. I need space between columns in the table header but no gaps between columns in the table body. How can I achieve this by including border space only in the thead section? ...

My index.html not successfully linking to the CSS file

I'm new to this and still learning, so please bear with me. I'm having trouble linking my CSS file to my index.html document in order to create a black banner at the top of the page. I've tried opening it in both Chrome and Explorer but noth ...

What is the best method to make an email address distinct?

<body> <p>Please input your email address:</p> <input id="email" style="margin-bottom: 20px; margin-top: 2px;" type="email" placeholder="Email Address"> <input onclick= "validateEmail(email)" type="su ...

What could be causing my hover effect to function exclusively on Chromium-based browsers and not on Firefox?

Could use some help with this code snippet .podmenu { display: flex; flex-direction: column; list-style: none; width: 10rem; margin-left: 3rem; margin-bottom: 60px; } .verze { list-style: none; flex-direction: column; ...

How to Safely Merge Data from Several Excel Files into a Single Worksheet in Google Sheets using ExcelJS without Overwriting Existing Data

Just to clarify - I'm not a seasoned developer, I'm still a newbie at this. My current challenge involves transferring data from multiple Excel files located in one folder to a single worksheet in Google Sheets. To do this, I am utilizing excelJ ...

Issues with Firebase cloud functions: encountered errors related to CORS and an unhandled error of TypeError stating that "openai is not a constructor"

My current challenge involves making a secure https onCall to the OpenAi API using a Firebase cloud function. Initially, I encountered a CORS error which I managed to resolve by modifying the code snippet as shown below: exports.GPT = functions.https.onCa ...

Executing a C# method from within a .js file using a Javascript function in a .cs file

I've made some updates based on the responses provided. My main area of confusion lies in the 'data' parameter and how to handle it in the JavaScript call. C# method [HttpPost] public string GetPreviewURL(string activityID) ...

What specific features does CSS3 offer in terms of background-size properties?

Let's delve into my knowledge: $, like in font-size: 14$; em, as in margin: 2em; What other units are out there? ...

The initial dropdown menu in PHP coupled with Javascript fails to retain my chosen option

As a novice PHP programmer, I successfully created a double drop-down list from a MySQL database. The idea is to choose a claims hub in the first box and then select an attorney associated with that specific hub in the second box. However, I am facing an ...

Calculating the sum of table columns with the help of knockout.js

Is there a way to calculate the table columns using knockout.js? I am familiar with jQuery but new to knockout.js and unsure how to approach this. Instead of generating the table data using JSON, I would like to directly create it in the HTML table itself. ...

Using jQuery/Javascript to create a dynamic content slider

I am currently working on developing a straightforward content slider. When the page loads, my goal is to display only one item (including an image and some content) and provide a navigation system for users to move through the items. Below is the basic H ...

Image expansion

I have a container element div that holds various content of unknown length. How can I add a background image that extends the entire length of the container since background-images do not stretch? I attempted to use a separate div with an img tag inside. ...

Exploring the utilization of a constant value within a styled component

I am looking to pass a styled component to a variable and then utilize that variable in the rendering process However, I am unsure of how to go about doing this. The main reason for passing it through a variable is for conditional purposes. Sample.styled ...

Infinite scroll causing Firebase ".length" function malfunction

My NextJs website is encountering errors related to Firebase infinite scroll. The issue seems to be with the .length property being undefined for some unknown reason. I am struggling to debug the code and make it work properly in Next.js. Any help would be ...

Is e.preventDefault() failing to work in Next.js?

Hey everyone, I'm new to react.js and next.js. I attempted to copy some data from a form and display it in the console, but using e.preventDefault() is preventing me from submitting my form. Is there a more effective way to achieve what I'm aimin ...