Styling and Layout
Traditional CSS
If you're using @docusaurus/preset-classic
, you can create your own CSS files (e.g. /src/css/custom.css
) and import them globally by passing it as an option into the preset.
Any CSS you write within that file will be available globally and can be referenced directly using string literals. This is the most traditional approach to writing CSS and is fine for small websites that do not have much customization.
Styling your site with Infima
@docusaurus/preset-classic
uses Infima as the underlying styling framework. Infima provides flexible layout and common UI components styling suitable for content-centric websites (blogs, documentation, landing pages). For more details, check out the Infima website.
When you init
your Docusaurus 2 project, the website will be generated with basic Infima stylesheets and default styling. You may customize the styling by editing the src/css/custom.css
file.
Infima uses 7 shades of each color. We recommend using ColorBox to find the different shades of colors for your chosen primary color.
Alternatively, use the following tool to generate the different shades for your website and copy the variables into src/css/custom.css
.
CSS Variable Name | Hex | Adjustment |
---|---|---|
--ifm-color-primary-lightest | #80aaef | |
--ifm-color-primary-lighter | #5a91ea | |
--ifm-color-primary-light | #4e89e8 | |
--ifm-color-primary | #3578e5 | 0 |
--ifm-color-primary-dark | #1d68e1 | |
--ifm-color-primary-darker | #1b62d4 | |
--ifm-color-primary-darkest | #1751af |
Replace the variables in src/css/custom.css
with these new variables.
Styling approaches
A Docusaurus site is a single-page React application. You can style it the way you style React apps.
There are a few approaches/frameworks which will work, depending on your preferences and the type of website you are trying to build. Websites that are highly interactive and behave more like web apps will benefit from a more modern styling approaches that co-locate styles with the components. Component styling can also be particularly useful when you wish to customize or swizzle a component.
Global styles
This is the most traditional way of styling that most developers (including non-front end developers) would be familiar with.
Assuming you are using @docusaurus/preset-classic
and /src/css/custom.css
was passed in to the preset config, styles inside that file would be available globally.
CSS modules
To style your components using CSS Modules, name your stylesheet files with the .module.css
suffix (e.g. welcome.module.css
). webpack will load such CSS files as CSS modules and you have to reference the class names from the imported CSS module (as opposed to using plain strings). This is similar to the convention used in Create React App.
The class names which will be processed by webpack into a globally unique class name during build.
CSS-in-JS
caution
This section is a work in progress. Welcoming PRs.
Sass/SCSS
To use Sass/SCSS as your CSS preprocessor, install the unofficial Docusaurus 2 plugin docusaurus-plugin-sass
. This plugin works for both global styles and the CSS modules approach:
- Install
docusaurus-plugin-sass
:
- npm
- Yarn
- Include the plugin in your
docusaurus.config.js
file:
- Write and import your stylesheets in Sass/SCSS as normal.
Global styles using Sass/SCSS
You can now set the customCss
property of @docusaurus/preset-classic
to point to your Sass/SCSS file:
Modules using Sass/SCSS
Name your stylesheet files with the .module.scss
suffix (e.g. welcome.module.scss
) instead of .css
. Webpack will use sass-loader
to preprocess your stylesheets and load them as CSS modules.