简体   繁体   English

如何在 React 中设置 select 元素的样式?

[英]How do I style a select element in React?

Right now I have a dropdown menu which looks like this:现在我有一个下拉菜单,如下所示:

我现在拥有的

But I want it to look like this:但我希望它看起来像这样:

图像必须如何

How can I style the HTML select element so that it becomes transparent like in the picture?如何设置 HTML select元素的样式,使其像图片中一样透明?

The GermanyLanguage component loads the SVG file for the flag. GermanyLanguage组件为标志加载 SVG 文件。 The SwitcherContent is a div styled by the styled-components package. SwitcherContent是一个由 styled-components package 设置样式的div

  return (
    <>
      <SwitcherContent>
        <GermanyLanguage />
        <select
          name="select"
          style={{ width: 120, marginLeft: 8 }}
          defaultValue={intl.locale}
          onChange={(e) => changeLocale(e.target.value)}>
          <option value="de">deutsch</option>
          <option value="en">english</option>
        </select>
      </SwitcherContent>
    </>
  );

export const SwitcherContent = styled.div`
  display: flex;
  flex-direction: row;
  align-items: center;
`;

background: 'bottom' dint help me, now switcher looks like Image背景:“底部”帮助我,现在切换器看起来像图像

I think you can do like this.我认为你可以这样做。

<GermanyLanguage/> -> SVG file
  return (
    <>
      <SwitcherContent>
        <GermanyLanguage />
        <select
          name="select"
          style={{ width: 120, marginLeft: 8, background:"bottom" }}
          defaultValue={intl.locale}
          onChange={(e) => changeLocale(e.target.value)}>
          <option value="de">deutsch</option>
          <option value="en">english</option>
        </select>
      </SwitcherContent>
    </>
  );

export const SwitcherContent = styled.div`
  display: flex;
  flex-direction: row;
  align-items: center;
`;

Every browser defines its own base stylesheet for built-in elements like select.每个浏览器都为 select 等内置元素定义了自己的基本样式表。 You need to override all of the default styled properties with the styles that you want.您需要使用所需的 styles 覆盖所有默认样式属性。

You can change the styles by adding properties to the style prop like you have done with style={{ width: 120, marginLeft: 8 }} , by using styled-components like you have done with SwitcherContent = styled.div , or by declaring classes in a CSS file and setting the className prop on your components.您可以更改 styles,方法是向style属性添加属性,就像使用style={{ width: 120, marginLeft: 8 }}一样,使用 styled-components 就像使用SwitcherContent = styled.div ,或者通过声明CSS 文件中的类并在组件上设置className属性。

You will need to override background , color , border , and possibly more.您将需要覆盖backgroundcolorborder ,可能还有更多。 The background property could be transparent , unset , or black . background属性可以是transparentunsetblack

This should get you started in the right direction:这应该让您朝着正确的方向开始:

return (
  <>
    <SwitcherContent>
      <GermanyLanguage />
      <select
        style={{
          width: 120,
          marginLeft: 8,
          background: "black",
          color: "white",
          border: "none"
        }}
        name="select"
        defaultValue={intl.locale}
        onChange={(e) => changeLocale(e.target.value)}
      >
        <option value="de">deutsch</option>
        <option value="en">english</option>
      </select>
    </SwitcherContent>
  </>
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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