📜 ⬆️ ⬇️

Ways to organize CSS-code

Developer Ben Frain once remarked: “Writing CSS is easy. Scaling and maintaining it is not. ”

Fortunately, on the Internet you can find many solutions to this problem. This article describes the main whales of the structure of the CSS-code, as well as interesting fish / mammals smaller.

The article serves only as a reminder or a quick reference book - it is strongly recommended to study the original documentation to get acquainted with the methodologies in detail.
')
So, let's begin.

Content




BEM


image

Author: Yandex
Documentation

Perhaps the most popular methodology in the world today. The name means "Block, element, modifier".

These categories play the following roles in the code:


Classes are built from these categories for direct use in CSS. In this case, you should not use tag selectors (this could potentially prevent the use of a block / element in a non-standard place on the site, ie, it binds the entity to the context); It is also recommended to avoid cascading selectors, since the resulting class itself quite accurately (specifically) selects the corresponding element.

This approach eliminates the unpredictable effects of the cascade and isolates individual modules from each other.

Sample code in BEM style:

.block_element {...} .block_element-modifier {...} 

For convenience, different separators are used between the block and the element and between the block / element and modifier; you can use any separators, but they must be the same within the same project.

And now about honey barrels and fly in the ointment:


By the way, for writing code in this style, the Stylus preprocessor was often recommended before, since it allows you to write code like this:

 .block { &-element {...} } 

Now you can write this using both SASS and LESS.

And one more note - in the original BEM methodology, the names of the modifiers are used that contain the modified value itself, for example, button - sizeLarge. Now in small projects, many developers do not indicate it for brevity: button - large.

Here you can read about the history of the BEM.

OOCSS


image

Posted by Nicole Sullivan
Presentation

OOCSS stands for Object Oriented CSS (Object-Oriented CSS). There are two main ideas in this approach:


So "divide and conquer." Using this structure, the developer gets common classes that can be used in different places.

And now - two news (as usual, good and bad):


In addition, the OOCSS approach itself does not offer specific rules, but abstract recommendations; therefore, the method is rather complicated to put into practice.

But, as it sometimes happens, some OOCSS ideas inspired the authors to create their own, more specific, ways of structuring code - the original forks of OOCSS.

SMACSS


image

Posted by: Jonathan Snook
Documentation

SMACSS stands for “Scalable and Modular Architecture for CSS” (Scalable and Modular Architecture for CSS).

The main goal of the approach is to reduce the amount of code and to simplify code support.

So, Jonathan suggested dividing the styles into 5 parts (in order to include them in the document):

  1. Base rules - basic styles. These are styles of the main elements of the site - body, input, button, ul, ol, etc. This section mainly uses tag and attribute selectors, classes - in exceptional cases (for example, if you have JavaScript-styled selects);
  2. Layout rules - layout styles. Here are the styles of the global elements of the size of the cap, footer, sidebar, etc. Jonathan suggests using id here in selectors, since these elements will not occur more than 1 time per page. However, the author of the article considers this a bad practice (every time an id selector appears in styles, somewhere in the world a kitten is sad). Use classes and you will be happy.
  3. Modules rules - styles of modules, that is, blocks that can be used several times on one page. For classes of modules, it is not recommended to use id and tag selectors (for repeated use and context independence, respectively).
  4. State rules - state styles. In this section, the various states of the modules and the skeleton of the site are prescribed. This is the only section in which the use of the "! Important" keyword is permissible.
  5. Theme rules - design. It describes the styles of designs that may eventually need to be replaced (so convenient to do, for example, New Year's decoration; for html themes for sale, such styles allow you to switch colors, etc.).

It is also recommended to enter namespaces for classes belonging to a particular group, as well as use a separate namespace for classes used in JavaScript.

This approach really allows to simplify the writing and maintenance of the code and recently attracted a fairly large number of developers.

Atomic CSS


image

Author: Thierry Koblentz - Yahoo
Original article

Atomic CSS, rarely also ACSS - atomic CSS. In some ways, this approach is an absolute OOCSS.

When using this approach, a separate class must be formed for each reused property. Example: the “margin-top: 1px” style implies the creation of the “mt-1” class, the “width: 200px” style and the creation of the “w-200” class.

This style allows you to minimize the amount of CSS-code due to the reuse of declarations, as well as relatively easy to make changes to the modules, for example, when changing technical specifications.

However, this approach has significant drawbacks! Here they are:


Due to these shortcomings, the approach was met with a significant amount of criticism. However, the approach can be effective for very large projects - it seems Yahoo! quite pleased with the use of ACSS ( link to an interesting presentation of the same Thierry Koblentz ).
In addition, atomic CSS is used in various frameworks to specify corrective element styles and in some layers of other methodologies.


MCSS




Author: Robert Kharitonov - Classmates
Documentation

“Ogres are like onions. The ogres have layers. And the bow has layers. Both have layers. ”
Shrek

MCSS - Multilayer CSS (Multilayer CSS). This style of writing code, originated in the company "Odnoklassniki", proposes to divide the styles into several parts, called layers .


The hierarchy of interaction between layers is very important:


I also recommend viewing the excellent MCSS presentation - there you can learn more about MCSS and more.


AMCSS




Posted by: Glen Maddern
Documentation

The name of the approach means Attribute Modules for CSS. This is a rather unusual approach, the references of which, however, are increasingly found on the development guru blogs.

To summarize, this method is a somewhat more human-readable representation of the BEM structure. Let's look at an example:

 <div class="button button--large button--blue"></div> 

Such a chain of classes is not very pleasing to the eye, so let's group these values ​​by attributes.

Here's what happens:

 <div button="large blue"></div> 

In order to avoid name collisions, it would be nice to add namespaces to the attributes, wouldn’t it? Then the code of our button will take the following form:

 <div am-button="large blue"></div> 

If you use the validator to check the code for correctness, do not forget to also add the prefix "data-" before the attribute name.

To write the CSS code, a little-known selector “~ =” (IE7 +) is used, which works as a class attribute: selects elements whose attribute values ​​contain specified words, separated by spaces. So, the selector of the form a [class ~ = "link"] [class ~ = "button"] is similar to the selector a.class.button (even in specificity, since the specificity of selectors in class and attribute are equal to each other!).

Respectively, CSS code

 .button {...} .button--large {...} .button--blue{...} 

Converted to

 [am-button] {...} [am-button~="large"] {...} [am-button~="blue"] {...} 

Well, how? If you think such a code is too original, for you there is a less radical form of using AMCSS:

 <div am-button am-button-large am-button-blue></div> 


FUN




Posted by ben frain
Original article

FUN means “flat hierarchy of selectors, service styles, components with names” (Utility styles, Name-spaced components). Well, or fun =)

For each letter of the name stands a certain principle:


Some developers note that code written using these principles is quite convenient to write and maintain; in a way, the author took the best from BEM and SMACSS and outlined these techniques in a simple and concise manner.

This approach imposes quite a few requirements on the structure of the code and the project; it only establishes the preferred form of the record of selectors and the way they are used in markup. But in small projects these rules may be quite enough to create high-quality code.

Conclusion


That's all!

As you can see, among these approaches there is no ideal - they all have their own advantages and disadvantages.
Therefore, I can only remind you that none of these approaches is an absolute dogma - you can take the approach from starting to create something of your own, or create a new approach from scratch. And maybe this method will allow the web to rise to the next step.

Thanks for attention!

Source: https://habr.com/ru/post/256109/


All Articles