LESS coding standard
This standard defines internal requirements for code formatting and style for teams that develop LESS and CSS code.
Some parts of the code might not comply with this coding standard yet, but we are working to gradually improve this.
This coding standard is optional for third-party developers.
General rules#
Indentation#
Use only spaces for indentation:
- Tab size: 4 spaces
- Indent size: 4 spaces
- Continuation indent: 4 spaces
Correct:
Copied to your clipboard1.nav {2 .nav-item {3 ...4 }5}
Formatting#
Braces#
Add one space before opening braces and a line break after. Add a line break before closing braces.
Correct:
Copied to your clipboard1.nav {2 color: @nav__color;3}
Incorrect:
Copied to your clipboard.nav{color: @nav__color;}
Selector delimiters#
Add a line break after each selector delimiter. Do not add spaces before or after delimiters.
Correct:
Copied to your clipboard1.nav,2.bar {3 color: @color__base;4}
Incorrect:
Copied to your clipboard1.nav, .bar {2 color: @color__base;3}
Quotes#
Use single quotes.
Correct:
Copied to your clipboard1.nav {2 content: 'lorem ipsum';3}
Incorrect:
Copied to your clipboard1.nav {2 content: "lorem ipsum";3}
Combinator indents#
Add spaces before and after combinators.
Correct:
Copied to your clipboard1.nav + .bar {2 color: @bar__color;3}
Incorrect:
Copied to your clipboard1.nav+.bar {2 color: @bar__color;3}45.nav +.bar {6 color: @bar__color;7}89.nav+ .bar {10 color: @bar__color;11}
Properties line break#
Start each property declaration in a new line.
Correct:
Copied to your clipboard1.nav {2 background-color: @nav__background-color;3 color: @nav__color;4}
Incorrect:
Copied to your clipboard1.nav {2 color: @nav__color; background-color: @nav__background-color;3}
Properties colon indents#
Add space after but not before the colon that separates property names from values.
Correct:
Copied to your clipboard1.nav {2 color: @nav__color;3}
Incorrect:
Copied to your clipboard1.nav {2 color : @nav__color;3}45.bar {6 color:@bar__color;7}89.item {10 color :@item__color;11}
End of file#
Add a blank line at the end of file.
End of selector#
Add a blank line after a selector.
Correct:
Copied to your clipboard1.nav {2 background-color: @nav__background-color;3}45.bar {6 background-color: @bar__background-color;7}
Incorrect:
Copied to your clipboard1.nav {2 background-color: @nav__background-color;3}4.bar {5 background-color: @bar__background-color;6}
End of the property line#
Add a semicolon after property.
Correct:
Copied to your clipboard1.nav {2 background-color: @nav__background-color;3}
Incorrect:
Copied to your clipboard1.nav {2 background-color: @nav__background-color3}
!important property#
Avoid using the !important
property if possible. If it is required, add a space before the property.
Correct:
Copied to your clipboard1.jquery-ui-calendar-item {2 background-color: @nav__background-color !important;3}
Incorrect:
Copied to your clipboard1.jquery-ui-calendar-item {2 background-color: @nav__background-color!important;3}
Comments#
First and second level comments must be surrounded by empty lines. First, second and third level comments should have two spaces after "//". Inline comments should have one space after "//".
Correct:
Copied to your clipboard1//2// First level comment3// _____________________________________________45.nav {6 background-color: @nav__background-color;7}89//10// Second level comment11// ---------------------------------------------1213.nav {14 background-color: @nav__background-color;15}1617// Comment18.nav {19 // New line comment20 background-color: @nav__background-color; // ToDo UI: todo inline comment21 color: @nav__color; // inline comment22}
Selectors#
Types#
Adobe Commerce and Magento Open Source support the two most recent versions of all major browsers. Internet Explorer is supported from version 9 and later.
You can use almost all CSS3 selectors: descendants, attributes, pseudo classes, structural, pseudo elements, and so on.
Exception: Avoid the id
selector.
Correct:
Copied to your clipboard1.nav {2 ...3}45.nav + bar {6 ...7}89.nav:not(.bar) {10 ...11}
Incorrect:
Copied to your clipboard1#foo {2 ...3}
Classes Naming#
Standard classes#
Class names should be lowercase, start with a letter (except helper classes), words should be separated with dash '-'.
Correct:
Copied to your clipboard1.nav-bar {2 ...3}
Incorrect:
Copied to your clipboard1.navBar {2 ...3}
Incorrect: underscore separation
Copied to your clipboard1.nav_bar {2 ...3}
Helper classes#
Helper class names should be lowercase and start with underscore ("_").
Some parts of the code might not comply with this standard yet. You might still find helper names with no underscores. We are working to gradually remove the inconsistency.
Example:
Copied to your clipboard1._active {2 ...3}
Size#
Use class names that are as short as possible, but as long as necessary. Try to convey what class is about while being as brief as possible.
Correct:
Copied to your clipboard1.nav-bar {2 ...3}
Incorrect: too long
Copied to your clipboard1.navigation-panel-in-footer {2 ...3}
Incorrect: too short
Copied to your clipboard1.nvpf {2 ...3}
Meaning#
Use meaningful, specific class names that reflect the purpose of the element. Class names should not be presentational or cryptic.
Correct: specific
Copied to your clipboard1.category {2 ...3}4.category-title {5 ...6}
Incorrect: cryptic
Copied to your clipboard1.foo-1901 {2 ...3}
Incorrect: presentational
Copied to your clipboard1.button-green {2 ...3}45.clear {6 ...7}
Selectors naming#
Type selectors#
Avoid qualifying class names with type selectors.
Unless necessary (for example with helper classes), do not use element names in conjunction with IDs or classes.
Correct:
Copied to your clipboard1.error {2 ...3}
Incorrect:
Copied to your clipboard1div.error {2 ...3}
Type selectors must be lowercase.
Correct:
Copied to your clipboard1.nav > li {2 ...3}
Incorrect:
Copied to your clipboard1.nav > LI {2 ...3}
Formatting#
Write selector in one line, do not use concatenation.
Correct:
Copied to your clipboard1.product-list-item {2 ...3}
Incorrect:
Copied to your clipboard1.product {2 ...3 &-list {4 ...5 &-item {6 ...7 }8 }9}
Nesting#
Avoid using more than three levels of nesting.
Exceptions are pseudo elements and states.
Correct:
Copied to your clipboard1.footer {2 ...3 .nav {4 ...5 }6 .nav-list {7 ...8 .nav-list-item {9 ...10 }11 }12}
Incorrect:
Copied to your clipboard1.footer {2 ...3 .nav {4 ...5 .nav-list {6 ...7 .nav-list-item {8 ...9 }10 }11 }12}
Properties#
Sorting#
Sort all properties in the alphabetical order. Mixins, variables, and so on should go first.
Correct:
Copied to your clipboard1.nav {2 background-color: @nav__background-color;3 color: @nav__color;4 text-align: center;5}
Incorrect:
Copied to your clipboard1.nav {2 color: @nav__color;3 text-align: center;4 background-color: @nav__background-color;5}
Shorthand#
Use shorthand properties where possible.
CSS offers a variety of shorthand properties that should be used whenever possible, even in cases where only one value is explicitly set.
Correct:
Copied to your clipboard1border-top: 0;2padding: 0 1em 2em;
Incorrect:
Copied to your clipboard1border-top-style: none;2padding-bottom: 2rem;3padding-left: 1rem;4padding-right: 1rem;5padding-top: 0;
0 and units#
Do not specify units "0" value.
Correct:
Copied to your clipboard1border-width: 0;2margin: 0;
Incorrect:
Copied to your clipboard1border-width: 0px;2margin: 0rem;
Floating values#
Omit leading "0"s in values, use dot instead.
Correct:
Copied to your clipboardmargin-left: .5rem;
Incorrect:
Copied to your clipboardmargin-left: 0.5rem;
Hexadecimal notation#
- Use lowercase only.
- Use three-character hexadecimal notation where possible.
- Avoid using hexadecimal values for color in properties, use only variables instead.
Correct:
Copied to your clipboard1@nav__color: #fafafa;2@nav-item__color: #f00;3...4color: @nav-item__color;
Incorrect:
Copied to your clipboard1color: #ff0000;2@nav__color: #FAFAFA;3@nav-item__color: red;
Variables#
Location#
Local variables#
If variables are local and used only in a module scope, they should be located in the module file, in the beginning of the general comment.
Example: _module.less
:
Copied to your clipboard1...23//4// Variables5// _____________________________________________67 // Colors8@btn__color: @color-brownie;9@btn-primary__color: @color-white;10@btn-secondary__color: @color-white;11...
Theme variables#
If variables are common for several modules they should be specified in the _theme.less
file.
Naming#
All variable names must be lowercase.
Value variables#
General model is the following:
Copied to your clipboard@property-name
Examples:
Copied to your clipboard1@primary__color: @color-phoenix;2@indent__base: 2rem;3@border-radius-round: 100%;
Parameter variables#
General model is the following:
Copied to your clipboard@component-element__state__property__modifier
Component name must meaningful. It can contain the primary
, secondary
, tertiary
names.
base
is a modifier.
Examples:
Copied to your clipboard1@color-orange: '';23@link__hover__color: '';45@nav-element__background-color: '';67@secondary__color: '';89@side-nav__indent__s: '';1011@side-nav-el__background-color: '';1213@side-nav-el__active__background-color: '';1415@side-nav-el__active-focus__background-color: '';1617@side-nav-el__active-focus__font-size__xl: '';1819@text__color__base: '';
Mixins#
Location#
Theme mixins (except extends) should be located in the web/css/source
directory. For more details, refer to Include CSS.
Naming#
For mixin naming apply the class naming rules.
For mixins grouping use the double underscore "__" prefix.
There are common situations when different elements use a similar set of CSS properties.
In a .css
file, properties are duplicated for each element. In a .less
file, it is done by reusing the CSS rule - using mixins.
For example, many elements on the page use the same animation. For this, create an .animation-1
class with a set of animation properties:
Example:
Copied to your clipboard1.extend__clearfix(...) {2 ...3}45.vendor-prefix__flex-direction(...) {6 ...7}
Copied to your clipboard1.animation-1 {2 transition: 300ms ease-in-out;3 -moz-transition: 300ms ease-in-out;4 -webkit-transition: 300ms ease-in-out;5 -o-transition: 300ms ease-in-out;6}
And then apply this mixin where necessary:
Copied to your clipboard1.example-1 {2 .animation-1();3 width: 100%;4}
After compiling the .less
file into a .css
file, the .example-1
element has the following:
Copied to your clipboard1.animation-1 {2 transition: 300ms ease-in-out;3 -moz-transition: 300ms ease-in-out;4 -webkit-transition: 300ms ease-in-out;5 -o-transition: 300ms ease-in-out;6}7.example-1 {8 transition: 300ms ease-in-out;9 -moz-transition: 300ms ease-in-out;10 -webkit-transition: 300ms ease-in-out;11 -o-transition: 300ms ease-in-out;12 width: 100%;13}
Mixins with parameters#
Mixins also accept parameters. When calling these mixins, these parameter values are passed in. When creating this type of mixin, always set default values, since issues may occur when calling the mixin without specifying the parameter value. The example below shows how to create a mixin with parameters based on the example above and shows how to call it:
Copied to your clipboard1.animation-1 (2 @animation-speed: 300ms,3 @animation-type: ease-in-out4) {5 transition: @animation-speed @animation-type;6 -moz-transition: @animation-speed @animation-type;7 -webkit-transition: @animation-speed @animation-type;8 -o-transition: @animation-speed @animation-type;9}10.example-1 {11 .animation-1(12 @animation-speed: 1500ms13 );14}
Mixin parameters are set in parentheses with default values.
When calling a mixin in parentheses, set the required value of the mixin parameters, if they differ from the default values.
After compiling the .less
file into a .css
file, the .example-1
element will have the following:
Copied to your clipboard1.example-1 {2 transition: 1500ms ease-in-out;3 -moz-transition: 1500ms ease-in-out;4 -webkit-transition: 1500ms ease-in-out;5 -o-transition: 1500ms ease-in-out;6}
Extends#
Location#
Local extends used only in one file, should be specified in this file.
Extends that are used in several files should be specified in the theme's source/_extend.less
file.
Naming#
Extend names should start with the .abs-
prefix.
@import
directive#
Always add the file extension of the imported resource.
Correct:
Copied to your clipboard1@import 'source/lib/_lib.less';2@import (css) 'styles.css';
Incorrect:
Copied to your clipboard1@import 'source/lib/_lib';2@import (css) 'styles';
Use single quotes.
Correct:
Copied to your clipboard@import 'source/lib/_lib.less';
Incorrect:
Copied to your clipboard@import "source/lib/_lib.less";