Antd – 有没有办法使用ConfigProvider更改内容的背景颜色?

huangapple go评论65阅读模式
英文:

Antd - is there any way to change Content background color with ConfigProvider?

问题

我尝试使用ConfigProvider将内容背景颜色更改为白色,以便不必在每个组件中更改背景颜色。我找到了Header和Sider的解决方案,但Content给我带来了一些问题。是否有任何魔法解决方案?

以下是主索引文件中的ConfigProvider:

<ConfigProvider
  theme={{
    components: {
      Layout: {
        colorBgHeader: '#fff'
      }
    }
  }}
>
  <App />
</ConfigProvider>

对我来说唯一有效的更改组件中背景的方法是手动更改它(我在使用styled-components):

import styled from 'styled-components';
import { Layout } from 'antd';

const { Content } = Layout;

export const StyledContent = styled(Content)`
  margin: 5px;
  background: ${({ theme }) => theme.colors.white};
`;
英文:

i'm trying to make Content background color change to white with ConfigProvider, so i don't have to change background color in every component. I found solution for Header and Sider, but Content is giving me a little problems. Is there any magic solution for that?

Here's ConfigProvider in main index file.

&lt;ConfigProvider
    theme={{
      components: {
        Layout: {
          colorBgHeader: &#39;#fff&#39;
        }
      }
    }}
  &gt;
    &lt;App /&gt;
  &lt;/ConfigProvider&gt;

The only way that works for me to change background in Component is to change it manually (i'm using styled-components)

import styled from &#39;styled-components&#39;
import { Layout } from &#39;antd&#39;;

const { Content } = Layout;

export const StyledContent = styled(Content)`
  margin: 5px;
  background: ${({ theme }) =&gt; theme.colors.white};
`;

答案1

得分: 1

第一种方法

您可以添加className,并在您的scss文件中全局定义它,然后您可以进行更改。

第二种方法

您可以为header定义背景颜色,并为body定义背景颜色,它将自动应用于content

<ConfigProvider
  theme={{
    components: {
      Layout: {
        colorBgHeader: "black",
        colorBgBody: "skyblue"
      }
    }
  }}
>

Codesandbox: https://codesandbox.io/s/mutable-browser-w1tt72?file=/src/App.js

英文:

There is two way to do that

1st way

You can add className and globally define that into your scss file and you can change it.

2nd way

You can define the background color for header and define the background color for body and it will automatically apply into content.

&lt;ConfigProvider
  theme={{
    components: {
      Layout: {
        colorBgHeader: &quot;black&quot;,
        colorBgBody: &quot;skyblue&quot;
      }
    }
  }}
&gt;

Codesandbox: https://codesandbox.io/s/mutable-browser-w1tt72?file=/src/App.js

huangapple
  • 本文由 发表于 2023年2月27日 04:51:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574937.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定