简体   繁体   中英

Styling react-select component with CSS className in Reactjs

I'm using react-select and I'm trying to style the Select component like written in the DOCS but adding a class with className does not work. The class tag is added to the DOM element's classes but doesn't affect the element itself. The theme stay the same.

If I add a class like this:

<Select
    className='my-class'
    {...selectProps}
/>

The DOM element looks like this:

<div class="my-class css-2b097c-container">
...
</div>

Which means that the default class css-2b097c-container of react-select will always "override" my custom class. I've tried to use the classNamePrefix option but did not work either :(

In order to keep my UI design complete I need to design it with classes and not with inline styling!

classNamePrefix works for most but not all components which is kinda annoying to me. Another solution is to add your own className as stated in your question but refer to both of them in your css using [attribute*=value] selector to increase specificity.

<Select
  className="mySelect"
  options={options}
/>
.mySelect[class*="-container"] {
  background-color: lemonchiffon;
  padding: 10px;
}

在此处输入图片说明

Live Example

编辑可疑日落-kb6is

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM