2

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!

4
  • 1
    did you try giving !important for your styles Commented Oct 2, 2020 at 9:35
  • @SarunUK I did think about it but it's not an ideal solution because it's not always working and not the most compatible :( thanks Commented Oct 2, 2020 at 9:49
  • Prop className will replace the SelectContainer with the className's value. please check the style guide for more information. react-select.com/styles Commented Oct 2, 2020 at 11:35
  • @MuhammadHarisBaig please read my question again Commented Oct 2, 2020 at 16:42

1 Answer 1

4

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;
}

enter image description here

Live Example

Edit suspicious-sunset-kb6is

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. Though I thought it could be this way you saved my work time :)
Thank you. I shoulda looked this up about 2 hours ago... smh.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.