CSS: Everything you need to know in Magento 2

How-to-Regenerate-CSS-File-in-pubstatic-Folder-in-Magento-2

CSS stands for Cascading Style Sheets

CSS describes how HTML elements are to be displayed

What is CSS in Magento 2?

The major benefit of CSS is creating a uniform look of several or even all pages of the website. Instead of manually defining styles for each element, a developer specifies all the visual configurations in a single CSS document. It allows saving a great amount of time and effort. Moreover, any alterations in the style are automatically applied to all the pages it was utilized for. Besides, CSS is compatible with multiple devices and browsers, which means no sophisticated optimization is required.

CSS

Due to all these benefits, CSS have become an important aspect of Magento frontend development. If you strive to be a good Magento developer, CSS knowledge and skills are a must.

Where are CSS files located in Magento 2?

After the compilation, all the resulting CSS files will be located at the following path:

pub/static/frontend/<Vendor_Name>/<Theme_name>/<locale>

where

  • <Vendor_Name> – name of the vendor folder that contains your theme,
  • <Theme_name> – name of your theme,
  • <locale> – name of the current site location (for instance, en_US).

These files are formed based on the LESS and CSS original files, located in various places in Magento. In order to fully understand this system, let’s have a look at the core Magento style files:

The style files have the following structure:

  • /<Namespace>_<Module>/web/css is the location of style files for different modules;
  • /web/css is the location of core style files.

The list of the core theme files, located in the /web/css theme folder:

  • styles-m.less contains the core website and mobile devices styles. This file contains other files, for instance, _styles.less.
  • styles-l.less  contains styles for desktop devices. It also contains other files, for example, _styles.less.
  • _styles.less is a compound file that contains other style files. According to Magento file naming standards, the underscore symbol (“_”) signifies that the file is not used separately, but is a part of other files.
  • /source is the folder that contains configuration files that call Magento UI library mixins.
  • /source/_theme.less is the file that contains new values for standard variables.
  • print.less – styles for the printed website version.
  • /source/_variables.less is the file where the user variables are commonly placed.

Apart from core style files, you can connect additional files to your theme. As a rule, they will be located in your theme folder at the /web/css path.

How-To-Add-CSS-Javascript-Files-To-CMS-Pages-In-Magento-2

If you want to change style files, we recommend doing it in the source files. We also strongly discourage you from changing the resulting files. The changes will be overridden in case of the further compilation.

How to add custom CSS files in Magento 2

Where to change styles in Magento 2? You can change them either in the added LESS or CSS files or in the Magento module files. If you want to add your own styles for a specific module, you can use 2 files in the module folder in your theme:

 _module.less contains the basic styles for the module in which it is located. As a rule, we create this file in the theme when we want to change the styles of this module significantly.

_extend.less contains additional styles for the module in which it is located. As a rule, we create this file to add minor changes to the module or styles for new elements that have not been stylized in the current module before. If we don’t want to rewrite existing module styles, creating such a file will be the best way.

CSS in Magento 1 vs Magento 2 Comparison

Generally, Magento 2 has an improved process of working with CSS files, compared to Magento 1. Apart from this, Magento 2 has not only standard CSS files, but also the following items:

  • built-in LESS compiler allows you to work with LESS files by compiling these files into final CSS files. Using LESS greatly simplifies and speeds up the process of creating styles for a site since we get additional LESS functionality (in short, these are variables, mixins, nesting code formatting and others).
  • UI library uses the LESS preprocessor and consists of LESS files, which use sets of mixins and variables to style all the main site elements (such as buttons, forms, navigation and others). You can find a full list of elements available for styling here. The UI library allows you to significantly simplify the process of site styling. For example, you can change several UI library variables and get a significantly transformed website.

Another difference is the location of the CSS theme files. In Magento 1, they were situated at the following path:

/ skin / frontend / <Vendor_Name> / <Theme_name> / css.

While theme style files in Magento 2 are more structured and divided into common style files (<Theme_folder> / web / css) and module style files (<Theme_folder> / <Namespace> _ <Module> / web / css), to which they apply.

Wrapping it up

We hope that you got a comprehensive guide of CSS in Magento 2 and understand how to use it in your website. Moreover, we tried  to share all our CSS knowledge and explain how to apply it in Magento. If you have any questions or comments, feel free to leave them down below.

Level up your website with ArrowHiTech – official partner of Magento

Our own Magento development support services is plus the option for monthly support packages. It is to make sure your online business always remains in top form. And of course, there are countless other developer websites that can be easily found and perused as well.

Capture

ArrowHiTech Development Agency

Our Magento 2 Products:

Capture

Magento 2 Theme collection on themeforest.net

Capture

Some extensions of Magento 2 extensions collection on magesolution.com

354 Total Views 4 Views Today