I am using the react-select library to create autocomplete drop-down.
I have used 2 drop-down parallel if 2nd has some data & I open first one then the zIndex problem comes. see the image
- 771
- 1
- 6
- 13
-
2Please add a [Minimal, Complete and Verifiable example](https://stackoverflow.com/help/mcve) to show us what you have done so far. – Björn Böing Apr 24 '19 at 13:12
-
1Very good question i just had the same problem today! – GOXR3PLUS Sep 23 '21 at 12:30
-
1https://thewebdev.info/2021/11/20/how-to-change-zindex-of-the-items-in-a-react-select-drop-down/ check this if it helps – Kishor Apr 12 '22 at 03:54
9 Answers
Add these lines in your all Select tag:
<Select
// other props
menuPortalTarget={document.body}
styles={{ menuPortal: base => ({ ...base, zIndex: 9999 }) }}
/>
- 3,185
- 2
- 16
- 32
- 2,233
- 2
- 13
- 14
-
-
2Save my day! It should be the most robust solution. If the parent container has `overflow: hidden`, just setting `menu` z-index to 9999 cannot prevent the popup cropped by the parent container. – Renyuan wang Apr 27 '21 at 08:38
-
You made my day, this solution is for the case u have a parent div which set overflow: hidden. `menuPortalTarget={document.body} ` did the trick – Minh Kha Jul 21 '21 at 04:43
-
2
-
1This solved my issue. I was opening it inside a small dialog, but I needed it outside. Thank you! – user.io Feb 09 '22 at 12:58
-
1For some reason, this removes any font styles applied to custom components. Overriding the z-index of `menu` instead of `menuPortal` does not do this. – ADK Apr 19 '22 at 05:08
-
yey!!! i am using react-select-country-list which uses react-select and with this code added to the select component it worked!!! thanks so much – Fo Nko May 25 '22 at 12:15
-
This breaks when scrolling. The menu does not move with the parent element. – NickC Jun 15 '22 at 21:47
-
Try this hacky way of setting zIndex, and let me know if it worked :)
<Select
styles={{
// Fixes the overlapping problem of the component
menu: provided => ({ ...provided, zIndex: 9999 })
}}
value={selectedOption}
onChange={evt => onSelectChange(evt, index)}
options={options}
/>
- 2,304
- 11
- 15
-
3depending of @user11124592's markdown and CSS you're solution is completely valid and not necessary "hacky" :) – Laura Apr 24 '19 at 15:57
-
4@Vicente thanks for your answer. If anyone else comes here and has trouble with this approach. You might be using the portal of react-select which in that case you need to use: ```menuPortal: provided => ({ ...provided, zIndex: 9999 })``` – Hudspeth Aug 26 '20 at 22:51
-
@Vicente, I have a similar problem, but I cannot display the zIndex. Do you have any other properties for the dropdown to overlap the other components? – Ruan Duarte Dec 03 '20 at 01:18
-
This is the superior solution. It's simpler and it can be applied to all Select components using a custom styles object. Additionally the other solution breaks when the page is scrolled as the portal is tied to the document body and not the menu element. Also it's not hacky, this is why z-index exists. – NickC Jun 15 '22 at 21:53
After seeing the half dozen related issues, yet not finding a resolution, I have finally found one.
<Select
menuPortalTarget={document.body}
menuPosition={'fixed'}
/>
Add these two options to your Select component.
It appears to make us of the new React Portal feature.
EDIT: If you do the above, you then run into this problem with the menu anchoring to the page due to position: fixed. Removing it may help. https://github.com/JedWatson/react-select/issues/4088
- 1,971
- 16
- 30
-
Correction here - the menuPosition={'fixed'} should be menuPosition="fixed", with double quotes. – technik Apr 28 '21 at 12:59
-
1Double quotes or single quotes both work. It's a style preference. – I Stand With Israel Apr 28 '21 at 13:45
-
-
-
This fixed my problem! I have a material-react-table w/ the form dropdown located above the table. Adding these 2 lines made it so the dropdown items are above the table! I used menuPosition="fixed" – aero8991 Jul 26 '23 at 16:21
For me the solution was kind of the total of all the answers (thanks!);
const customStyles = {
///.....
menuPortal: provided => ({ ...provided, zIndex: 9999 }),
menu: provided => ({ ...provided, zIndex: 9999 })
///.....
}
<Select
//.....
menuPortalTarget={document.body}
menuPosition={'fixed'}
styles={customStyles}
//.....
/>
- 1,321
- 1
- 19
- 29
-
1Adding yours "customStyles" only, works for me with "zIndex: 1000". But be careful with "Tooltips" or "Popovers" and add "zIndex" accordingly – michal Apr 06 '21 at 21:50
-
You can remove menuPosition={'fixed'} attribute. It prevents scrolling if menu list overflows page. – Ahmet Firat Keler May 25 '22 at 07:31
Another way is We can config the classNamePrefix and control it through the outer class.
import Select from "react-select";
<Select
classNamePrefix={"my-custom-react-select"}
/>
.my-custom-react-select__menu {
z-index: 2;
}
Bonus, We can re-style everything
.my-custom-react-select__control {
border-width: 1px !important;
border-color: #cbd5e0 !important;
padding-top: 0.16rem;
padding-bottom: 0.16rem;
}
.my-custom-react-select__control--is-focused {
border-color: #746df7 !important;
box-shadow: none !important;
}
.my-custom-react-select__indicator-separator {
display: none;
}
- 3,404
- 1
- 19
- 22
Just simply add the below two lines of code.
const customStyles = {
///.....
menuPortal: provided => ({ ...provided, zIndex: 5 }),
///.....
}
<Select
//.....
menuPosition={'fixed'}
styles={customStyles}
//.....
/>
- 91
- 4
Only combination of those answers made the working solution for me on Next.js. menuPortalTarget={document.body} broke the application with the error ReferenceError: document is not defined, which makes sense on SSG/SSR :)
menuPosition={"fixed"} as suggested by @I Stand With Israel with combination of answer from @hemant rao: menuPortal: (base) => ({ ...base, zIndex: 4 }),.
- 8,508
- 6
- 68
- 94
This is my solution when using "react-select" (version 4.3.1)
import ReactSelect from 'react-select';
...
...
<ReactSelect
className="custom_zindex"
...
...
/>
In the css file:
.custom_zindex {
z-index: 100; /* or 999 */
}
"className" is one of the props in react-select (https://react-select.com/props).
- 391
- 4
- 7
Change the zIndex of the parent component of this select
<div style={{zIndex:1000}}>
<React-Select-Component/>
</div>
- 285
- 1
- 8