Scope Ant Design v5 theming with ConfigProvider design tokens
Use antd v5 ConfigProvider theme.token to scope color, radius, and text customization without global Less overrides.
26 Jul 2025, 03:53 UTC

The problem: global Less variables no longer drive antd v5
If you upgraded an Ant Design for React (antd) application to version 5 and tried to change the brand color by editing @primary-color in a Less file, you may have noticed nothing happens. antd v5 ships with CSS-in-JS enabled by default and uses design tokens instead of Less variables as its primary customization surface. Editing legacy Less variables is not the supported path for v5 components.
The useful takeaway: wrap your application, or a subtree, in ConfigProvider and pass theme.token values such as colorPrimary, borderRadius, or colorText. Those tokens flow to descendant components through antd's useToken hook, so a single provider can retarget many components at once without touching global stylesheets.
Check your antd version first
Examples here assume antd v5. Ant Design Vue and antd v4 use different theming mechanisms, and token names can change between major releases.
Run this in your project root:
npm list antd
# or
yarn list --pattern antd
Look for 5.x.x. If you see 4.x, stop: the v5 token examples below will not apply. No special permissions are required; this is a read-only dependency check. Risk: applying v5 configuration to v4 can silently ignore tokens or produce unexpected styles because v4 still relies on Less variables by default.
Use ConfigProvider for scoped token theming
ConfigProvider makes theme available to every antd component inside its subtree. Place it near your app root so tokens are set before components mount, which helps avoid a flash of restyled components.
import { ConfigProvider, Button } from 'antd';
import React from 'react';
const appTheme = {
token: {
colorPrimary: '#0066cc',
borderRadius: 8,
colorText: '#1a1a1a',
},
};
export default function App() {
return (
<ConfigProvider theme={appTheme}>
<Button type="primary">Primary button</Button>
</ConfigProvider>
);
}
Place this in your application entry file, typically src/App.js or src/main.jsx. After saving, inspect a rendered Button with your browser's developer tools. The button's computed color should reflect colorPrimary. If it does not, confirm the component is actually inside the ConfigProvider subtree and that your installed antd version is v5.
Component-level overrides when one component needs different styling
Global tokens are convenient, but sometimes only one component type should change. Use theme.components under the same ConfigProvider:
const appTheme = {
token: {
colorPrimary: '#0066cc',
},
components: {
Button: {
colorPrimary: '#cc6600',
},
},
};
In this setup, Button uses #cc6600 while other components inherit #0066cc from theme.token. Component-level overrides apply only to that provider's subtree, so you can nest ConfigProvider instances to give different sections of the page different themes. Nested provider merge behavior can vary by antd release, so verify whether a child provider replaces or augments parent tokens for your installed version.
Avoid creating a new theme object on every render
antd memoizes theme objects to avoid regenerating CSS-in-JS styles. If you define the theme inline inside a component, a new object is created on every render, which can force antd to regenerate styles for the entire provider subtree:
// Avoid: new object every render
<ConfigProvider theme={{ token: { colorPrimary: '#0066cc' } }}>
Define the theme outside the component or memoize it with useMemo so the reference stays stable across renders.
Limitation: runtime CSS-in-JS and strict CSP
antd v5 injects component styles at runtime by default. If your site uses a strict Content Security Policy that blocks inline style elements, runtime injection can be violated. For production builds with CSP, use @ant-design/cssinjs with your bundler to extract component styles into static CSS files instead of inline style elements. To check, run your production build, for example npm run build, and inspect the emitted CSS assets for antd component classes. In development, styles may still appear as inline elements even when production extraction is configured.
How to verify the token took effect
- Confirm antd v5 in package.json or the lockfile.
- Render a Button inside ConfigProvider with theme.token.colorPrimary set.
- Open browser developer tools, inspect the Button, and check its computed background color or generated class.
- Compare theme.token.colorPrimary against theme.components.Button.colorPrimary on the same Button to confirm which override wins in your setup.
If the color does not change, the component may be rendered outside the provider, or another stylesheet may be overriding antd's generated classes with higher specificity.
Closing
For antd v5, treat ConfigProvider theme.token as the default customization surface. Reserve Less for cases where you are deliberately maintaining a v4-style setup or integrating Less-based build tooling. Set the theme early at app root, keep the theme object referentially stable, and verify in production if you use CSP or CSS extraction. To revert, remove the ConfigProvider wrapper or delete the theme prop; no build state persists beyond your source files.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.