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
PerpetualWarβ€’2y 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β€’2y 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
PerpetualWarβ€’2y ago
this is when modal is set to overflow-y:visible
PerpetualWar
PerpetualWarβ€’2y ago
those are shortcuts for date picker, I understand first pic on its own was confusing
MarkBoots
MarkBootsβ€’2y ago
why does the model need an overflow-y hidden?
PerpetualWar
PerpetualWarβ€’2y ago
it needs auto (or scroll) as modal is vertically scrollable based on content size
MarkBoots
MarkBootsβ€’2y ago
ah okay. yea, then make the date picker display: fixed, and calculate in js where it should be placed
PerpetualWar
PerpetualWarβ€’2y ago
ok, can you elaborate on how I should calculate correct position, if it is position fixed now?
MarkBoots
MarkBootsβ€’2y 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
PerpetualWarβ€’2y ago
are you saying I should get position of select element itself (input field so to speak)?
MarkBoots
MarkBootsβ€’2y 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β€’2y 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
PerpetualWarβ€’2y ago
ok, I think I get it, thanks, its very much appreciated πŸ˜‰ so this is the only way , to use fixed?
MarkBoots
MarkBootsβ€’2y ago
yea, if you really can not get around of the overflow. I think so
PerpetualWar
PerpetualWarβ€’2y 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β€’2y ago
again, hard to tell without live code if you can make a codepen, it would really help
PerpetualWar
PerpetualWarβ€’2y ago
Thanks, I will try your solution to make it work, appreciated πŸ˜‰
Jochem
Jochemβ€’2y 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β€’2y ago
yea was thinking that too, but i suspect the container already is relative
Jochem
Jochemβ€’2y ago
hm, yeah, that'd be a deal killer
MarkBoots
MarkBootsβ€’2y ago
i was thinking maybe just put the datepicker in a modal centered on the screen.