What is the best way to adjust the height of my accordion to ensure the text is displayed correctly?

After finding an accordion online, I decided to restyle it and add a slide effect. However, I encountered a problem with setting the height to 100px. When trying to change it to auto, the slide effect that I wanted was removed. How can I set the height so that it fits the text while still maintaining the slide animation?

Here is my current implementation.

body {
  background:#d0d0d5;
}
/* Basic structure */
#accordion { 
  margin: 100px auto;
  width: 400px;
}
#accordion ul {
  list-style: none;
  margin: 0;
  padding: 0;
}
.accordion {
  -webkit-transition: all 150ms ease-in-out;
  transition: all 150ms ease-in-out; 
  height: 0px;
  overflow: none; 
  line-height: 1.3em; 
  overflow: hidden;
}
.accordion:target {
  -webkit-transition: all 150ms ease-in-out; 
  transition: all 150ms ease-in-out; 
  height: 100px;
}
#accordion ul li a {
  text-decoration:none;
  display:block;
  padding:10px;
}
.accordion{
  padding:0px;
}
/* Colors */
#accordion ul{
  -webkit-box-shadow:0 4px 10px #3f3f3f;
  -moz-box-shadow:0 4px 10px #3f3f3f;
  box-shadow:0 4px 10px #3f3f3f;
  -webkit-border-radius:5px;
  -moz-border-radius:5px;
  border-radius:5px;
}
#accordion ul li a {
  background: #2B2D30;
  border-bottom:1px solid orange;
  color: white;
}
.accordion {
  -webkit-transition: all 800ms ease-in-out; 
  transition: all 800ms ease-in-out;
  height: 0px;
  background:#fdfdfd;
  color:#999;
}
.accordion:target {
  border-top:3px solid #ffbf87;
}
<div id="accordion">
  <ul>
    <li>
      <a href="#one" >Q. test</a>
      <div id="one" class="accordion">
        test text
      </div>
    </li>
    <li>
      <a href="#two">Q. another tester</a>
      <div id="two" class="accordion">
        here is another text
      </div>
    </li>    
    <li>
      <a href="#three">Q. I keep getting errors what do I do?</a>
      <div id="three" class="accordion">
        Take a look at <b>this page</b> to find the most common errors and a how to resolve them.
        Take a look at <b>this page</b> to find the most common errors and a how to resolve them.
        Take a look at <b>this page</b> to find the most common errors and a how to resolve them.
        Take a look at <b>this page</b> to find the most common errors and a how to resolve them.
        Take a look at <b>this page</b> to find the most common errors and a how to resolve them.
        Take a look at <b>this page</b> to find the most common errors and a how to resolve them.
      </div>
    </li>
  </ul>
</div>

Answer №1

It is recommended to utilize max-height and min-height instead of the height property.

Below is the updated code:

body{
  background:#d0d0d5;
}

/*  Basic stucture
=====================*/
#accordion{
  margin:100px auto;
  width:400px;
}
#accordion ul {
  list-style:none;
  margin:0;
  padding:0;
}
.accordion {
  -webkit-transition: all 150ms ease-in-out;
  transition: all 150ms ease-in-out;
  max-height:0px;
  overflow:none; 
  line-height:1.3em; 
  overflow:hidden;
}
.accordion:target {
  -webkit-transition: all 150ms ease-in-out; 
  transition: all 150ms ease-in-out; 
  max-height: 300px /* or more 1000 */ ; 
  min-height: 100px
}
#accordion ul li a {
  text-decoration:none;
  display:block;
  padding:10px;
}
.accordion { 
  padding:0px;
}

/*  Colors 
====================*/
#accordion ul {
  /*box-shadow*/
  -webkit-box-shadow:0 4px 10px #3f3f3f;
  -moz-box-shadow:0 4px 10px #3f3f3f;
  box-shadow:0 4px 10px #3f3f3f;
  /*border-radius*/
  -webkit-border-radius:5px;
  -moz-border-radius:5px;
  border-radius:5px;
}
#accordion ul li a {
  background: #2B2D30;
  border-bottom:1px solid orange;
  color: white;
}
.accordion {
  -webkit-transition: all 800ms ease-in-out; 
  transition: all 800ms ease-in-out;
  max-height: 0px;
  min-height: 0;
  background:#fdfdfd;
  color:#999;
}
.accordion:target {
  border-top:3px solid #ffbf87;
}
<div id="accordion">
    <ul>
        <li>
            <a href="#one">Q. test</a>
            <div id="one" class="accordion">
                test text
            </div>
        </li>
        <li>
            <a href="#two">Q. another tester</a>
            <div id="two" class="accordion">
                here is another text
            </div>
        </li>
        <li>
            <a href="#three">Q. I keep getting errors what do I do?</a>
            <div id="three" class="accordion">
                Take a look at <b>this page</b> to find the most common errors and how to resolve them.
            </div>
        </li>
    </ul>
</div>

Visit this link for a live demo

Answer №2

Hey there! I've managed to get the animation you were looking for working perfectly. Here's what I did:

.accordion { -webkit-transition: all 150ms ease-in-out; transition: all 150ms ease-in-out; min-height:0px; overflow:none; line-height:1.3em; overflow:hidden; }
.accordion:target { -webkit-transition: all 150ms ease-in-out; transition: all 150ms ease-in-out; min-height:100px; height:auto; }

https://jsfiddle.net/r6f6jbvy/2/

Answer №3

Feel free to give this a try

body{
    background:#d0d0d5;
  }
/*  Custom layout
=====================*/
#accordion{margin:100px auto;width:400px;}
#accordion ul{list-style:none;margin:0;padding:0;}
.accordion{-webkit-transition: all 150ms ease-in-out; transition: all 150ms ease-in-out; max-height:0px;overflow:none; line-height:1.3em; overflow:hidden;}
.accordion:target{-webkit-transition: all 150ms ease-in-out; transition: all 150ms ease-in-out; max-height: 300px /* or more 1000 */ ; min-height: 100px}
#accordion ul li a{text-decoration:none;display:block;padding:10px;}
.accordion{padding:0px;}
/*  Color Scheme 
====================*/
#accordion ul{
  /*box-shadow*/
  -webkit-box-shadow:0 4px 10px #3f3f3f;
  -moz-box-shadow:0 4px 10px #3f3f3f;
  box-shadow:0 4px 10px #3f3f3f;
  /*border-radius*/
  -webkit-border-radius:5px;
  -moz-border-radius:5px;
  border-radius:5px;
}
#accordion ul li a{
  background: #2B2D30;
  border-bottom:1px solid orange;
  color: white;
}
.accordion{
  -webkit-transition: all 800ms ease-in-out; 
  transition: all 800ms ease-in-out;
  max-height: 0px;
  min-height: 0;
  background:#fdfdfd;
  color:#999;
}
.accordion:target{
  border-top:3px solid #ffbf87;
}
<div id="accordion">
  <ul>
    <li>
      <a href="#one" >Q. test</a>
      <div id="one" class="accordion">
        test text
      </div>
    </li>
    <li>
      <a href="#two">Q. another tester</a>
      <div id="two" class="accordion">
        here is another text
      </div>
    </li>    
    <li>
      <a href="#three">Q. How can I fix common errors?</a>
      <div id="three" class="accordion">
        Check out <b>this page</b> for solutions to frequently encountered errors and their resolutions.
        Check out <b>this page</b> for solutions to frequently encountered errors and their resolutions.
        Check out <b>this page</b> for solutions to frequently encountered errors and their resolutions.
        Check out <b>this page</b> for solutions to frequently encountered errors and their resolutions.
        Check out <b>this page</b> for solutions to frequently encountered errors and their resolutions.
        Check out <b>this page</b> for solutions to frequently encountered errors and their resolutions.
      </div>
    </li>
  </ul>
</div>

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

Tips for executing a function only once within the animation loop in Three.js

Is there a way to call a function only once when a certain condition is met in Three.js? I am currently sampling the frames per second (FPS) to send it to an external service. The FPS is sampled and averaged over time, and the average value is sent after 1 ...

having trouble retrieving 200 status code from Angular server response

Objective: I need to make certain changes to the record locally if the server responds with a 200 code. Problem: I am unable to retrieve the response code or access the 'message' attribute. This is the server response I receive from the HTTP ca ...

Implementing a function to load HTML pages upon clicking a button with JavaScript

I need help creating a button that loads an HTML page using JavaScript, without redirecting to the page. However, the current code I have is not loading the HTML page as desired. Here is the code snippet in question: <!DOCTYPE html> <html> &l ...

Thymeleaf: Expression parsing error

I am working on a Thymeleaf template that includes pagination functionality. <ul class="results_perpage" > <li th:if="${previous != null}"><a th:href="javascript:movePage(`${previous}`);" class="results_menu" th:text="PREVIOUS">< ...

Creating custom methods or functions within imported models in Sequelize: A guide

As a newcomer to the world of Node.js, I am embarking on my initial project involving node development. While my background primarily revolves around utilizing Laravel for various projects, I am well-versed in the concepts of MVC (Model View Controller). I ...

Improving Search Functionality with jQuery, Ajax, and Multiple Features

I am currently working on implementing a search feature using multiple jQuery functions. Below is the code and functions I have created: $(document).ready(function() { var timer = null; $('input#search').keyup(function(e) { s ...

Angular - How child components can communicate with their parent components

I'm struggling to understand how to communicate between components and services. :( Even though I've read and tried a lot, some examples work but I still don't grasp why (?). My goal is to have one parent and two child components: dashboa ...

Creating a standalone module using webpack that can be accessed through a script element

When setting the library target to 'umd' in the package.json file, the expectation was that the outputs would be usable via a <script> tag and if no module system was available it would be attached to the window. However, this does not seem ...

What is the best way to position the <h:panelGrid> itself at a location other than the center?

Whenever I utilize this code snippet <h:panelGrid id="myGrid" columns="1" rendered="#{myBean.showResults}" width="50%" border="1" style="text-align: center;"> The panelGrid items will be centered HOWEVER, I desire to center the h:panelGrid it ...

I am encountering a problem specifically with Chrome when it comes to the following block of JS code. This code works perfectly in Firefox, but in Chrome it displays the original size of the

Here is a snippet of JavaScript code that seems to be functioning well in Firefox but encountering issues in Chrome: let mainImg = document.getElementById('big_image'); let smallImgs = document.getElementById('small_image').getElements ...

Have you ever come across odd ways to use Array.of() and Array.from()?

Discover two innovative methods introduced in ES6 for Array: Array.of() and Array.from(). Their distinct uses are outlined on this informative page. However, I find myself perplexed by the application of Array.of in this specific line. // usage of Array. ...

Develop diverse canvases with shapes such as ovals, rectangles, and circles

My task involves creating a canvas in an oval shape. Below is the code I am using in jQuery with the created object of canvas. $("#decalshap").change(function() { alert("decal"); var shap = $(this).val(); if(shap=="oval") ...

Every time Tailwind compiles, it seems to generate a glitchy class

There was a time when I accidentally wrote 2 classes without a space max-w-[412px]mb-[36px]. This caused an error, so I added a space between the classes. However, the error persisted even after making this correction. In a last attempt to resolve the issu ...

Utilizing Titanium MVC: Invoke function and pause for response

Currently, I am in the process of developing my very first Titanium iPhone application. Within a model module, I have this code snippet: (function() { main.model = {}; main.model.getAlbums = function(_args) { var loader = Titanium.Ne ...

How can I dynamically bind the count value in an ng-repeat loop?

I'm working with HTML code that involves looping through layers, but the values in the columns change with each loop iteration. I have concatenated the index value to the table column, but it's not working as expected. Any suggestions? <div c ...

Encountering an issue with expressjs when running as transferred with MIME type text/html

When running expressjs as a local server, I encountered the following errors. Can anyone identify what is causing this issue? Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost:3000/js/lib/bootstrap/dist/css/boo ...

The mix-blend-mode feature is not compatible with Chrome browser

I'm having an issue with the mix-blend-mode property in Chrome, as it's not functioning correctly. However, it works perfectly fine on Firefox and Safari. My goal is to achieve a cutout text effect. Thank you for any assistance! h1 { mix-blend ...

Retrieve an Excel file using Selenium through a URL, but only obtain JavaScript code instead

I am attempting to download an Excel file using its URL, but all I receive is JavaScript code. I'm unsure of how to retrieve the actual file instead of just the JS code. Here is my current code: # -*- coding: utf-8 -*- from selenium import webdrive ...

Creating a series of images in JavaScript using a for loop

Currently attempting to create an array of images, but with a large number of images I am looking into using a "for loop" for generation. Here is my current code snippet : var images = [ "/images/image0000.png", "/images/image0005.png", "/ima ...

issue with replicated field

My code is working perfectly fine, as you can see here. Now, I am attempting to replace <input id="input1" /> with <div id="input1"> </div>. You can view my progress here. The issue lies in the IDs - they are meant to change from input1 ...