Best practice for CSS class naming for use with jQuery selectors

前端 未结 6 2038
北恋
北恋 2020-12-28 15:55

While building a Javascript-heavy web application, what is the best practice for naming CSS classes to keep the Javascript code and CSS stylesheets clean and the UI structur

6条回答
  •  生来不讨喜
    2020-12-28 16:32

    Trying to deal with unique names can work well for small projects, but the larger you get the more likely you will have conflicts.

    That is why I like the second approach.

    However, to make it easier, you can use SASS, to pre process your css files. You can then do nesting like this:

    #list {
        .delete {
        }
        .items {
            .item {
            }
        }
    }
    

    And you will get code similar to your second example, without having to write it all out.

    As for the jQuery selectors, those would still need to be written out longhand if you wanted to do it that way, but having complex selectors like that is often considered a sign of a bad design.

提交回复
热议问题