What is preventing my accordion from closing completely?

I'm currently working on creating FAQ questions in an accordion format. However, I've encountered an issue where adding padding around the answer section prevents the accordion from closing completely. Removing the padding solves the problem, but I prefer to have the padding for aesthetic reasons.

Prior to this, everything was functioning perfectly without any issues when the padding was not applied. Once the padding is added back, the collapsed answer still displays a few lines of text, which is not desired.

(The problematic accordion is the final entry on the page labeled "0002.")

The structure and styling code for the page can be seen below:

body {
  font-family: 'work sans', Arial, sans-serif;
  font-size: 14px;
  color: #999;
  margin: 0px;
  padding: 0px;
  background-color: #010101;
  background: url(https://imgur.com/YRl8KNP.png) fixed center;
  background-size: cover;
  text-align: left;
}

/* Additional CSS Code Snippets */

...

This world involves collaborative storytelling efforts where lore-related queries are answered by staff or community members. If you encounter questions related to the world-building aspect, feel free to explore them using the provided quick links or consult the relevant sections within the content.

Remember that your active participation and curiosity contribute to shaping the narrative of this expansive and immersive setting!

Answer №1

To address your initial inquiry, it seems that the .panel div is not closing all the way due to its intrinsic minimum height caused by the top and bottom padding.

A simple way to achieve the desired outcome without major code changes is to nest the text within a child div of .panel and transfer the padding property (and optionally the line-height property) to this nested element, named .panel__content.

div.panel {
  margin:10px 30px 20px 30px;
  background:rgba(0,0,0,0.3);
  display:block;
  max-height: 0;
  overflow: hidden;
  transition: 0.2s ease-out;
}

.panel__content{
  padding:40px;
  line-height: 170%;
}
<div class="panel">
  <div class="panel__content">English has now emerged as the predominant language globally, yet with easy access to information, one can learn any language, even those considered extinct! Linguistic diversity thrives in today's world, with many individuals being bilingual. The top three languages spoken in City 0023 are English, French, and Chinese.</div>
</div>

See working example on JSFiddle


Additional Notes:

  1. I recommend validating your HTML and CSS to resolve existing errors. Use W3C's online validators for best results:

  2. You might exclude the use of div in your .panel selector for a more concise approach. Shorter selectors are generally preferred.

  3. The display:block; property may be unnecessary for .panel, as divs default to block-level elements.

Answer №2

Here's a suggestion to enhance your panel:

box-sizing: border-box;

This addition will ensure that the padding is included in the height calculation. If necessary, you can make adjustments to other properties if there are slight shifts. Alternatively, you could consider setting the padding top and bottom to 0 when the panel is closed, and smoothly transition these values when collapsing.

Answer №3

When it comes to CSS, the max-height property takes precedence over the height property. However, keep in mind that any padding specified for the top and bottom will be added to the total height of the element when the CSS is compiled. In this case, the final height will be 80px.

To address this issue, adjust the padding as follows:

div.panel { padding: 0 40px; }

Furthermore, make sure to update your script accordingly:

var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
  acc[i].addEventListener("click", function() {
    this.classList.toggle("active");
    var panel = this.nextElementSibling;
    if (panel.style.maxHeight) {
      panel.style.maxHeight = null;
      panel.style.paddingTop = 0;
      panel.style.paddingBottom = 0;
    } else {
      panel.style.maxHeight = panel.scrollHeight + "px";
      panel.style.paddingTop = null;
      panel.style.paddingBottom = null;
    } 
  })
}

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 it necessary for parameter assertion in Javascript to throw an error immediately if the function returns a Promise

I've been immersed in the world of NodeJS code for quite some time now, observing various techniques employed by different developers. One question that has been on my mind is whether it's considered a best practice to have a function that, with ...

What is causing the newly generated input tags to disappear from the page?

I'm having an issue where my code successfully creates new input tags based on user input, but they disappear shortly after being generated. What could be causing this to happen? <form> <input type='text' id='input' /> ...

I am looking to gather user input in JavaScript and then showcase that input on the webpage. What would be the best

I am currently learning Java and I decided to challenge myself by creating a hangman game. The issue I am facing is that when the user inputs a letter, nothing happens - there is no output indicating whether the guess was correct or incorrect. I suspect th ...

Footer not disappearing

Need assistance! My footer isn't showing up properly at the bottom even after applying clear:both. Can someone help please? If anyone can provide guidance, it would be greatly appreciated. Thanks for all your help in resolving the issue. ...

How to efficiently transfer data between Node and Angular 7 using Electron

After setting up an Angular 7 application running on http://localhost:4200, I developed a Node JS application responsible for authenticating users on Facebook, accessible at http://localhost:3000. The callback redirection functions correctly within the No ...

Arranging divs with HTML and CSS

I am currently facing issues with the positioning of 3 scripts in my HTML file. The vertical gauge is overlapping the second circular gauge, despite trying different positioning options to no avail. How can I adjust the layout so that the vertical gauge a ...

Leveraging server-sent events (SSE) for real-time updates on a web client using JavaScript

I have been experimenting with server-side events (SSE) in JavaScript and Node.JS to send updates to a web client. To simplify things, I created a function that generates the current time every second: setTimeout(function time() { sendEvent('time&a ...

"Strategies for effectively utilizing the .find method to locate specific users within a database

I'm currently working on developing a User Authentication system, but I've hit a roadblock when it comes to verifying users. Specifically, I'm struggling with understanding how to iterate through all of my users in order to filter out their ...

Filtering database records using a static dropdown list in MVC 5: A step-by-step guide

Is there a way to filter database records based on the columns QtyRecieved, QtyRecievedand Void using a static HTML dropdown list? The QtyRecieved and QtyRecievedand column are both decimal values, while Void is a boolean. This is what I have attempted: ...

Is it possible to utilize v-html with message data in Vue without any limitations on the <title> element?

Currently utilizing Vue.js 2 and my SDK is IntelliJ: I am attempting to bring HTML content into a Vue.js file. The main objective is to include the <title></title> attribute, as it seems that Vue doesn't have direct support for this feat ...

The process of authenticating route parameters in Nuxt

I'm having trouble validating route parameters in my page component using the following code: async validate({ params, store }) { await store.dispatch(types.VALIDATE_PARAMS_ASYNC, params.id) } And here's the corresponding code in the store: ...

ReactJS not updating when multiple checkboxes are selected

Struggling to resolve an issue with this component. I encounter an error when trying to select multiple items using checkboxes. Oddly enough, another one of my components with the same code has no error. Please take a look at my code below, any help is gre ...

using mixins with styled-components

I have encountered a challenge while attempting to pass a mixin from Material UI to a styled-component. The problem lies in finding a way to transfer the mixin value to the styled-component without having to assign it to a css property. Unfortunately, dire ...

The number of subscribers grows every time $rootscope.$on is used

I currently have an Angular controller that is quite simple: angular.controller('appCtrl', function ($scope, $rootScope) { $rootscope.$on('channel.message', function () { // do something here } }); In addition, I have a ...

Trouble with updating the view when an array is modified in ng-repeat? It seems like using $scope.$apply() may not

When updating the array inside a function, the view does not automatically update. However, if you use console.log to check the array after pushing values, it shows the updated array. Even trying $scope.apply() inside $timeout did not solve this issue. Ja ...

The issue with clip-path not functioning correctly in Safari persists

My list of clipped elements is causing an issue when using the CSS3 clip-path method to clip an image into an SVG path. The clipping works perfectly, but in Safari (V 12.1.4), the position of the clipped element changes with each box. The element ...

Refreshing on current page with PHP [unveiling a new world of content]

My application consists of 4 pages that utilize PHP sessions. On page 1, users input their username and password, on page 2 they provide additional information, page 3 displays results and requests more details, and finally page 4 is the success page. Upon ...

Learn how to create a visually stunning CSS menu by centering a background image from the website cssmenumaker

I found a ready-made dropdown menu on . The menu is working well, but I would like to center the buttons. Is there a way to do this? Here is the CSS code: @import url(http://fonts.googleapis.com/css?family=Open+Sans:600); /* Menu CSS */#cssmenu, #cssmenu ...

AngularJS is failing to properly register custom directives

Looking to enhance the SEO caching capabilities of my AngularJS website, I've embarked on implementing a couple of directives for custom page titles and descriptions. Incorporated within my app config (angular.module('website', [...'we ...

Break down the organization of CSS

While exploring a website, I came across this CSS code that sets the font family to "Open Sans," Helvetica, Arial, and sans-serif. However, I am having trouble understanding this structure. Any assistance would be greatly appreciated. Thank you in advanc ...