Changing the site design: quick restart of your project
I wrote many times that the web design master must be creative, must look for new solutions to everyday situations, keep an eye on fashion ... But unfortunately, fashion is…

Continue reading →

Advanced CSS: Using Sass Maps for UI Components
Sass is a great and very popular CSS preprocessor. If you are not familiar with it, take a look at these lessons. Associative arrays (Maps) are an extremely underrated feature…

Continue reading →

Web design standards: everything you need to know for beginners and pros
How do design standards increase website popularity? There is an opinion that web design standards attract users to sites, and most importantly - keep them for a long time. Why?…

Continue reading →

Advanced CSS: Using Sass Maps for UI Components

Sass is a great and very popular CSS preprocessor. If you are not familiar with it, take a look at these lessons.

Associative arrays (Maps) are an extremely underrated feature of Sass. They help automate the creation of user interface elements and improve the architecture of the entire application. Associative arrays will come in handy when you need a set of modifier classes for user interface elements.

A modifier is a class that modifies the style of a block depending on the context. A block is a universal parent class that can be used on its own.

BEM (Block Element Modifier)

I have summarized several code snippets and patterns that I often use. Although, of course, you must be able to apply this technique to other elements of the interface.

Sass Associative Array Syntax
Sass (Scss)
$ map: (
key-1: value-1,
key-2: value-2,
key-3: value-3
);
You can get the value by the key like this:

Sass (Scss)
map-get ($ map, key)
Customization
You probably have a Sass file in which you declare all the variables (something like _variables.scss or _base.scss). If not, you must create it. Here we will describe our map.

After several attempts and errors, I realized that the best way to configure associative arrays is to first declare the variables and then create a Map with these variables. This method allows you to use simple variables in the code (for example, $ color-robin) instead of getting the value using map-get (for example, map-get ($ ui-colors, primary)).

Sass (Scss)
// UI Colors
$ color-robin: # 8FCCCC;
$ color-vista: # 79D1AD;
$ color-mandy: # e67478;
$ color-apricot: # ed8864;
$ color-eastside: # 9279c3;

// UI Colors Map
$ ui-colors: (
default: $ color-robin,
success: $ color-vista,
error: $ color-mandy,
warning: $ color-apricot,
info: $ color-eastside
);
I usually declare 4 maps:

Theme colors (corporate identity colors)
Colors of interface elements (colors for errors, tooltips, buttons)
Grayscale (colors for text, shadows, backgrounds)
Corporate colors (used, for example, for social network icons)
The same template will be used for typography. You can first declare individual variables for fonts, and then put them in an array.

What variables should be used in an associative array? The variables that you will use in mixins to create modifier classes. Except for the obvious – color selection – associative arrays can be used for typography, icons or images. In the following example, I will show how to create different classes for buttons using the UI Colors color map.

Buttons
I use the each directive, which loops through the $ ui-colors array and creates a modifier class for the .button base class. We get modifiers to style buttons like ‘error’ or ‘success’.

Sass (Scss)
.button {
// common styles for all buttons
// …
// generates modifier classes
& – # {$ theme} {
background-color: $ color;
color: transparentize ($ white, .2);
}
& – # {$ theme}: hover,
& – # {$ theme}: focus {
background-color: shade ($ color, 20%);
color: $ white;
}
}
// Usage:
// .button .button – default
// .button .button – success
// .button .button – error
// .button .button – warning
// .button .button – info
This approach eliminates the need for code duplication. And also, if you want to change some color in the color scheme or add new colors – all interface elements will be updated automatically.

A similar pattern can be used for flags, links, and other elements that use modifiers.

Social Networking Icons
I used the same template as in the previous example, but this time I added social network icons. The code below creates a social-media-icon block class and modifiers for various icons.

Sass (Scss)
// Colors
$ color-facebook: # 3B5998;
$ color-twitter: # 55ACEE;
$ color-pinterest: # CC2127;
$ color-youtube: # E52D27;

$ social-media-icons: (
facebook: $ icon-facebook,
twitter: $ icon-twitter,
pinterest: $ icon-pinterest,
youtube: $ icon-youtube
)

// Icons
$ icon-facebook: url (‘assets / facebook.png’);
$ icon-twitter: url (‘assets / twitter.png’);
$ icon-pinterest: url (‘assets / pinterest.png’);
$ icon-youtube: url (‘assets / youtube.png’);

$ social-media-colors: (
facebook: $ color-facebook,
twitter: $ color-twitter,
pinterest: $ color-pinterest,
youtube: $ color-youtube
)
.social-media-icon {
border-radius: 50%;
padding: .5rem;
text-align: center;
background-size: cover;
background-repeat: no-repeat;
@each $ theme, $ color in $ social-media-colors {
& – # {$ theme} {
background-color: $ color;
}
}
@each $ theme, $ icon in $ social-media-icons {
& – # {$ theme} {
background-attachment: $ icon;
}
}
}
// Usage:
// .social-media-icon .social-media-icon – facebook
// .social-media-icon .social-media-icon – twitter

Layout adaptive menu
Adaptive menus collapsing into a hamburger on mobile devices can now be seen on almost any site. In this lesson we will learn to make up such menus. In the…

...

SOLID principles in JavaScript. Open-closed principle
The essence of the principle The principle of openness-closeness is as follows: "Software entities (classes, modules, functions, etc.) must be open for expansion, but closed for change." That is, the…

...

Changing the site design: quick restart of your project
I wrote many times that the web design master must be creative, must look for new solutions to everyday situations, keep an eye on fashion ... But unfortunately, fashion is…

...

Web Design Fundamentals
Often, inexperienced developers do not adhere to generally accepted standards, either because of ignorance, or because of the idea that they are smarter than others, and therefore get a terrible…

...