Hi, I'm an old webdev who is rusty in CSS; I learn about calc() recently and never really learnt display: flex properly.
I made some webs with a responsive menu layout (relevant CSS code posted on bottom). I tried using flex but I still had to do one ugly hack: I have the menu heights for the different resolutions hardcoded and I have to update them manually every time a new chapter is added. It's not a big deal, but I would like to know if there is a proper way to do this.
Some alternatives I've thought about:
The new round(), but it's too new and not supported by most browsers.
JavaScript
... but I feel like there must be a clean CSS-only way to achieve this.
I'm not entirely sure what behavior you're trying to achieve with the hard-coded heights. Can you describe how you want the menu to behave? I do think it's very doable in pure CSS without any magic numbers.
If your goal is just to make sure the menu creates the right number of "columns", you should try using display:grid instead of flex. It gives you much more control over that kind of layout, and you can set it up to automatically fill the available area with columns of constrained width. You can then also have items populate vertically instead of horizontally (grid-auto-flow:column).
Other stuff that seems weird to me - why set the height to 10,000px? Also, if the menu links just take you to anchors lower down on the page, consider using the :target pseudo-class so that only the selected one is shown, the rest can be display:none.
I want it to behave exactly how it's behaving now. Trying to explain it with words is much harder :/.
I do think it's very doable in pure CSS without any magic numbers.
I agree, I would be very surprised if that's not the case.
you should try using display:grid instead of flex. It gives you much more control over that kind of layout
I didn't even know display: grid exists... I'm really outdated. I will give it a try!
why set the height to 10,000px?
Yeah, that's really weird. It's been a long time since I started this and I don't remember. As the comment says, it's later reset on JS:
$('html, body').css('min-height', 0);
I think it was related to some weird case when you reload the page ant the content being slow to render (because the KaTeX formulas). But I removed the code and tried to replicate any unwanted behavior and I wasn't able. So it's probably dead code.
if the menu links just take you to anchors lower down on the page, consider using the :target pseudo-class so that only the selected one is shown, the rest can be display:none.
I didn't know about :target either, thanks for the tip. I just took a quick look at the documentation and, from what I see on the demos, it seems that only one chapter could be open at once, but I want to allow to open multiple chapters. Is that possible with :target?