What is the best way to open up a parent CSS accordion when the child accordion's border is covering the parent's border?

Exploring Options and Background

https://example.com/jsfiddle

I have been experimenting with creating an expandable/collapsable accordion for displaying restaurant food choices, envisioning a digital menu akin to Just-Eat or Hungry House.

My attempt involves using W3Schools' Animated Accordion tutorial to build this digital food menu. https://www.example.com/howto_js_accordion

The Challenge at Hand

In the code snippet, you can observe that I have tried to place the

<button class="accordion">
within another element.

The issue arises when expanding the nested accordion; it fails to enlarge its parent container, leading to obscured or truncated child content.

To address this problem, collapsing the parent accordion after opening the child one is necessary. However, this approach may inconvenience users.

Suggested Course of Action

A potential solution could involve adjusting the CSS properties to ensure that the accordion containers automatically adapt to their content. Currently, they seem rigid until clicked on.

I am also open to alternative suggestions regarding the implementation of this 'digital menu' concept.

Sample Code Illustration

https://example.com/jsfiddle

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;
    } else {
      panel.style.maxHeight = panel.scrollHeight + "px";
    }
  });
}
.accordion {
    background-color: #eee;
    color: #444;
    cursor: pointer;
    padding: 18px;
    width: 100%;
    border: none;
    text-align: left;
    outline: none;
    font-size: 15px;
    transition: 0.4s;
}

.active, .accordion:hover {
    background-color: #ccc;
}

.accordion:after {
    content: '\002B';
    color: #777;
    font-weight: bold;
    float: right;
    margin-left: 5px;
}

.active:after {
    content: "\2212";
}

.panel {
    padding: 0 18px;
    background-color: white;
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.2s ease-out;
}
<button class="accordion">Section 1</button>
    <div class="panel">
      <p>The Text in Section 1</p>
      <button class="accordion">Section 2</button>
      <div class="panel">
        <p>The Text in Section 2</p>
      </div>
    </div>

Answer №1

If you're tired of setting a fixed value for the max-height property of your div.panel, consider changing it to initial instead, as shown here.

Nevertheless, be aware that this adjustment may disrupt your animation. You have two options: switch to using jQuery for animations (.slideUp/.slideDown or .slideToggle), which avoids this issue altogether, or get creative with some timer tricks: https://jsfiddle.net/wwx100f8/80/

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

Text loaded dynamically is not remaining within the div

I am experiencing an issue with dynamically loaded content where Images work fine, but Links and Text are not displaying properly. For reference, you can view the problem in this JSFiddle: http://jsfiddle.net/HRs3u/1/ I suspect that it might be related t ...

Error message when using Vue Global Filter: Value undefined is not defined

Trying to format currency, I initially attempted to use a global filter in Vue: Vue.filter('formatMoney', (val) => { if (!value) return '' val = val.toString() return val.replace(/\B(?=(\d{3})+(?!\d))/g, ",") ...

Add a horizontal line between two div elements to create a visual division in the middle

Here is a snippet of my HTML/CSS/JS code: <div id="blockcart-wrapper"> <div class="blockcart cart-preview"> <div class="header"> <a rel="nofollow" href="#"> <img class="cart-icon" src="https://via.placehold ...

Tips for sending a Json array to a servlet

[DUPICATE] Here is the JSON code I am using for my POST request: var data_create = JSON.stringify($("#form_create_delegate").serializeArray()); alert("data_create content" + data_create); // console.log(data_create) $.ajax({ ...

Warning from Socket.io: The client has not completed the handshake and should attempt to reconnect

My code has been causing some trouble lately. Even after seeking help from various forums, I am still facing an issue with my nodejs. It keeps disconnecting and sending me a warning message that says: warn: client not handshake client should reconnect. ...

Incorporate an additional retrieve functionality in your asp.net web api

I'm brand new to the world of asp.net web api. I have a solid understanding of get(), put(), post() and delete(). For my application, I need to implement two additional get() methods. Here is an overview- public class StudentController : ApiControll ...

Managing form submissions in React with inputs spread across multiple components

I've been working on a ReactJS project with a form that is divided into multiple components. The main component imports all the child components, each containing input boxes, along with a submit button: My issue lies in trying to get all the componen ...

Tips for effectively running multiple XMLHttpRequests in a loop

While running XMLHttpRequest inside .map(), everything operates smoothly. list.map(function(file) { const xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () {} xhr.onerror = function () {} xhr.upload.addEventListener(& ...

Could someone please review my CSS code for the dropdown menu? I'm having trouble centering the navigation menu

As a newcomer to coding in CSS, I have tried the suggested solutions for my issue without success. Any help would be greatly appreciated. My goal is to center my menu on the page while keeping the container background covering the full width available... ...

Django causing CSS issues following the utilization of an if statement

I have been working on adding a dropdown list to my navbar, which I created using Bootstrap. However, whenever I place the dropdown menu inside an if statement for looping categories into a list, it seems to be breaking the CSS. Check out the navbar witho ...

Selecting elements in jQuery with attributes that begin with a particular string

I am seeking a way to select all elements with an attribute that begins with a specified prefix - specifically referring to the attribute name, not the value. Let's consider this example: <div data-abc-name="value">...</div> <a href=". ...

Disabling font-awesome checkboxes

As someone who is new to front-end technologies, I am trying to achieve the following. Despite my efforts to find a solution, I have not been successful so far. Here is the issue I am facing-- I have two master checkboxes labeled 1) I do not like numbers ...

Floating navigation bar that appears and disappears as you scroll

My webpage has a dynamic navbar that consists of two parts: navbarTop and navbarBottom. The navbarTop should appear when the user has scrolled down more than 110 pixels, while the navbarBottom should show up when the user scrolls up. The issue I am facing ...

New approach of using Django to replace Ajax method

My current input elements are: <input id="country_name" type"text" /> <input id="country_id" type"text" /> I am looking to dynamically update the ID based on the country name entered in the country_name input using the onchange event. To achi ...

Unable to maintain sequential IDs for textboxes using Javascript or JQuery

I am encountering a problem involving the addition and deletion of multiple text boxes using buttons. The issue lies in maintaining sequential IDs for each textbox. Below is an explanation of my code: <div class="form-group" id="intro-box"> < ...

Error in node.js: Headers have already been sent

I have set up a routes.js file in nodejs and all routes are working fine except for the "UPDATEAUTH" route. I am facing an error which says "Headers already Sent". The specific line causing this issue is -> router.post("/updateauth",function(request,res ...

Struggling to effectively transfer a callback function within a series of functions

I am currently utilizing the codebird library to make requests to the Twitter API. The responses from these requests are functioning as expected, but I am looking to pass that response along to my route. Below is a segment of my route.js: router.get(&apos ...

Is it possible to assign a class to the parent element when a Bootstrap 4 dropdown menu is expanded?

Is there a way to include a class in the parent element when a Bootstrap Dropdown is open? Here is the HTML code: <div class="item-content"> <div class="block-handle"> <div class="dropdown"> <a cl ...

The save changes feature is not effectively syncing with the database

So, I have a button that triggers a javascript function, which initiates an AJAX request to call an actionresult that is supposed to update my database. Javascript Function function changeDepartment() { // Initializing and assigning variables va ...

Express Module Employs Promises for Returns

I have a JavaScript file for elasticsearch (could be any other database as well) that performs a simple query and uses a promise to return the data. I am using this module in my Express server (server.js) with the hope of retrieving the data, as I ultimat ...