【发布时间】:2021-08-02 02:18:26
【问题描述】:
我正在尝试覆盖第三方组件的内联样式。
我关注了文档how can i override inline styles
所以,我使用&[style] 覆盖了内联样式,但这不起作用。
我使用的第三方组件是CookieConsent
现在,我的组件看起来像这样:
import React from 'react';
import CookieConsent from 'react-cookie-consent';
import styled from 'styled-components';
const StyledCookieBanner = styled(CookieConsent)`
&[style] {
justify-content: center;
align-items: center;
width: calc(100% - 20px);
margin: 10px;
padding: 20px;
background-color: white;
border-radius: 22px;
box-shadow: 0 0 19px 3px rgba(0, 0, 0, 0.17);
}
`;
const CookieBanner = (): React.ReactElement => {
return (
<StyledCookieBanner debug buttonText='Ok'>
Cookie
</StyledCookieBanner>
);
};
export default CookieBanner;
我也试过how can i override styles with higher specificity,但没有成功。
我发现覆盖样式的唯一方法是做类似的事情并使用!important
const StyledCookieBanner = styled(CookieConsent)`
> div {
justify-content: center;
align-items: center !important;
width: calc(100% - 20px) !important;
margin: 10px;
padding: 20px;
background-color: white !important;
border-radius: 22px;
box-shadow: 0 0 19px 3px rgba(0, 0, 0, 0.17);
}
`;
也试过了,没有成功
const StyledCookieBanner = styled(CookieConsent)`
> div {
// &&& {
&[style] {
justify-content: center;
align-items: center;
...
}
}
`;
文档看起来很清楚,但我没有成功。
我错过了什么吗?这是可能的还是我应该使用style 组件道具?
【问题讨论】:
标签: javascript css reactjs styled-components css-in-js