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

Table of Contents

CreateStylesUtils
css
cx
responsive
token
appearance
isDarkMode
prefixCls
ClassNameGeneratorOption
hashPriority
label
Creating Styles
createStyles
🚧 createStylish
createGlobalStyle
Container Components
ThemeProvider
StyleProvider
Hooks
useTheme
useThemeMode
useResponsive
Advanced Settings
🚧 setupStyled
createInstance

createStyles

Create scoped styles
引入方法
js
import { createStyles } from 'antd-style';
NPM
UNPKG
BundlePhobia
PackagePhobia
Anvaka Graph
编辑文档
NEXT
🚧 createStylish

相关资源

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 & 数字科技
‌
‌
‌
‌
Default

This is the most recommended usage. It can be used to write application styles or override basic component styles.

The createStyles function can be used to create scoped styles. In terms of writing ability and developer experience (DX), it is comparable to CSS Modules. It is also more convenient and powerful for dynamic theme writing. For the basic usage of createStyles, please refer to Quick Start - Style Creation. This section will focus on the API of createStyles.

The createStyles function can take a function as an argument, and the signature of this function is as follows:

ts

The functionality of each property is detailed below.

CreateStylesUtils

The first parameter utils used when writing styles provides a series of auxiliary objects and methods to facilitate efficient style writing. Its type is CreateStylesUtils, and the properties are listed in the table below:

PropertyTypeDescription
cssCssUtilCSS serialization function
cxClassNamesUtilCSS class name utility function
responsiveResponsiveUtilResponsive media query utility function
tokenFullTokenIncludes antd tokens and all custom tokens
appearanceThemeAppearanceCurrent theme mode under the ThemeProvider
isDarkModebooleanA syntactic sugar that can be used to reduce the cost of appearance judgment
prefixClsstringThe prefix marked on the ThemeProvider, which can be used to flexibly respond to component prefixes

css

Type: CssUtil

typescript

The CSS serialization function is the core API in createStyles. This method is based on the encapsulation of emotion/css, and we have enhanced many capabilities, such as supporting cascading of multiple CSS objects (after v11, @emotion/css no longer supports cascading, see related issue), and supporting the :where selector.

This serialization function supports both CSS Object and CSS String. The CSS Object syntax can provide TypeScript type hints by default, while the CSS String syntax needs to be combined with related plugins to provide hinting capabilities.

Note

Unlike the css method in @emotion/css, the type of the result of this method is SerializedStyles, which cannot be directly applied to a className. We have added a layer of conversion in createStyles, and the final styles.xxx obtained is a className string.

Common issues and discussions:

  • Does the css solution support variable nesting?
  • Points to note when used with container

cx

Type: ClassNamesUtil

typescript

It is basically equivalent to the cx in emotion/css (related link), and is used in 95% of the same scenarios. It is only necessary to pay attention when using it with the container of StyleProvider (related discussion), which is not encountered in general situations.

responsive

Type: ResponsiveUtil

typescript

This utility function provides a convenient way to create responsive media queries. An example is as follows:

ts

The breakpoints of this utility function follow the PC-First principle, using max-width query logic for xs to xl, and min-width query logic for xxl.

BreakpointCalculation Logic
xs@media (max-width: ${token.screenXSMax}px)
sm@media (max-width: ${token.screenSMMax}px)
md@media (max-width: ${token.screenMDMax}px)
lg@media (max-width: ${token.screenLGMax}px)
xl@media (max-width: ${token.screenXLMax}px)
xxl@media (min-width: ${token.screenXXLMin}px)
mobileAlias for xs
tabletAlias for md
laptopAlias for lg
desktopAlias for xxl

token

Type: FullToken

ts

It includes antd tokens and custom tokens on the ThemeProvider. There are many related usages in this document, so they will not be repeated here.

By extending the CustomToken type, you can get type hints for custom tokens.

ts

appearance

Type: 'dark' | 'light' | string

The theme mode under the outermost ThemeProvider.

isDarkMode

Type: boolean

A syntactic sugar for judging light and dark theme modes, which is equivalent to appearance === 'dark'. Using isDarkMode directly can reduce the cost of appearance judgment.

prefixCls

Type: string

The prefix marked on the ThemeProvider. By using this parameter, you can flexibly override the antd prefix.

ts

The above style code can accurately override regardless of the value of the prefixCls wrapped by the outer ThemeProvider.

ClassNameGeneratorOption

The second parameter of createStyles can be used to control the generated className.

PropertyTypeDescription
hashPriorityHashPriorityWeight of the generated hash className styles
labelstringAdd a suffix

hashPriority

Type: 'low' | 'high'

Controls the weight of the generated className, defaulting to high.

If set to low, the generated hash style selector will be wrapped with the :where selector to reduce its weight. Generally, this can be used in component library scenarios, but it is not recommended for other scenarios.

label

Type: string

Similar to the label in emotion. After adding a label, it will be added to the suffix. Generally, it is not necessary to use.

index.tsx
tsx
响应式断点

不同断点下,背景色和文本内容不同

操作按钮
普通卡片
主要卡片
tablet
赋予 with-label 标签