CSS Architecture
CSS ARCHITECTURE:
CSS is basically an intuitive language. It is used for describing the presentation of a document. Its main purpose is to define a selector to target HTML and apply the attributes to them. By defining a selector can be easily overridden by some other selectors and also elements can inherit their properties from their parents on the DOM tree.
That is when a developer defines a CSS class, it automatically affects the global scope which results in the modification of all the related elements and also their children. But if we want to modify the specific element, this approach can’t be useful. And when the size of the application and the team members grow, this type of styling would mess up all the other things. That’s where CSS architecture comes to play.
COMMON PRACTICES TO BE AVOIDED:
Modifying the elements based on their parents
.navbar {
background: white;
color: black;
width: 50%;
}
#sidebar .navbar {
width: 200px;
}
Body.homepage .navbar{
background: white;}
Inordinately the combinators.
#navbar ul li ul li div{ }
#contents article h1:first-child{ }
Forging generic names for classes.
.card {}.card .title{}.card .desc{}.card .content{}
Though those codes are technically valid, each has led to disaster while working in a group or large application.
MAJOR OBJECTIVES OF CSS ARCHITECTURE:
The four major objectives of CSS architectures are Predictable, Maintainable, Reusable, Scalable.
Predictable:
` Since CSS itself is an intuitive language, CSS architecture should be the same. If a newbie is in a project, he/she itself can easily visualize all the stuff.
Maintainable:
When some component is being added to the page, it shouldn't make a drastic change to all other components.
Reusable:
CSS rules of a particular component should be abstract, so that we can build new components quickly from the existing one.
Scalable:
Scalable CSS means it can be easily managed by a single person or a large group. It should be easily approachable without requiring an enormous amount of learning.
STEPS TO BE FOLLOWED:
CSS architecture can be achieved by the modular approach. The three steps to start structuring our CSS are
- Breaking the code into smaller chunks (SASS, LESS).
- Coding the components should be individual and in an encapsulated manner.
- Naming the CSS selectors according to their purpose and relationship with other elements(BEM, SMACSS).
These jottings are just the starting point. For further reading on this concept, I would recommend you to go along with our other posts.
Comments
Post a Comment