Customize theme styles
Let's say you created a new theme inheriting from Blank or Luma, and chose the Less compilation mode. What's next? Where to add the style changes? This topic gives quick answers.
Extend parent styles
To extend the parent theme's styles in your theme:
In your theme directory, create a
web/css/sourcesub-directory.Create a
_extend.lessfile there. The path to it looks like following:Copied to your clipboard<theme_dir>/│ ├── web/│ │ ├── css/│ │ │ ├── source/│ │ │ ├──_extend.less...Add your Less code in this file.
However, the above only works if the theme's parent is a Blank. Consider a Theme A which is the child of Blank. Theme A has two children, B and C. A global style rule is added to the _extend.less file of theme A. This extends its parent Blank. Theme B and C also have their own _extend.less files. Theme B and C will override the parent (Theme A), rather than extending it further. Theme B & Theme C are extending their grandparent (Blank) and overriding their parent (Theme A) in this setup.
In case of subsequent descendants of the child theme, you can avoid this behavior by following these steps:
Create a
_extend-child.lessin both your parent and child themes.Keep
_extend-child.lessempty in your parent theme and add it too your parent theme's_extend.lessfile.Add a
@import '_extend-child.less'rule to the end of your parent's theme's_extend.lessfile.In your child theme, add
@importor style rules in_extend-child.lessto extend parent theme's CSS.Copied to your clipboardapp/design/frontend/Vendor/├── parent│ └── web│ └── css│ └── source│ ├── _extend-child.less (keep this file empty)│ └── _extend.less└── child└── web└── css└── source└── _extend-child.less...
Extending a theme using _extend.less is the simplest option when you are happy with everything the parent theme has, but want to add more styles.
The rules and variables declared in _extend.less always have precedence over ones declared in _theme.less.
Override parent styles
To override parent styles (that is, override default UI library variables):
In your theme directory, create a
web/css/sourcesub-directory.Create a
_theme.lessfile here. The path to it then looks like following:Copied to your clipboard<theme_dir>/│ ├── web/│ │ ├── css/│ │ │ ├── source/│ │ │ ├──_theme.less...It is important to remember that your
_theme.lessoverrides the parent_theme.less.Copy all variables you need from the parent
_theme.less, including those which will not be changed. For example, if your theme inherits from Blank, the_theme.lessyou should copy from is located at<Magento_Blank_theme_dir>/web/css/source/_theme.less.Make the necessary changes.
The drawback of this approach is that you need to monitor and manually update your files whenever the parent's _theme.less is updated.
Add structured changes
To make your changes easier to read and support, structure them by adding a separate overriding or extending .less files for each UI library component you change. Let's use the button component implemented in _button.less as an illustration.
Extend component styles
In your theme directory, create a
web/css/sourcesub-directory.Add
_buttons_extend.lessand_extend.lesshere. The path to the files looks like following:Copied to your clipboard<theme_dir>│ ├── web/│ │ ├── css/│ │ │ ├── source/│ │ │ ├──_buttons_extend.less│ │ │ ├──_extend.less...In
_buttons_extend.lessadd your styles for the button component.In
_extend.lessregister the_buttons_extend.lessby adding the following code:@import '_buttons_extend.less';
Override component styles
To override the parent theme's styles for buttons in your theme:
In your theme directory, create a
web/css/sourcesub-directory.Create a
_buttons.lessfile here. The path to it looks like following:Copied to your clipboard<theme_dir>/│ ├── web/│ │ ├── css/│ │ │ ├── source/│ │ │ ├──_buttons.less...This file overrides the
_buttons.lessof the parent theme.Add your styles for the button component. If the file is left blank, then no styles are applied for the component.
Extend module styles
To extend a Module's styles in your theme:
In your theme directory, create a
Module_Name/web/css/sourcedirectory.Create an
_extend.lessfile in the theme directory. For example:Copied to your clipboard<theme_dir>/├── <module_dir>/│ ├── web/│ │ ├── css/│ │ │ ├── source/├──_extend.less...Add additional styles in the
_extend.lessfile.
For example, to extend the Magento_Review module's style, the directory path should be <your_theme_dir>/Magento_Review/web/css/source/_extend.less.
Override module styles
To override module styles in your theme:
In your theme directory, create a
Module_Name/web/css/sourcedirectory.Create a
_module.lessfile in the theme directory. For example:Copied to your clipboard<theme_dir>/├── <module_dir>/│ ├── web/│ │ ├── css/│ │ │ ├── source/| | | ├──_module.less...This file overrides the
_module.lessfile of the specific module.Add your styles in the
_module.lessfile.
For example, to override the Magento_Review module's style, the directory path should be <your_theme_dir>/Magento_Review/web/css/source/_module.less.