简体   繁体   English

不同的CSS样式

[英]Different CSS Styles

/* Style 1 */
.myclass {
 background:#ff0;
 border:1px solid #ff0
}
#myid {
 width:80px;
 height:80px;
}

/* Style 2 */
.myclass     { background:#ff0; border:1px solid #ff0 }
#myid        { width:80px; height:80px; }

I'm sure there must be more styles for writing CSS, I'd like to know what are they. 我确定必须有更多的样式来编写CSS,我想知道它们是什么。 Is there an article already written that lists all styles. 是否已经有一篇文章列出了所有样式。

Thanks 谢谢

Here is a good list and discussion: 这是一个很好的清单和讨论:

http://css-tricks.com/different-ways-to-format-css/ http://css-tricks.com/different-ways-to-format-css/

My answer will be more than you want, because I've seen a css files that was 60K long and consisted mostly of classes and ID's overwriting others - because no one knows how css works. 我的答案将超出您的期望,因为我看到了一个css文件,该文件长60K,并且主要由类和ID覆盖其他文件组成-因为没人知道css的工作方式。 They just keep adding things until they get the result they want. 他们只是不断添加东西,直到获得想要的结果。

.myClass { /* 0,0,1,0 */
  top-margin: 1px;
  padding: 3px;
}

#myid { /* 0,1,0,0 */
 width: 80px;
 height: 80px;
}

Repeat with space in between. 在两者之间重复空格。

Now the important stuff. 现在重要的东西。

  1. The comments are the specificity of the selector. 评论是选择器的特异性。
  2. Things should be ordered from least specific to most specific in the file. 在文件中,应按从最低到最高的顺序对事物进行排序。
  3. Within matching specificities, it should be alphabetical. 在匹配的特性内,应按字母顺序。
  4. There should be no more than one selector per definitiion - even if you could have put them all in the same selector. 每个定义最多只能有一个选择器-即使您可以将它们全部放在同一个选择器中。 That way you can find them later. 这样您以后可以找到它们。 And your specificity doesn't get messed up. 而且您的专一性也不会弄乱。 Hopefully you are using a compressor that can handle that correctly. 希望您使用的压缩机能够正确处理该问题。
  5. Classes and ID's should be specifically named to somewhat match where they are in your system. 应该对类和ID进行专门命名,以使其在系统中的位置有些匹配。 In other words, classes at www.example.com can be called 换句话说,可以调用www.example.com上的类
    .table . .table If you need a new table class at www.example.com/foo/... it should be named something like .foo-table and should apply and be used by things 如果您需要在www.example.com/foo / ...上创建一个新的表类,则应将其命名为.foo-table类的东西,并应被事物使用并使用
    below it. 在它下面。
  6. All css should be in separate files from your html document and all modifications to css should preferably be done by changing/adding classes and not by changing specific style attributes at the element level. 所有css应该与html文档位于不同的文件中,并且对css的所有修改最好应通过更改/添加类而不是通过在元素级别更改特定样式属性来完成。

I'm the only one I know that does it this way, but everyone who has used it since has said, "Damn, that makes it easy to find my CSS errors!" 我是唯一知道这样做的人,但是此后使用过它的每个人都说:“该死,这很容易找到我的CSS错误!”

Ok. 好。 I don't know if there is any article discuss about this. 我不知道是否有任何文章对此进行讨论。 But for me style 1 is better in term of readability and debugging. 但是对我来说,样式1在可读性和调试方面更好。

just my 2 cent 只有我的2美分

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

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