简体   繁体   中英

styling react-select using classes

I am using react-select in my code. https://www.npmjs.com/package/react-select

I want to style my drop down using classNames, so that I referred https://react-select.com/styles . The DOM structure of react slect is shown in the link.

How can I style the react-select using classNames?

Can anyone show a sample code?

According to the documentation

If you provide the className prop to react-select, the SelectContainer will be given a className based on the provided value.

So it should work like this:

<Select className="abc" .... />

Then you can use your classname as usual.

.abc {
color: red;
}

You can also use classNamePrefix

by specifing a classNamePrefix, react-select will render all classNames with your prefix. If you use this:

<Select className="abc" classNamePrefix="react-select" ... />

Your Select will automatically render a class structure like this: 在此处输入图片说明

See their example:

For example, given className='react-select-container' and classNamePrefix="react-select", the DOM structure is similar to this:

<div class="react-select-container">
  <div class="react-select__control">
    <div class="react-select__value-container">...</div>
    <div class="react-select__indicators">...</div>
  </div>
  <div class="react-select__menu">
    <div class="react-select__menu-list">
      <div class="react-select__option">...</div>
    </div>
  </div>
</div>

So in your css, simply do:

.react-select-container {
  background-color: 'red';
}

.react-select__menu {
  height: 100vh;
}

etc

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