【问题标题】:style my placeholder in react select using css使用 css 在反应选择中设置我的占位符
【发布时间】:2019-12-13 11:28:57
【问题描述】:
  • 我是 react select 的新手。
  • 我正在尝试使用 css 设置占位符的样式
  • 所以我在沙盒中构建了一个原型。
  • 我尝试使用 css 更改占位符的颜色。
  • 我将颜色设为红色,但仍未覆盖
  • 你能告诉我如何解决它吗?
  • 在下面提供我的沙箱和代码 sn-p。

  • 即使我搜索了占位符 css,得到了这个链接但仍然没有帮助我

https://www.w3schools.com/cssref/sel_placeholder.asp

https://codesandbox.io/s/react-codesandboxer-example-1q8nd

<Select
    defaultValue={""}
    label="Single select"
    placeholder={" Placeholder test "}
    options={flavourOptions}
    theme={theme => ({
      ...theme,
      borderRadius: 0,
      colors: {
        ...theme.colors,
        primary25: "hotpink",
        primary: "black",
        background: "red",
        color:"red",
      }
    })}
  />

【问题讨论】:

    标签: javascript html css reactjs redux


    【解决方案1】:

    根据react-select docs,neutral50 响应占位符的颜色。

    试试这个:

    <Select
      defaultValue={""}
      label="Single select"
      placeholder={" Placeholder test "}
      options={flavourOptions}
      theme={theme => ({
        ...theme,
        borderRadius: 0,
        colors: {
          ...theme.colors,
          neutral50: 'red',  // Placeholder color
        }
      })}
    />
    

    或者你可以简单地做如下:

    export default () => {
      return (
        <div>
          <p>
            We see that the final rendered output is not an input box, but rather a
            set of divs with certain classes applied on them... so we just change
            the class
          </p>
          <Select
            defaultValue={""}
            label="Single select"
            placeholder="Placeholder test"
            styles={colorStyles}
            options={flavourOptions}
          />
        </div>
      );
    };
    
    const colorStyles = {
      placeholder: defaultStyles => {
        return {
          ...defaultStyles,
          color: "blue"
        };
      }
    };
    

    查看此示例:Example

    【讨论】:

    • 嗨,是否可以在没有主题选项的情况下修复它,因为在我的代码库中我们没有使用任何主题以及这个数字的用途neutral50
    • 我尝试使用 &amp;::placeholder 解决解决方案,因为你们的解决方案在代码库中不起作用,在沙箱中提供我更新的代码,你能帮助codesandbox.io/s/react-codesandboxer-example-bv3di
    【解决方案2】:

    占位符实际上是一个 div,所以你可以通过 CSS 来设置样式:

    .css-1wa3eu0-placeholder {
      color: red !important;
    }
    
    p {
      color: blue;
    }
    
    .myDiv > div:nth-child(2) > div > div > div:nth-child(1) {
      font-size: 9px;
      color: orange !important;
    }
    

    完成working example here

    【讨论】:

    • 嗨,是否可以在没有主题选项的情况下修复它,因为在我的代码库中我们没有使用任何主题以及这个数字的用途1wa3eu0
    • 主题选项在您的代码中使用并在我分叉您的示例时被复制 - 如果您确实检查元素(浏览器开发人员工具),您会看到输入实际上是一个 div 和类在该 div 上名为 css-1wa3eu0-placeholder 或者您可以遍历到确切的占位符 div 并应用您的样式
    • @xoi 你看过 Akber 提供的完整工作示例吗?
    • 我也想出了与您收到的两个答案类似的解决方案,如果它们还不够,我会说您的问题和代码框上的示例代码需要更新。
    • @Akbar:我尝试使用 &amp;::placeholder 解决解决方案,因为你们的解决方案在代码库中不起作用,在沙箱中提供我更新的代码,你能帮助codesandbox.io/s/react-codesandboxer-example-bv3di
    猜你喜欢
    • 1970-01-01
    • 2017-11-03
    • 1970-01-01
    • 2018-10-13
    • 2011-12-25
    • 1970-01-01
    • 2020-06-24
    • 2017-11-18
    • 1970-01-01
    相关资源
    最近更新 更多