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

Table of Contents

What is CSS in JS
Common Writing Styles
Method 1: styled
Method 2: css with className
Method 3: Using css props in React
Engineering Support
VS Code
Webstorm
Ant Design Token VSCode Plugin
Basic Knowledge
Introduction
Quick Start to CSS in JS
Comparison of CSS in JS Writing Methods
Design Philosophy and Implementation Strategy
Quick Start
Writing Style
Theme Switching
Custom Theme
Advanced Usage
Babel Plugin
SSR Integration
Component Development
Integrated styled
🚧 Stylish Composite Styles
Performance Comparison of CSS-in-JS Libraries
Migrating from Less
Less Application Automated Migration
Manual Migration of Less Applications
Migrating Less Components
🚧 Compilation Differences Between CSS-in-JS and Less
PREV
Introduction
NEXT
Comparison of CSS in JS Writing Methods

相关资源

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

Quick Start to CSS in JS

What is CSS in JS

In concept, CSS in JS is similar to JSX. Simply put, it means writing CSS-related styles in a JS environment. By leveraging the dynamism of JS itself, it can achieve very flexible styling capabilities. This article will not provide basic introductions. If you lack prior knowledge of CSS in JS, you can read some related articles in the community:

  • Embracing CSS in JS
  • CSS-in-JS: A Controversial Technical Solution
  • In-depth Analysis of CSS-in-JS

If there are better learning materials, feel free to submit a PR.

Common Writing Styles

Let's take a look at the four common ways of writing styles in the world of CSS in JS. We will not evaluate these writing styles, but by understanding some mainstream solutions, you will have a better sense for the subsequent introductions.

Method 1: styled

styled was pioneered by styled-components, and the syntax is as follows:

tsx

Subsequently, emotion also followed suit with @emotion/styled, so most mainstream CSS in JS libraries support this syntax. We have tested that switching between styled-component and @emotion/react using the styled syntax has no impact on the styles, and the performance difference is negligible.

However, it is worth noting that some syntax supported by styled-components (e.g., component selectors) require configuration of a Babel plugin in @emotion/react to be supported. Therefore, the package size of styled-components is significantly larger compared to @emotion/react.

Method 2: css with className

The second usage method was proposed by emotion, with the core being the css method.

tsx

It is worth mentioning that the css object not only supports CSS strings, but also supports the object-oriented approach to writing CSS.

ts

If you want to achieve the same dynamic effect as styled, you need to place the css within a function to execute and achieve dynamism.

tsx

Method 3: Using css props in React

In @emotion/react, an additional method is provided for using css props in React. This is the third method and is also the recommended approach by emotion for use in React.

tsx

In addition, there are many other creative approaches in other CSS in JS libraries, such as stitches. However, due to space limitations, they will not be listed one by one.

By now, you should have a basic understanding of how to write CSS in JS. The next article will provide a detailed analysis of the strengths and weaknesses of various writing styles, and offer a selection of antd-style.

Engineering Support

After four or five years of development, most IDEs now have good support for CSS in JS. Therefore, when writing, the experience is basically the same as writing regular CSS.

VS Code

For VS Code users, you need to install the vscode-styled-components plugin, which provides automatic prompts. This plugin works for both styled-component and emotion.

Webstorm

Webstorm provides default support for CSS in JS. If you are a Webstorm user, you don't need to do anything; all intelligent prompts and code completions for CSS in JS are ready to use out of the box.

Ant Design Token VSCode Plugin

Community member shezhangzhang has developed a VSCode plugin for the Ant Design V5 Token system, which provides token prompts directly in VSCode.

Project link: shezhangzhang/antd-design-token