Tuts+: How We’re Using Modules to Organize Our Front-End Code

…[W]e adopted an approach to CSS based on the BEM methodology. Instead of defining styles which apply globally, all styles are siloed into self-contained “blocks” by way of a naming convention. A “block” is defined, more or less, as a single free-standing unit of content that is potentially reusable (although it’s not mandatory that it actually be reused).

For example, let’s take a look at the “featured-sections” block:

Tuts Featured Sections Block

According to our naming convention, this block has a root div element with the class name featured-sections. It contains elements with class names such as featured-sections__title and featured-sections__section-link.

We’re using a matching naming convention for our source code, as such all the styles for this featured-section block are stored in modules/featured_section.sass:

This naming convention ensures that styles no longer conflict and intermingle. As long as our naming convention is followed, with the block name at the start of each class name, it’s impossible for a style to affect something outside of its own block.

It also makes it super easy to work out where to look in the codebase for the styles corresponding to an element. You can simply look at the element’s class name, and you’ll know the name of the stylesheet to open.

source

Leave a Reply

Your email address will not be published. Required fields are marked *