Extending Parent Styles in Custom Theme using LESS

Extending Parent Styles in Custom Theme using LESS

Simplest way to extend parent styles in Custom Theme using Less

To extend the parent theme’s styles in your theme:

In your theme directory, create a web/css/source sub-directory.

  • Create a _extend.less file there. The path to it looks like following:

  • Add your LESS code in this file.

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.

Simplest way to override parent styles in Custom Theme using Less

To override parent styles (that is, override default Magento UI library variables):

  • In your theme directory, create a web/css/source sub-directory.
  • Create a _theme.less file here. The path to it then looks like following:

  • 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.less you should copy from is located at:

  • 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.

Less Compilation with Grunt

Prerequisites

Make sure that you set your Magento application to the developer or default mode.

Installing and configuring Grunt

Magento has built-in Grunt tasks configured, but there are still several prerequisite steps you need to take to be able to use it:

  • Install node.js to any location on your machine. Refer: https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions
  • Install Grunt CLI tool globally. To do this, run the following command in a command prompt:  npm install -g grunt-cli
  • Now Rename the following files:

  • Install (or refresh) the node.js project dependency, including Grunt, for your Magento instance. To do this, run the following commands in a command prompt:

  • Add your theme to Grunt configuration. To do this, in the dev/tools/grunt/configs/themes.js file, add your theme to module.exports like following:

Where the following notation is used:

<theme>: your theme code, conventionally should correspond to the theme directory name.

<language>: specified in the ‘code_subtag’ format, for example en_US. Only one locale can be specified here. To debug the theme with another locale, create one more theme declaration, having specified another value for language

<path_to_file>: path to the root source file, relative to the app/design/frontend/<Vendor>/<theme/>web directory. You need to specify all root source files of the theme. If your theme inherits from a certain theme, and does not contain its own root source files, specify the root source files of the parent theme.

  • (Optional) If you want to use Grunt for “watching” changes automatically, without reloading pages in a browser each time, install the LiveReload extension in your browser.

Grunt Commands:

Command Usage
grunt clean:<theme>
For example:
grunt clean:blank
It removes the theme related static files in the pub/static and var directories.
grunt exec:<theme> It republishes symlinks to the source files to the pub/static/frontend/<Vendor>/<theme>/<locale> directory.
grunt less:<theme> Compiles .css files using the symlinks published in the pub/static/frontend/<Vendor>/<theme>/<locale> directory
grunt watch Tracks the changes in the source files, recompiles .css files, and reloads the page in the browser pages (you need to have LiveReload installed for you browser)

Basically, you need to use grunt less:<theme> which would compile less.

Source: http://devdocs.magento.com/guides/v2.0/frontend-dev-guide/css-topics/css_debug.html