如何避免类名冲突?

问题描述:

假设我正在建立一个博客,并为时间轴帖子添加一个名为"post"的类.然后,另一个开发人员进入并创建一个高亮部分,其中也有一个"post"类.突然之间我们发生了碰撞.

Let's say I'm building a blog and add a class named "post" for the timeline posts. Then another developer comes in and creates a highlights section which also has a "post" class. All of a sudden we have a collision.

我通常通过嵌套类而不是保留任何全局"来解决此问题,但据我了解,这不是最佳实践:

I usually solve this by nesting classes and not keeping anything "global" but from what I've understood it's not best practice:

.main .post {
    background-color: red;
}

.highlights .post {
    background-color: green;
}

当我检查大型站点CSS文件时,我看到了页面上看似特定部分的全局类名的负载,所以我真的很想知道它们如何解决它.在添加类名之前,他们会搜索并扫描整个项目以查找类名吗?如果要添加已经存在的内容怎么办?仅出于此目的而对其重命名似乎是一个非常糟糕的做法.

When I inspect big sites CSS files I see loads of global class names for seemingly specific parts of the page so I'm really wondering how they solve it. Do they search and scan the whole project for a class name before adding it? What do they do if they want to add something which already exists? Renaming it just for the sake of it seems like a really bad practice if anything.

秘诀是使用某种框架/方法来实施纪律,例如 OOCSS BEM .

The secret is to use some sort of framework/methodology that enforces discipline e.g. OOCSS or BEM.