简体   繁体   English

CSS最佳实践/新手问题

[英]CSS Best Practice/Newbie Question

Ok, a really quick question - which is the best way out of these to apply css styles: 好的,一个非常快速的问题 - 这是应用css样式的最佳方法:

1 - Use lots of different classes that apply different parts of the style ie class='font-1 red-bkg border-1' etc etc. 1 - 使用许多不同的类来应用不同的风格部分,即class ='font-1 red-bkg border-1'等。

Or 要么

2 - Style up individual parts of the site seperately 2 - 单独设置网站的各个部分

What you should do for font for example is to apple it to body , same for background colour, font colour etc... 例如,你应该为font做的是把苹果涂在body ,同样用于背景颜色,字体颜色等......

body{font: Verdana 38px; color: #000; background: #fff;}

Then for individual features (eg margins, padding, borders etc) they should be defined in a per-class way. 然后对于单个特征(例如边距,填充,边框等),它们应该按照每个类的方式定义。

.classname {
    margin: 0px 5px 10px 5px;
    padding: 10px 5px 10px 6px;
 }

It is better for maintainability and makes your HTML less messy. 它更易于维护,并使您的HTML不那么混乱。

I believe to justify shared classes you should have more than one property in it, otherwise you are not gaining anything from using CSS's modularity. 我相信为共享类证明你应该有多个属性,否则你没有从使用CSS的模块性中获得任何东西。

Ie things like this are not good ideas: 即这样的事情不是好主意:

.bold { font-weight: bold; }

Style up individual parts of the site seperately. 单独设计网站的各个部分。 The other solution would kind of screw the intention behind it - separating content from styling. 另一种解决方案会将其背后的意图搞砸 - 将内容与造型分开。

http://jsfiddle.net/sheriffderek/RMfEn/ http://jsfiddle.net/sheriffderek/RMfEn/

html HTML

<section class='container blocks'>
    <h2>Blocks of content</h2>
    <div class='block highlight-theme'>
        <p>None of the styling should be done in the html.</p>
    </div>

    <div class='block base-theme'>
        <p>You can use modular classes to style common pieces of the layout and then modify them with more specific classes.</p>
    </div>

    <div class='block contrast-theme'>
       <p>So the stuff in this box could be a dark-theme with .contrast-theme or something</p>
    </div>
</section>


css CSS

.container, .block { /* structural elements */
    width: 100%;
    float; left;
    padding: .5rem;
    overflow: hidden; /* use a clear-fix instead */
}

/* mini themes /// mix and match */

.base-theme {
    background: lightgray;
    color: black;
}

.highlight-theme {
    background: yellow;
    color: red;
}

.contrast-theme {
    background: gray;
    color: white;
}

You should name logically classes, because when you change your layout and currently you have style like 你应该在逻辑上命名类,因为当你改变你的布局时,你现在的风格就像

.bold .5px-brd .red.bg

then changing this to another colour and style will include grep'ing through entire application code in order to correct css styles. 然后将其更改为另一种颜色和样式将包括grep'ing整个应用程序代码,以纠正CSS样式。

As you may notice approach like 你可能会注意到这样的方法

.bold .5px-brd .red.bg

it's good, and don't go with philosophy of CSS. 它很好,不适合CSS的哲学。

Classes with name like .bold should be used as auxiliary style. 名称如.bold的类应该用作辅助样式。 Never as basic construction block. 从不作为基本的建筑块。

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

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