【问题标题】:child css is being override by the parent子 css 被父级覆盖
【发布时间】:2022-12-18 16:18:07
【问题描述】:

我在我的 React 项目中使用 Styled-Component。我注意到我的子组件 CSS 被其父组件覆盖了。

这是父组件及其样式组件包装器:

import styled from "styled-components";
import Child from "./Child";

const Parent = () => {
  return (
    <ParentWrapper>
      <div className="nice-box" />
      <Child />
    </ParentWrapper>
  );
};

const ParentWrapper = styled.div`
  .nice-box {
    border: 3px solid black;
    height: 50px;
    width: 50px;
  }
`;

export default Parent;

这是子组件:

    import styled from "styled-components";

const Child = () => {
  return (
    <ChildWrapper>
      <div className="nice-box" />
    </ChildWrapper>
  );
};

const ChildWrapper = styled.div`
  .nice-box {
    border: 3px solid green;
    height: 100px;
    width: 100px;
  }
`;

export default Child;

这是结果: result

【问题讨论】:

标签: css reactjs styled-components


【解决方案1】:

我的建议是,使用类名并将样式仅应用于那些类。仅此而已

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-30
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    • 2012-10-07
    • 2019-11-22
    • 2022-01-19
    相关资源
    最近更新 更多