Ant Design is a very popular UI library for React. It provides a well designed and eminently composable collection of components.
However, it is very opinionated about styles. Its philosophy is not only to provide tools, but a whole conceptual approach for designing UIs. Depending on your needs and inclinations, this can be a massive pro, or a very irritating con.
Thankfully, Ant Design also provides a way to theme the experience they offer. This page in Ant Design’s documentation provides guidelines for making its components look more in line with a user’s brand identity. It is serviceable, but the operative word here is “guideline”: It only superficially lists approaches you can employ to make it happen, there is no actual example of it being implemented in a real app, and the required configuration is only lightly touched upon.
Get the most out of your reward programs
This article aims to complement the docs by providing a step by step guide to theming Ant Design with their recommended approach. Possible pain points will be signalled with a ⚠️gotcha alert⚠️, and for each step, I will point to a specific commit of the repo I created for this article.
It is based on a barebone project starter I made for testing and experimentation, but any project built with Webpack and Babel where you have access to the config files will behave similarly. Theming an unejected create-react-app project is different and arguably better documented.
If you just want to see the code, head over there now : https://github.com/mathieu-anderson/antd-theming-examples
The purpose of babel-plugin-import is to allow modular import of Ant Design components, so that writing this…
… only imports the Select component, not the whole Ant Design library. But more importantly, the styles option of this plugin adds the possibility to also import the styles of the given component. This allows us to never have to import styles directly from antd (as it's done in the component’s demo Codesandbox provided by Ant Design’s docs with import "antd/dist/antd.css";).
This gives us an application styled with the default Ant Design theme :
Ant Design styles rely on the CSS post-processor less. It's made clear that the correct way to theme Ant Design is to override the default less variables. We therefore need to be able to parse those variables, and use them in our project.
Nothing has changed in the looks of our app, but all the preliminary preparations are now done. We can start theming our Ant Design components!
Now that the configuration is correct, we can follow the official docs more easily. The first solution is to write inline configuration for our theme, in a “plain object” kind of syntax.
This override happens during the compile time, and finally we see tangible results!
This naive approach to theming might not be the most practical, though. You might want the native IDE support you get when writing actual CSS/LESS. You also might want to separate your configs better, and have something like a theme file.
This is a much more robust solution for theming, and gives the same result as the previous one (with different colours and fonts).
You'll probably have noticed that everything is styled according to the theme we specified, not just the Ant Design components. ⚠️gotcha alert⚠️ This is because the default behaviour of Ant Design is to ship a host of global styles whenever you import a component’s style. Another instance of Ant Design being very opinionated.
There are ways to avoid this, but they deserve their own article. In the meantime, you can directly overwrite the Ant Design styles and theme by writing your own CSS for the components you want to have control over.
These rules let us regain control over the fonts and the links styles:
So, I hope this guide helps anyone having difficulties implementing the theming recommendations from Ant Design’s official docs.
If you have other approaches to this task, please get in touch and share them with us!
Get expert insights & news on the latest promotion trends in our monthly newsletter.
CEO & Founder