I first came into contact with object-oriented CSS because the CSS in the project has exceeded 8,000 lines and lacks constraints and management. In the near future or in the near future, there are urgent requirements for reconstruction. In the front-end refactoring, we have already discussed the object-oriented refactoring of JavaScript. At this time, let's take a look at CSS object-oriented and modularity, which provides a very good idea for my work.
First of all, what I want to say is that in this era of turbulent concepts, stubborn pursuit of certain concepts and meanings will not lead to better design and higher productivity. Object-oriented is a This idea, or a methodology, brings some inspiration to CSS reconstruction. This is enough. There is no need to worry about the definition and rigor of OO CSS.
What problem did our CSS code encounter?
The reusability is poor. Looking at the name of a CSS, it is difficult to tell which modules may reference it and which parts of the web page this CSS is used for;
As a result, no one dares to modify and Delete, the following styles can only be piled on top;
Afraid of duplicate CSS names, names like s1, s2, t1, and t2 began to appear. This is tantamount to drinking poison to quench thirst. No one knows what these styles do. ;
As a result, CSS is getting bigger and bigger;
The ideas of high-fidelity artists and business developers are completely different. We see various styles of CSS definitions and naming;
Sometimes, you will also encounter the problem of CSS conflict. It all boils down to that sentence: Complexity is the root of all software problems.
Principles of CSS reconstruction:
Vertical modularization: Create CSS module files from large to small, such as: Public-> Portal-> Channel-> Column-> Content ;
Horizontal modularization: suitable for some highly independent components, such as player modules and pop-up layer modules;
Provide guiding CSS: for example, prepare several sets of styles for list type display , all the list displays in the entire system are gathered into this place. The replacement, comparison and modification of future skins will greatly facilitate the artist;
CSS thumb principle: If two of the same functional areas The styles are very similar, only one is kept!
The frame (column) uses the grid to control the width, and the content controls the height. When the page is composed of several columns, the width of each column can be fixed or fixed proportionally by the channel, but the height needs to depend on content.
After the rules of CSS are set, they need to be controlled and managed from the perspectives of open source and cost reduction. On the one hand, newly added CSS must act in accordance with the rules. This needs to be given to high-fidelity designers and business developers. Make requirements; on the other hand, the original CSS needs to be gradually reconstructed.
CSS itself supports inheritance and modularization. In addition, on HTML pages, it is easier to use styles and extended styles, such as: "div class="player wmpPlayer"".
If one day, the front-end of the project becomes so complex that UML modeling is required for HTML, then put CSS in it too. DOM and its affiliated and associated models are very suitable for modeling.
Finally ends with the example from http://oocss.org/:
Css code