There are several recognized methodologies for formatting your CSS. Ultimately, it’s up to you that you feel most comfortable, but it helps you manage CSS for more complex projects. Not that it matters, but I tend to use a combination of BEM and SMACSS.
BEM is a very useful, powerful, and simple naming convention that simplifies the reading and understanding of your front-end code, simplifies its operation, simplifies scaling, improves reliability and clarity, and is much more strict.
Block
An autonomous entity that makes sense on its own, for example:
header, container, menu, checkbox, input
Element
Parts of the block and do not have a separate meaning. They are semantically attached to their block:
menu item, list item, checkbox caption, header title
Modifier
Flags on blocks or elements. Use them to change the appearance or behavior:
disabled, highlighted, checked, fixed, size big, color yellow
OOCSS's goal is to encourage code reuse and, ultimately, faster and more efficient stylesheets that are easier to add and maintain.
OOCSS is based on two main principles:
- Separation of the structure from the skin
This means defining repeating visual functions (for example, background and border styles) as separate “skins” that you can mix and match with different objects to achieve a large amount of visual diversity without a lot of code. See Object of the module and its shell.
- Separation of containers and contents
Essentially, this means "rarely use location-specific styles." The object should look the same no matter where you put it. So instead of a style specific to .myObject h2 {...}, create and apply a class that describes this question, for example. This gives you confidence that: (1) all unclassified s will look the same; (2) all elements with a category class (called mixin) will look the same; and 3) you do not need to create an override style for the case when you really want .myObject h2 to look like normal.
SMACSS is a way to learn your design process and as a way to adapt this rigid framework to a flexible thinking process. This is an attempt to document a consistent approach to website development using CSS.
There is categorization in the core of SMACSS. By classifying CSS rules, we begin to see patterns and can identify best practices around each of these patterns.
There are five types of categories:
Base Contains reset and default styles. It can also have basic styles for controls such as buttons, grids, etc., which can be overwritten later in the document under certain circumstances.
Markup Will contain all the navigation, breadcrumbs, Sitemaps, etc. Etc.
Modules Contain area-specific styles such as contact form styles, homepage tiles, product listings, etc. Etc. Etc.
State Contains state classes such as isSelected, isActive, hasError, wasSuccessful, etc. Etc.
Theme Contains any styles related to the theme.
There are too many details, but look at the others: