I have a div container containing 4 divs. There are 2 headers and 2 content divs in it. Clicking on the headers will toggle the visibility of the content divs below them.
I would like to consider the following 3 situations:
This is the structure of HTML
Header 1LoremLoremLoremLoremLoremLoremLoremLoremLoremHeader 2IpsumIpsumIpsumIpsumIpsumIpsumIpsumIpsum
This is the style I tried to apply. Please note that I'm using Sass.
.flex-container { display: flex; flex-direction: column; height: 300px; width: 150px; overflow-y: auto; } .header { display: flex; align-items: center; min-height: 35px; background-color: #99e9f2; cursor: pointer; } .header-content { flex-basis: calc(50% - 35px); flex-grow: 1; overflow-y: auto; display: flex; flex-direction: column; .item { padding: 3px 12px; background-color: #e3fafc; } }
This is the link to the code pen.
max-heigth: calc(50% - 35px)
(title height is 35px), which solved the issue with the growing content div causing whitespace gaps to appear, but This also makes it so that if another content div is closed, the content div will not grow past that height. flex-grow: 1
instead of flex-basis: calc(50% - 35px)
and max-height : calc(50% - 35px)
can make the content div grow more than about 50% of the container height, but if a content div has more elements, it will cause the height of the content div to be uneven, resulting in uneven available space. Distribute evenly.
This way you can achieve what you want to do. You can modify it as needed: