r/csshelp • • Jun 08 '22

Request Is it possible to make a resizable "vertical split" with CSS only? That is, without JS?

I'm trying to build a generic web-component to provide a resizable vertical split. I've come across some ideas of providing this via JavaScript, binding to mouse events and calculating the sizes of the two "panels" in the split-component.

However, I've also seen that CSS nowadays has the resize property. This seems a lot less fiddly and - hopefully - also less buggy.

However, making the components "stick" to an outer container with fixed size proves challenging.

After a lot of fiddling around, I have two examples, one based on flexbox, the other one without. Both work half-way. But neither is "good enough" to consider this "done".

The flexbox solution sticks to the outer-container constraints but feels "rubbery" and has other annoying side-effects.

The basic solution feels perfect, but it does not constrain itself to the parent container.

Any ideas how to deal with this? Or will I be forces to add a pinch of JS to this?

Expected behaviour: https://imgur.com/a/tMFtqMf

8 Upvotes

5 comments sorted by

1

u/chlocodile Jun 08 '22

Do you have any examples you could provide of what you’re trying to do?

1

u/exhuma Jun 08 '22

It's pretty hard to demonstrate. I added an image which - hopefully - helps to understand the expected behaviour.

1

u/sadshark Jun 10 '22 edited Jun 10 '22

Edit: Here's an example: https://jsfiddle.net/bjk3n16t/24/

Set the height of the top container to 30% (or whatever) and the bottom container to 100%.

Then add to first container "flex-grow: 0; flex-shrink: 0;". And add to the bottom container "flex-grow: 1; flex-shrink: 1;"

Also set the wrapper (blue in your case) height to 100vh.

You mistake was setting the bottom container to anything less than 100%. It needs to be 100% in order to fill the wrapper regardless of the top container's size.

Personally I would still use JS because "resize" doesnt seem to work on mobile and on desktop is bad UI, you can barely see the resize icon and you can only resize from the corner.

1

u/exhuma Jun 12 '22

Thank you so much. This works perfectly.

Concerning JS: My original post really made it sound like I wanted to strictly avoid JS. This is not the case. But I felt like I was getting very close to a solution without dealing with JS just yet, and for a first iteration this will do just fine. I agree that it is unfortunate that the handle is only in a corner. But I can iterate on that while I have already something out that is usable.

A JS solution is non-trivial. While not overly complicated it still has a much larger code footprint and higher chances of bugs to creep in. I can use the CSS only version a fallback until they have been dealt with.

Concerning mobile: This component is used in a web-application which is not targeting mobile platforms. So that should do - for now.

Again, thanks for the help.