Ant Design StyleAnt Design Style
Quick Start
Best Practices
API Reference
Release Notes
⌘ K

Table of Contents

Customization based on ConfigProvider
Basic Overrides
Increasing Specificity for Overrides
Overrides for Multiple classNames Scenarios
Related Discussions
Best Practices and Case Studies
Writing Style
CSS Modules Global Style Override Migration
Writing Linked Styles for Parent-Child Components
Theme Customization
Customizing Ant Design Component Styles
Extending Custom Token Type Definition
Failure of antd Static Methods to Respond to Themes
Theme Switching
Flashing Issue in Dark Mode
UI Library Development
🚧 Styling Best Practices for Customizing Components Based on antd v5
Style Cases
Clay Style UI
Custom Component —— macOS Selector
Style Components
PREV
Writing Linked Styles for Parent-Child Components
NEXT
Extending Custom Token Type Definition

相关资源

Ant Design
Ant Design Pro
Ant Design Pro Components
Umi-React 应用开发框架
Dumi-组件/文档研发工具
qiankun-微前端框架

社区

Medium
Twitter
yuqueAnt Design 语雀专栏
Ant Design 知乎专栏
体验科技专栏
seeconfSEE Conf-SEE Conf-蚂蚁体验科技大会

帮助

GitHub
更新日志
讨论

more products更多产品

yuque语雀-知识创作与分享工具
AntVAntV-数据可视化解决方案
EggEgg-企业级 Node.js 框架
kitchenKitchen-Sketch 工具集
xtech蚂蚁体验科技
Copyright © 2022-2024
Made with ❤️ by 蚂蚁集团 - AFX & 数字科技
‌
‌
‌
‌

How to elegantly override Ant Design component styles?

Customization based on ConfigProvider

In V5, Ant Design provides a brand new theme property for customization. Therefore, if you need to customize component styles, it is recommended to use the theme field on the ConfigProvider as a priority.

The demo example is as follows:

INFO

For more customization capabilities based on ConfigProvider, please refer to Discussing the Theme of Ant Design V5 (Part 1): Creative Use of CSSinJS Dynamic Themes.

The ThemeProvider of antd-style is a business-layer encapsulation based on ConfigProvider, providing user-friendly customization capabilities. See: Custom Theme

Basic Overrides

The createStyles method has a prefixCls parameter. By using this parameter, you can pass in the prefix of the component, so that any style overrides can automatically change with the change of prefixCls.

Increasing Specificity for Overrides

In some components, directly adding a class name may not have enough specificity to override the styles. In this case, you can use the & symbol to increase the specificity accordingly.

Overrides for Multiple classNames Scenarios

classNames is a major feature of antd V5: [RFC] Semantic DOM Structure for all Components. In the past, when defining styles, we needed to find many DOM nodes for extensive style overrides. However, during the upgrade process of antd, sometimes the DOM structure may be adjusted. As a result, our overridden styles may encounter issues.

With classNames, it will provide us with a stable DOM structure API. We can pass in the class names through classNames, which will point to the corresponding DOM nodes, thereby greatly reducing the risk of Breaking Changes caused by DOM changes. It also eliminates the need for us to hackishly search for style class names.

Related Discussions

  • Style Specificity Issues
index.tsx
tsx
index.tsx
tsx
index.tsx
tsx
无法覆盖
可正常覆盖
$