简体   繁体   English

CSS命名MVC布局的最佳实践

[英]CSS Naming Best Practice in MVC Layouts

I need to create a CSS naming convention in an MVC Project with Layouts. 我需要在带有布局的MVC项目中创建一个CSS命名约定。

The use of Layouts creates the need to be very careful when it comes to choose the name of a class because it can be override by one declared in the Layout CSS file. 在选择类的名称时,Layouts的使用需要非常小心,因为它可以被布局CSS文件中声明的一个覆盖。

One rule I use is to have only element classes for style and element ID's for jQuery use. 我使用的一个规则是只使用样式的元素类和jQuery使用的元素ID。

Let say I have a layout like this: 假设我有这样的布局:

<div class="lyb-ctn">
    <div class="lyb-wrp">

        @RenderBody()

        <div class="lyb-ctn-rgt">
            <div class="lyb-ctn-subscribe">
                <p class="lyb-ctn-subscribe-title">Subscribe</p>
                <input placeholder="Email" /><input type="button" />
            </div>
            <div class="lyb-ctn-categories">
                <p class="lyb-ctn-categories-title">Categories</p>
                <div class="lyb-ctn-categories-ctn-list">
                    <div class="category">
                        <p>Cars</p>
                        <p>Boats</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

One option would be: 一种选择是:

.lyb-ctn {
    position:fixed;
    padding:0px;
    margin:0px;
    width:100%;
    height:100%;
    background-color: #f2f2f2;
    padding-top: 50px;
}

.lyb-wrp {
    max-width: 960px;
    width: 95%;
    margin: auto;
    background-color: #ffd800;
}

.lyb-ctn-rgt {
    float: right;
    width: 235px;
    border: solid 1px #ff6a00;
}

.lyb-ctn-subscribe {
    width: 100%;
}

.lyb-ctn-subscribe-title {
    color: #80bd01;
}

.lyb-ctn-categories {
    width: 100%;
}

.lyb-ctn-categories-title {
    color: #80bd01;
}

I also build an other option but this one, I think, is dangerous because if it happens to exist a ".rgt-ctn" in the parent layout, it can override this one: 我还构建了另一个选项,但我认为这个选项很危险,因为如果它在父布局中恰好存在“.rgt-ctn”,它可以覆盖这个:

.lyb-ctn {
    position:fixed;
    padding:0px;
    margin:0px;
    width:100%;
    height:100%;
    background-color: #f2f2f2;
    padding-top: 50px;
}

.lyb-ctn .wrp {
    max-width: 960px;
    width: 95%;
    margin: auto;
    background-color: #ffd800;
}

.lyb-ctn .wrp .rgt-ctn {
    float: right;
    width: 235px;
    border: solid 1px #ff6a00;
}

.lyb-ctn .wrp .rgt-ctn .subscribe-ctn {
    width: 100%;
}

.lyb-ctn .wrp .rgt-ctn .subscribe-ctn .title {
    color: #80bd01;
}

Here is another one that seems clean but we can't see the hierarquy of the DOM when we look at it, and I think it maybe more difficult to find an element to edit: 这是另一个看起来干净的但我们看不到DOM的层次结构,我认为找到一个要编辑的元素可能更难:

.lyb-ctn {
    position:fixed;
    padding:0px;
    margin:0px;
    width:100%;
    height:100%;
    background-color: #f2f2f2;
    padding-top: 50px;
}

.lyb-wrp {
    max-width: 960px;
    width: 95%;
    margin: auto;
    background-color: #ffd800;
}

.ctn-side-options {
    float: right;
    width: 235px;
}

.ctn-subscribe,
.ctn-categories,
.ctn-tags {
    width: 100%;
}

.ctn-subscribe .title, 
.ctn-categories .title,
.ctn-tags .title {
    color: #80bd01;
    padding: 25px 0 10px 5px;
}

.ctn-categories .ctn-list, 
.ctn-tags .ctn-list {
    width: 100%;
    background-color: #fff;
    border: solid 1px #e6e6e6;
    border-radius: 3px;
    margin: 0;
    padding: 0;
}

Or is there a better approach? 还是有更好的方法?

TLDR : Get organised. TLDR:组织有序。

Firstly, I strongly recommend you read up on: 首先,我强烈建议您阅读:

OOCSS - http://www.smashingmagazine.com/2011/12/12/an-introduction-to-object-oriented-css-oocss/ OOCSS - http://www.smashingmagazine.com/2011/12/12/an-introduction-to-object-oriented-css-oocss/

SMACSS - https://smacss.com/ SMACSS - https://smacss.com/

and

BEM - http://csswizardry.com/2013/01/mindbemding-getting-your-head-round-bem-syntax/ BEM - http://csswizardry.com/2013/01/mindbemding-getting-your-head-round-bem-syntax/

Putting some effort into gaining an understanding of a modular approach to CSS at the start of a project will pay huge dividends as the project becomes larger. 在项目开始时花些精力去理解CSS的模块化方法,随着项目变得越来越大,它将带来巨大的回报。 Having a strong naming convention will make life a lot easier if you are working on a project with two or more developers. 如果您正在与两个或更多开发人员合作开展项目,那么拥有强大的命名约定将使生活变得更加轻松。

Next, if you aren't already doing so, it it well worth transitioning to a CSS pre-processor such as SASS - http://sass-lang.com/ , LESS - http://lesscss.org/ or STYLUS - http://learnboost.github.io/stylus/ 接下来,如果您还没有这样做,那么值得转换到CSS预处理器,例如SASS - http://sass-lang.com/,LESS - http://lesscss.org/或STYLUS - http://learnboost.github.io/stylus/

Using a pre-processor helps organisation because you can break your stylesheets into as many files as you want, then import them all into one at compile time to follow performance best practice. 使用预处理器可以帮助组织,因为您可以将样式表分成任意数量的文件,然后在编译时将它们全部导入到一个文件中,以遵循性能最佳实践。 (See also - http://thesassway.com/advanced/modular-css-naming-conventions ) (另见 - http://thesassway.com/advanced/modular-css-naming-conventions

You mention you have a rule of only using classes as CSS hooks and IDs as JS hooks - this is a good start, however I recommend moving away from using IDs and move towards a js- prefixed class. 你提到你有一个规则只使用类作为CSS钩子和ID作为JS钩子 - 这是一个很好的开始,但我建议不要使用ID并转向js-前缀类。 In this way your JS also becomes re-usable. 通过这种方式,您的JS也可以重复使用。

The next thing to look at is creating a pattern library - a great example of this is Mailchimp's example - http://ux.mailchimp.com/patterns 接下来要看的是创建一个模式库 - 一个很好的例子就是Mailchimp的例子 - http://ux.mailchimp.com/patterns

So much for general guidance - how is all this relevant to: 如此多的一般指导 - 这些都与以下内容相关:

"I need to create a CSS naming convention in an MVC Project with Layouts. The use of Layouts creates the need to be very careful when it comes to choose the name of a class because it can be override by one declared in the Layout CSS file." “我需要在带有布局的MVC项目中创建一个CSS命名约定。使用布局会在选择类名时需要非常小心,因为它可以被布局CSS文件中声明的一个覆盖“。

You will see all of the above guides all aim towards organising your CSS (and JS) in to chunks of reusable code. 您将看到所有上述指南都旨在将CSS(和JS)组织成可重用代码块。 The key to this is planning, and having a canonical reference for each chunk you are going to use - hence the pattern library. 关键是计划,并为您将要使用的每个块提供规范参考 - 因此是模式库。

In your example you might like to create a guide for each of your layouts - how is each layout used and how does / should it affect descendant content? 在您的示例中,您可能希望为每个布局创建一个指南 - 每个布局如何使用以及它如何/应该如何影响后代内容?

Where you want layouts to pro-actively influence their descendant content you will want to scope them. 如果您希望布局主动影响其后代内容,您需要将它们作为范围。 So wrap them in a class which can act as a namespace. 所以将它们包装在一个可以充当命名空间的类中。 Ie

<div class="layout-one">[ALL DESCENDANT CONTENT HERE]</div>
.layout-one p {
    color: green;
}

<div class="layout-two">[ALL DESCENDANT CONTENT HERE]</div>
.layout-two p {
    color: red;
}

However, I take it your concern is more that your layouts are going to re-actively influence descendant content in a way which breaks your design. 但是,我认为你更关心的是你的布局会以一种破坏你的设计的方式重新影响后代内容。 So scope any styles which relate only to the layouts but not the descendants more tightly. 因此,任何仅与布局相关但不更紧密地与后代相关的样式的范围。

Rather than: .layout-one p {} as above, go for .layout-one__p {}, .layout-one__h4 {} 而不是:.layout-one p {}如上所述,去.layout-one__p {},.layout-one__h4 {}

As you can see this BEM inspired naming convention makes it immediately clear the the style in question is directly part of the layout. 正如您所看到的,这个受BEM启发的命名约定使得它立即变得清晰,所讨论的样式直接是布局的一部分。 We would not expect to see this class being used outside of the layout HTML, so we can write styles without any concerns about them affecting descendant content. 我们不希望看到这个类在布局HTML之外使用,所以我们可以编写样式而不用担心它们会影响后代内容。

Or, we can write styles within a layout's namespace and be sure that our styles will be affected as we want. 或者,我们可以在布局的命名空间中编写样式,并确保我们的样式会受到影响。

I think the first and second layout are totally different story. 我认为第一和第二种布局是完全不同的故事。 For instance: in the second layout, (I Believe) class .lyb-ctn .wrp means they will also pass rules havent been took place from .lyb-ctn to .wrp . 例如:在第二个布局中,(I Believe)类.lyb-ctn .wrp意味着它们也将传递从.lyb-ctn.wrp

.lyb-ctn .wrp{
    position:fixed;
    padding:0px;
    margin:0px;
    width:100%;
    height:100%;
    background-color: #f2f2f2;
    padding-top: 50px;

following is the code from .wrp which will override only part of the .lyb-ctn class 以下是从.wrp的代码,其将覆盖.lyb-CTN类部分

    max-width: 960px;
    width: 95%;
    margin: auto;
    background-color: #ffd800;

}

Well, they might appear to be the same in browser for now. 好吧,它们现在可能在浏览器中看起来是一样的。 But with more and more style involves in, it's very easy to get into unexpected result. 但随着越来越多的风格涉及,很容易陷入意想不到的结果。

I think the third one makes more scenes in this case. 我认为第三个在这种情况下会产生更多场景。

first I'd use camelcase naming 首先,我使用camelcase命名

second use as much classes and as little ids in css as possible as this enables code reuse, also it's better to have 第二,尽可能多地使用css中的类和小ID,因为这样可以重用代码,也可以使用

.w100 { width: 100%; } .black { color: black; }

than

.something { width: 100%; color: black; }

or even worse 甚至更糟

#something { width: 100%; color: black; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM