is it possible to align div below other div even if they arent under same parent?

I'd like to know if its possible with css, I dont think so. but just in case asking here! for example in the select component, the dropdown will be in a portal so not under the same parent, so I think ive to get the trigger button's position and then make the dropdown aligned below it using top
No description
17 Replies
ἔρως
ἔρως3mo ago
just give it 100% width. here's an example: https://jsfiddle.net/y6j1xhrc/ as you can see, the width of the <p> is the same as the width of the .x element
Display two grid columns with background colors - JSFiddle - Code P...
JSFiddle - Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle.
ἔρως
ἔρως3mo ago
as long as the dropdown element has position: relative, your position: absolute element will take the 100% from the parent element
siduck
siduckOP3mo ago
thats not what i wanted! the dropdown isnt under the same parent its rendered in a portal so for example the body element
ἔρως
ἔρως3mo ago
then put the position: relative in whatever element is the "parent" you can also try something with sub-grid i can't tell you if it will work because i never tried it alternatively, just js it
siduck
siduckOP3mo ago
<body>
<div>
<button> abc </button>
</div>

MORE DIVS

<ul> dropdown here </ul>
</body>
<body>
<div>
<button> abc </button>
</div>

MORE DIVS

<ul> dropdown here </ul>
</body>
ya thats the only solution ig
ἔρως
ἔρως3mo ago
or just hard-code everything or use css variables for the width
siduck
siduckOP3mo ago
width isnt the concern but the positioning is
ἔρως
ἔρως3mo ago
why don't you move the <ul> under the <button>? or ... <details>?
siduck
siduckOP3mo ago
cuz it needs to be rendered outside the parent div
ἔρως
ἔρως3mo ago
why?
siduck
siduckOP3mo ago
No description
ἔρως
ἔρως3mo ago
oh, ai slop the true source of truthness
siduck
siduckOP3mo ago
all JS UI libs are using portal for custom <select> they render the dropdown div somewhere else so it wont get affected by the parent style of the <button>
ἔρως
ἔρως3mo ago
you will have to set the position in js, just like jquery used to do or maybe try something with the popup api probably you can get something to work with it but the support is still ... flaky https://developer.mozilla.org/en-US/docs/Web/API/Popover_API
siduck
siduckOP3mo ago
yea seen this idk how many years it'll take to make it stable 😭 looks mostly done
ἔρως
ἔρως3mo ago
i wouldn't use it without a fallback/polyfill for the next 2 years
Kingpin
Kingpin3mo ago
Popover is already stable, but you also will need Anchor which isnt.

Did you find this page helpful?