Using Flexbox to create collapsible elements that maintain a consistent width

Within my flexbox setup, I am utilizing disclosure elements such as <details> and <summary>. Is there a way to ensure that when the details are collapsed, they occupy the same width as when they are expanded? Currently, there is a fluctuation in width when expanding and collapsing: https://i.sstatic.net/51N6y.gif

A visual representation of the flexbox containing these two elements can be seen in this image:

https://i.sstatic.net/woWoM.png

The desired outcome is for the collapsed width to match the expanded width, ideally without needing to assign fixed widths....

Answer №1

In my opinion, the most effective method would be to assign a width to the container of the accordion. However, if you are hesitant to specify widths explicitly, there is an alternative approach you can consider.

Instead of concealing your collapsible item using display: none, you can utilize visibility: hidden instead. This will make the element invisible and non-interactive, while still maintaining the necessary width for rendering as the element remains present. To eliminate the vertical space, set height: 0.

I have prepared a sample code snippet for you to review:

function toggle(el) {
  el.children[0].classList.toggle('hide')
}
* {
  font-family: sans-serif;
}

.main {
  background: steelblue;
  display: flex;
  width: 100%;
}

.sidepanel {
  background: crimson;
  display: flex;
  min-height: 100vh;
  color: white;
  padding: 20px 10px;
}

.container {
  display: flex;
}

.hide {
  visibility: hidden;
  height: 0;
}
<div class="container">
  <div class="sidepanel">
  <ul onclick="toggle(this)"> Click Me!
  <div class="hide">
  <li>Data</li>
  <li>Data</li>
  <li>Data that has some words</li>
  </div>
  </ul>

  </div>
  <div class="main"></div>
</div>

However, one drawback of this strategy is that without setting a width on the left panel, it may occupy excessive space if the menu items do not wrap their text. Text wrapping implies a sort of predetermined width, so it's something to keep in mind...

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

Unusual actions observed when using CSS pseudo selector :checked

I have implemented a unique CSS solution to showcase a five-star rating system on my website. The method I followed can be found at . While this technique works flawlessly in some sections of my website, it strangely fails in others. Could this be due to c ...

What is the best way to populate the "value" field in a form with checkbox input type from a database?

I've been struggling to retrieve values from the database and insert them into a checkbox array, but so far I've had no luck. Can anyone provide some assistance? *The 'img[]' section is always empty; it seems like no value is being pas ...

Disable a button during an AJAX request

Whenever a button is clicked, the record is saved to the database without refreshing the page using AJAX. I have implemented the AJAX code successfully. Now I need guidance on how to manage the state of the Submit button - enabling/disabling it dynamicall ...

The CSS I've written isn't functioning properly within a div that was generated using

I have been trying to populate a div with data using JSON, but I am facing an issue where my div, which is set with JavaScript for each JSON element, does not seem to be working correctly. The classes "lulu" and "lora" are not being recognized. Apologies f ...

The navigation buttons in Flexslider can't be seen when hovered over, accompanied by an orange box

Currently, I am incorporating FlexSlider 2 into a WordPress website. I am facing an issue where the back and forth navigation buttons are not functioning properly. The back navigation button appears as just an orange box, and the forward navigation button ...

Aligning a Material UI button to the far right of a row

I am struggling to align a React material-ui button to the right-most of the page and despite trying to use the justify="flex-end" attribute within the following code, it doesn't seem to be working: <Grid item justify="flex-end" ...

Troubleshooting problem with displaying HTML content on Internet Explorer 10

My website was developed using jquery with a dynamic coding style. It was functioning properly in IE10 while working from Visual Studio. However, after deploying it to the production server, the entire style seemed broken in IE10. In all other versions o ...

What is the reason this JavaScript code functions properly within a <script> tag but not when placed in a separate .js

After researching various image sliders online, I came across this specific one. It worked perfectly fine when I included the JavaScript directly in a script tag within the HTML file. However, as soon as I tried to link it via a .js file, the slider stoppe ...

How can we enhance the integration of HTML and PHP for a smoother workflow?

Are there alternative solutions, aside from using a template engine like Smarty, for managing large code like this? <table class="table table-striped dataTable table-bordered"> <thead> <tr> <?php $o ...

Is the Mini Cart button in Woocommerce not aligned properly?

My website was functioning perfectly until yesterday. However, after updating some plugins, the Mini Cart dropdown button started to display in a weird and misaligned manner. https://i.sstatic.net/SsZee.jpg Prior to the plugin updates, the button had the ...

issue with retrieving HTML data from PHP and converting it to JSON

I've tried multiple search queries but nothing seems to be working. Can someone please assist me? ----------jQuery Function ---------------------------- function showRequestFunctionToBox(thisId){ var encodedItemId = thisId; ...

Interactive section for user input

I am looking to add a commenting feature to my website that allows for dynamic editing. Essentially, I want users to be able to click on an "Edit" span next to a comment and have it transform into an editable textarea. Once the user makes their changes and ...

Tips for refreshing Facebook's og tags

I've been racking my brains over this issue for days, and despite the abundance of similar inquiries, I have yet to find a solution. On my blog-like website, each post requires its own title and description for Facebook articles. I know I can set the ...

Caution CSS Division featuring an Icon

When completing a domain purchase, I designed a personalized warning message or division that appears during checkout. However, upon implementing my own CSS, The lock icon is supposed to appear on the left side of the text but instead displays inline. ...

Jquery is causing some issues with the code provided:

This is the code snippet from script.js: $(document).ready(function() { $('#mainobject').fadeOut('slow'); }); Here is a glimpse of index.html: <!DOCTYPE html> <html> <head> <title>Hitler Map&l ...

How can you verify the inputted age and execute the correct conditional statement?

Currently, I am going back to basics in JavaScript and realizing that my foundation is not as strong as I had assumed. It seems like I need a fresh perspective because I can't seem to pinpoint where the code error lies. (A lot of the resources I foun ...

Utilizing two distinct CSS frameworks within a single Rails application

At the moment, I have an application with its frontend built using Bootstrap 3 (leveraging the bootstrap-rails gem). Now, I am looking to incorporate the Materialize.css framework into the same app. After downloading the Materialize.css stylesheet, my conc ...

Exchanging properties of jQuery selected elements

Context I am currently developing a web application using .NET 2.0 that utilizes standard WebControls. While the eventual plan is to migrate to version 4.x, I have an immediate need to modify its mobile behavior by incorporating HTML5 form input types for ...

The entire page is filled by the image link block

I created a primary div to serve as a container with a width set to 90%. At the top of this div, I have a title (image) with styling attributes such as height: 5em, display: block, and margin: auto. In my HTML structure, it is organized like this: <a h ...

The navigation bar and footer are not utilizing the entire width of the browser

Something seems off on the Safari browser, as there's still a gap in the right side of the navbar and footer. I've been using Bootstrap and steering clear of Javascript. Any ideas on how to fix this? Appreciate any help. Thanks! Here's the ...