position absolute inside overflow scroll container

This is normal css behavior, however sometimes we need to allow certain dropdowns to go outside scrollable container (think datepicker inside scrollable modal OR context menu dropdown inside scrollable table). How to circumvent this, I'm open to JS solution as well as I doubt it can be solved with css only.
21 Replies
PerpetualWar
PerpetualWarOP•3y ago
In this example you can see datepicker is cut off on left side by modal which is set as overflow-y: auto
MarkBoots
MarkBoots•3y ago
Bit hard to understand the question without (live) code. It seems the date picker shows 2 full month, so i don't understand what is 'cut off'. You could try and see what position: absolute or fixed does.
PerpetualWar
PerpetualWarOP•3y ago
this is when modal is set to overflow-y:visible
PerpetualWar
PerpetualWarOP•3y ago
those are shortcuts for date picker, I understand first pic on its own was confusing
MarkBoots
MarkBoots•3y ago
why does the model need an overflow-y hidden?
PerpetualWar
PerpetualWarOP•3y ago
it needs auto (or scroll) as modal is vertically scrollable based on content size
MarkBoots
MarkBoots•3y ago
ah okay. yea, then make the date picker display: fixed, and calculate in js where it should be placed
PerpetualWar
PerpetualWarOP•3y ago
ok, can you elaborate on how I should calculate correct position, if it is position fixed now?
MarkBoots
MarkBoots•3y ago
in js you can use Element.getBoundingClientRect() to get the position and size of your drop down, then place the datepicker accordingly with top, left position
PerpetualWar
PerpetualWarOP•3y ago
are you saying I should get position of select element itself (input field so to speak)?
MarkBoots
MarkBoots•3y ago
if you want to align the datepicker to the bottom right of the select get the x y position (bottom right) x: element.x + element.width y: element.y + element.height
MarkBoots
MarkBoots•3y ago
Element.getBoundingClientRect() - Web APIs | MDN
The Element.getBoundingClientRect() method returns a DOMRect object providing information about the size of an element and its position relative to the viewport.
PerpetualWar
PerpetualWarOP•3y ago
ok, I think I get it, thanks, its very much appreciated 😉 so this is the only way , to use fixed?
MarkBoots
MarkBoots•3y ago
yea, if you really can not get around of the overflow. I think so
PerpetualWar
PerpetualWarOP•3y ago
I had some idea like to somehow track if select is open, and if its open, I replace overflow: auto with overflow: visible, so it does not contain absolute elements anymore your solution is better to not make modal jerky (showing scrollbar and removing scrollbar)
MarkBoots
MarkBoots•3y ago
again, hard to tell without live code if you can make a codepen, it would really help
PerpetualWar
PerpetualWarOP•3y ago
Thanks, I will try your solution to make it work, appreciated 😉
Jochem
Jochem•3y ago
I think if you set position: relative on the grandparent of the datepicker, and then position: absolute; on the datepicker, it'll pull it out of the parent and that would let it overflow the parent's side. what I'm a bit confused about though is that overflow-y is messing with the x overflow but again, both are very hard without live code to mess around with
MarkBoots
MarkBoots•3y ago
yea was thinking that too, but i suspect the container already is relative
Jochem
Jochem•3y ago
hm, yeah, that'd be a deal killer
MarkBoots
MarkBoots•3y ago
i was thinking maybe just put the datepicker in a modal centered on the screen.
Want results from more Discord servers?
Add your server