Magento 2: How To Create a Theme (Pt.2)

Jun 15, 2017

18737 Denis Natalevich

Magento 2: How To Create a Theme (Pt.2)

In the first part of  “How To Create a Theme in Magento 2”  we were talking about theme declaration, registration, applying in the admin panel and Composer.json file addition. Now it’s time to continue.

Directories creation for CSS, JavaScript, images, and fonts

Let’s create a folder for theme static files. The structure should be following:


││ ├──source/

If everything relating to CSS, fonts, images and js folders is clear, there is a question about a source folder and its intended use.

Magento 2 incorporates LESS, a CSS pre-processor that simplifies the management of complex CSS files. To define styles of a Magento store, you can use both – CSS and LESS stylesheets.

 If your theme inherits from the Magento out-of-the-box Blank or Luma theme, you can override the default LESS files.

As an example, we can create a _theme.less file with basic variables override in Magento UI. In the folder /lib/web/css/source/lib/variables/ in source files we can find default values for those variables which can be overridden. The module styles can be set in a _module.less file as well as for widgets it can be performed in a _widgets.less file. For minor corrections, it is possible to create a _extend.less file. So as an example we’re going to change a _theme.less file.

Now we have the following structure:

Magento 2 How To Create a Theme (Pt.2)__1

Theme logo definition

In Magento 2 logo “logo.svg” is located in <theme_dir>/web/images by default. Logo format is SVG by default.

In order to use your own logo, you should:

  1. If your logo is .svg, you should rename as logo.svg and put in <theme_dir> /web/images
  2. If your logo has another format, it should be declared. For logo declaration it’s necessary to add a file <theme>/Magento_Theme/layout/default.xml.

In this case, we extend the layout of a parent theme. Then in the layout, the data on logo should be added.

For example, the logo logo.png which has size 70×40 will be declared as:

Images configuration

The function is considered as rather useful in Magento 2. Product images configuration such as size, proportions, cutting, background color is described in the file <theme>/etc/view.xml. The file is obligatory, so in case it wasn’t inherited from Magento 2 default theme “luma” or “blank”, it would be necessary to copy from a default theme (values are not inherited).

Let’s consider the example. On the main page, products are displayed with the widget. Now decrease images from 240×300 to 200х200.


Partner With Us

Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey

Magento 2 How To Create a Theme (Pt.2)__3

new_products_content_widget_grid is unique within theme. Type small_image coincides with Small Image Role in admin part. Permitted values are image, small_image, swatch_image, swatch_thumb and thumbnail.

Magento 2 How To Create a Theme (Pt.2)__4

After saving an item, all images are cached. That’s why after size alteration a command php bin/magento catalog:images:resize can be executed for images regeneration.

As a result, we have the same “Luma” but with another logo and image size.

Magento 2 How To Create a Theme (Pt.2)__5

“Less” usage for changing styles

After a theme has been created, but before starting to change the styles, you should decide which LESS compilation mode should be used. There are two modes available in Magento:

– server-side compilation mode;

– client-side (recommended for theme development).

Client compilation is used during development mode, as all changes performed can be seen in a moment. A browser will compile every time when calling styles file. If it’s server compilation, you should remove folder contents pub/static and var/view_preprocessed manually. These actions can be optimized with Grunt. It tracks any changes in files, removes definite folders and compiles LESS automatically.

The compilation type can be changed in admin panel: Stores > Configuration > Advanced > Developer > Frontend Development Workflow > Workflow type:

Magento 2 How To Create a Theme (Pt.2)__6

Let’s apply server compilation which is considered as default. We’re going to set background color as well as fonts in the file _theme.less.

Since we’ve applied server-side compilation mode, after changes we need to remove directories contents /pub/static/frontend/Belvg/air/en_US и var/view_preprocessed.

Let’s observe the changes:

Magento 2 How To Create a Theme (Pt.2)__7

As we can see, the template structure in Magento 2 pretty differs from Magento 1.x. The structure has been improved, so now it’s much more logical, all the theme files are structured, which is very good for search.

Magento 2: How To Create a Theme (Pt.1)
Magento 2: How To Create a Theme (Pt.2)
Magento 2: How To Create a Theme (Pt.3)

Andrey Dubina

Partner With Us

Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Andrey

Post a new comment

BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *