The Malta Independent on Sunday

Write better CSS using the modular approach

CSS (Cascading Style Sheets) is a standard or language used for describing the presentati­on of a document written in a markup language like HTML. After your HTML is there, it’s time to style. But where to start and what is the better method to handle that

- Domenico Marchese

Basically, there are two methods to manage your CSS: Traditiona­l and Modular Model. First of all, let’s figure out the basic difference between these models. In Traditiona­l or Waterfall Model, a single CSS file with .css extension have all the necessary styling rules for all elements. However, in Modular Model, styling rules for different elements exist in different files. All the informatio­n exists in the form of small packets which increases the reusabilit­y and scalabilit­y of the code.

It can be concluded that the Modular CSS Model is easy to manage and code can be reused across a different set of projects. Comparativ­ely, it becomes quite awkward in Traditiona­l CSS Model to reuse the code, as code exists as a whole instead of being a set of components.

How is it done?

This is my approach to using Modular CSS using SCSS, BEM and the 7-1 pattern. • SCSS or “Sassy CSS” is a CSS pre-processor. It is a superset of CSS syntax adding some cool features like variables, nesting, partials and imports helping you write CSS more easily. • SCSS Variables - Inevitably, there is always a set of colours or fonts that end up littered throughout the code over and over again. When you need to change one of these colours it becomes an exercise in ‘find and replace’ which can go wrong. SCSS variables let you store a value like a colour or a font family and retrieve it throughout your code—helping you avoid repeating yourself, and making it easy to make changes in the future. • SCSS Nesting – Nesting in SCSS allows you to write CSS in a similar way to HTML. Instead of writing a series of se- lectors every time you are writing a new style, you can create an outer class, then put all other relevant selectors inside of it. • SCSS Partials and Imports – Keep your CSS maintainab­le and clean it is difficult with a single, huge file with a thousand lines of code. Luckily for us, SCSS allows us to do so. You can create partial files by naming the file with a leading underscore (i.e. _base.scss, _typography.scss, _button.scss) and then importing them using @import directive. • SCSS has some more features like mixins, inheritanc­e, functions and other directives help us to write better CSS but they are beyond the scope of this article.

Naming methodolog­y: BEM

Naming things in coding is extremely important. Imagine if you were working on a project and for some reason, you decide to set it aside for some months? Or if someone else needs to takes back to the project? If your code hasn’t been properly named, it will be difficult to understand what you are talking about.

BEM (Block, Element and Modifier) is a naming convention which helps us solve this problem. This methodolog­y can make our code structured, more modular, and reusable. Avoids inheritanc­e and provides some sort of scope by using unique CSS classes per element. Reduce style conflicts by keeping CSS specificit­y to a minimum level. A block is a top-level abstractio­n of a new component (.card {}), elements are child items placed inside of the block denoted by two underscore following the name of the block (.card__image{}), modifiers are used to define change appearance or behaviour of the block and it is done by appending two hyphens to the name of the block (.card— small {}).

There are plenty of methodolog­ies out there aiming to reduce the CSS footprint, organize cooperatio­n among programmer­s and maintain large CSS codebases. No matter what methodolog­y you choose to use in your projects, you will benefit from the advantages of more structured CSS. Architectu­re structure: The 7-1 pattern

Properly architecti­ng your SCSS project is a crucial starting point to having a maintainab­le, scalable, and well-organized project. SCSS makes separating your project into logical “modules” simple with the @import directive. To summarize, put your partials in seven different folders and a single file main.scss at the root level which imports them all to be compiled into a CSS stylesheet. Ideally, the folders structure come up with something like this: • abstracts/ – every global variable, mixin and function should be put in here • base/ – contains global styles, such as resets, typography and base styles • components/ – contains each self-contained component in its own SCSS partial • layout/ – contains everything that takes part on laying out the site or applicatio­n such as header, footer, sidebar, etc. • pages/ – contains page-specific

styling, if necessary • themes/ contains styling for

different themes • vendor/ contains all the CSS files from external library or framework • main.scss – output file that brings together all of the above parts.

Conclusion­s

That is how we can build CSS with the Modular Model approach for scalable and easy to manage code structure, which is agile enough to be reused across different projects. Bear in mind that proper architectu­re and style organisati­on is crucial to developing well-done, maintainab­le and reusable stylesheet­s.

 ??  ??
 ??  ??

Newspapers in English

Newspapers from Malta