1. What are CSS methodologies? Source of the above image: https://2019.stateofcss.com/technologies/ You may not take the time to pay attention to and understand CSS methodologies in your daily development, but as you gain experience, you may think for yourself, or read other people’s code, and refer to mature frameworks on the Internet, which more or less contain some shining points of CSS methodologies. The following mainstream CSS methodologies can help you summarize and sort out the content. 2. Common CSS methodologies 1. OOCSS
(1) Rules 1. Don’t use ID, use Class Directly use classes to set styles. This not only increases semantics, but also reduces CSS's dependence on HTML. 2. Separation of structure and style Although early HTML and CSS achieved the separation of structure and style, there are still two types of styles in CSS:
So OOCSS recommends separating these two styles. For example, there are three buttons, white/green/red, which can be defined as: class="btn btn-default" class="btn btn-green" class="btn btn-red" If you really have a lot of purple buttons, you can create a separate purple button class. This can greatly reduce the amount of CSS code. 3. Separation of container and content Content should never be confined to a specific container and should be freed for reuse. # Wrong way to write.header.action { } .header .action .login { } .header .action .register { } # Correct way to write .header{ } .action{ } .login { } .register { } Inherited selectors are useful because they can reduce style conflicts caused by the same name (often occurring when multiple people are collaborating on a project). However, we should not overuse it. (2) Advantages and Disadvantages benefit:
shortcoming:
(3) Examples Bootstrap uses OOCSS. For example: html <div class='header'> <ul class='menu'> <li class='menu-item active'>1</li> <li class='menu-item'>2</li> <li class='menu-item'>3</li> </ul> <div class="action"> <button class="btn btn-login">login</button> <button class="btn btn-register">register</button> </div> </div> CSS: .header { } .menu { } .menu-item { } .item.active { } .action { } .btn { } .btn-login { } .btn-register{ } 2. BEM (1) Content BEM consists of three parts:
(2) Rules 1. Naming conventions
In the standards of some companies (such as Tencent), double hyphens are replaced by single underscores ( For example: demo-html: <div class='header'> <ul class='header__menu'> <li class='header__menu-item--active'>1</li> <li class='header__menu-item'>2</li> <li class='header__menu-item'>3</li> </ul> <div class="header__action"> <button class="header__btn--login">login</button> <button class="header__btn--register">register</button> </div> </div> demo-less: .header { &__menu {} &__menu-item {} &__action {} &__btn { &--login {} &--register {} } } 2. Avoid nesting BEM has at most three levels: B+E+M. So please avoid the format of The block here is very similar to a namespace. (3) Advantages and disadvantages benefit:
shortcoming:
other: BEM naming will make the class name longer, but after gzip compression, the bandwidth overhead can be ignored. BEM does not allow the use of tag selectors. Even the simplest li must be written as .menu-item. (4) Practice Ele.me's framework elementUI is a type of BEM, or you can also study the website company.yandex.ru/. 3. SMACSS (1) Rules 1. Categorizing CSS Rules It divides CSS into 5 different categories: For example, CSS Reset and CSS Normalize. For example, left and right columns, and grid systems. For example, a product list, a navigation bar. Reusable. For example, the selected state. 2. Naming Rules Add Base does not require a prefix. And use tags instead of classes and IDs.
example: <div class="l-box m-profile m-profile--is-pro-user"> <img class="m-avatar m-profile__image" /> <p class="m-profile__bio">...</p> </div> (2) Examples Online demo: https://codepen.io/savemuse/pen/gWVpvd 4. Atomic CSS (1) Example <div className="P(10px) M(20%) Pos(f) Start(50%) Bgc(#fff)" /> There will be a special Atomic CSS tool to help parse the class name in the above HTML into normal CSS. (slightly) (2) Advantages and Disadvantages Benefits: Minimize CSS styles into components and maximize reusability. Disadvantage: This is basically writing inline-style, but we use class name to express it. (3) Summary This approach is really radical. I can't accept it for now. Conclusion Leaving aside the radical Atomic, I have the following suggestions for the remaining OOCSS/BEM/SMACSS: Their respective ideas are complementary and conflicting, and they can be used in a trade-off manner in actual development. They can all be combined with CSS preprocessors (such as less/sass) for better efficiency By comparing and summarizing the benefits introduced above, we find that the core problem they solve is: modularity The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. |
<<: Building FastDFS file system in Docker (multi-image tutorial)
>>: 14 Ways to Create Website Content That Engages Your Visitors
1. When inserting, updating, or removing DOM elem...
Maybe everyone knows that js execution will block...
Table of contents background Understanding compos...
1. Create a folder to store nginx shell scripts /...
This article mainly introduces the method of CSS ...
Copy code The code is as follows: <span style=...
1. Dynamic Components <!DOCTYPE html> <h...
Installation The required documents are provided ...
Table of contents 1. What is a custom instruction...
filter is generally used to filter certain values...
I'm currently learning about MySQL optimizati...
When I turned on my MAC at night, I found that th...
This article uses examples to describe how to cre...
Today I will share with you a neon button animati...
Table of contents 1. List interface display examp...