Home >Web Front-end >HTML Tutorial >Mozilla recommended CSS attribute writing order and naming rules_html/css_WEB-ITnose
传说中的Mozilla推荐
Java代码
来源:
Java代码
在怿飞’s Blog的这篇文章里,又将上面的属性分成了三组:显示属性、自身属性和文本属性。在回复里,inG补充这还和浏览器的解析过程有关:浏览器先对DOM定位,然后解析自身属性,接着再解析内部对象。(没找到相关的英文资料,有知情者还望告知)
在Mozilla官方,其实并没有推荐任何CSS书写顺序。很可能是某个开发者在阅读fantasai的这篇文章 mozilla.org Markup Reference 时,顺便对fantasai的CSS源文件产生了兴趣,因此才有了上面的发现。
字母排序
NETTUTS上时不时有些好文章,这不,前不久,Trevor Davis就分享了一篇:5 Ways to Instantly Write Better CSS. 这篇文章中,推荐CSS的属性按字母排序。
优点是:简单,任何人只要遵守,一看就明白。
缺点是:太简单,缺乏逻辑性。比如position, left, top等,这种紧关联的属性,如果都按字母排序,书写和维护起来都不方便。Andy Ford推荐的排序
Andy Ford是HTML和CSS方面的专家,最近写了一篇文章:Order of the Day: CSS Properties. 文章推荐的CSS书写顺序为:
Java代码
Andy’s order is generally consistent with the order recommended by fantasai, but the details are more operable.
There is also a very lively discussion thread on SitePoint: How do you order your properties within a declaration block?
I like the writing order of fantasai and Andy, but in fantasai's order, the "self" attribute It's a little vague, and Andy's is too detailed to remember. I think we can learn from the classification of CSS properties in the CSS 2.1 Specification and slightly adjust Andy’s order:
1. Properties that affect document flow (such as: display, position, float, clear, visibility, table- layout, etc.)
2. Properties of its own box model (such as: width, height, margin, padding, border, etc.)
3. Typesetting-related properties (such as: font, line-height, text-align, text- indent, vertical-align, etc.)
4. Decorative attributes (such as: color, background, opacity, cursor, etc.)
5. Content-generated attributes (such as: content, list-style, quotes, etc.)
Things are never that simple, such as the following problems:
1. How to deal with shorthand? For example, border: 1px solid red; where border-width is related to the box model, but border-color is decorative. How to organize it?
2. Considering the skin-changing function, should color, background, border-color and other color-related items be put together? to facilitate future modifications.
3. How to deal with hacks? Is it better to put it alone at the end of the css file, or next to the hack attributes?
4. When maintaining a colleague’s CSS file, how should I comment on the newly added or modified attributes? How to write?
5. Also, considering CSS Sprite, all background image selectors are placed together? But that’s beyond the scope of this article: the order and organization of properties within CSS selectors.
6. Further discussion is: the structural organization within CSS files, and the organization of multiple CSS files.
CSS naming rules:
Like other programs, CSS has the concept of scope, with global and class-local effects.
For example:
p{background:#f00;}/* Scope: global*/
.div p{color:#000;}/* Scope: In div class */
Introducing several writing methods and weight comparison of Css
1) Tag: weight value is 0,0,0,1
2) Class: The weight is 0,0,1,0
3) Attribute selection: The weight is 0,0,1,1
4) ID: The weight is 0, 1,0,0
5) The weight of important is the highest 1,0,0,0
I believe that when you are writing Css, when the project is larger and the content is more , naming is a headache, and a block needs to express the styles of different states. Mastering the naming rules is a powerful tool, which can make your work more effective. It is roughly as follows: (Reprinted from: http://www.cssforest.org/blog /index.php?id=143, you can go here to read more technical articles)
To avoid when the status changes When the name loses its meaning, the most common ones are the class names used for layout, such as "left" and "right". When the left column is no longer the left column, the name "left" has no practical meaning. This goes against our recommendation of "naming should be meaningful", and using serial numbers is even more problematic. It seems to be true, but there has been a problem that has bothered me for a long time. If the same module appears more than once on a page, and the details are different, what should the name of the subsequent ones be called? Aren't "one" and "two" serial numbers? In fact, the situation we want to avoid is that when the state (performance) changes, the corresponding defined class name will not lose its meaning.
There are several situations where the so-called status (performance) changes:
1. The HTML remains unchanged and the style definition changes. If the name uses a name that represents a certain state, such as "red", "font14", etc., it will definitely cause inconsistency between the definition and the naming, which will have a relatively large impact on subsequent consequences.
2. The style definition remains unchanged, but the HTML changes. HTML changes mean that the class name can be replaced, that is, if the class name uses a name that represents a certain state, it will be more conducive to modification.
3. Both style definition and HTML have changed. Just consider not to have the consequences of the first scenario.
But the actual situation is not just one kind of situation, but more often it appears in a mixture.
Rules
[Module Prefix] _ Type_ (Function | Status) n _ [Position n]
Legend description:
* (Required): Required exist.
* [Optional]: You can choose according to your needs.
* |: Select one from more than one.
* n: There can be multiple.
Noun description:
Module prefix
The prefix used when defining a module.
Type
Defines the content type of the class. Such as input boxes, text, paragraphs, etc.
Function
Defines the function of the class, which is used to supplement the type.
Status
Defines the status of the class, used to supplement the type.
Position
Define the position used by the class, such as homepage, navigation, etc. The use of words like left and right is not excluded, but it should be avoided as much as possible.
* Each item can have its own abbreviation list, and the abbreviations of the same name should be as unified as possible.
* The words selected should be words that do not express a certain state (such as color, size, etc.) in detail to avoid the name losing its meaning when the state changes.
* Consists of lowercase letters (a-z) and numbers (0-9) that do not start with a number.
* Ensure the reusability of the class (.class) and the uniqueness of the object (#id). Avoid using reserved words for id.
Example:
Java code
Chinese explanation naming
Text input box.input_tx Paragraph text color .c_tx_p
Password input box.input_pw Album pop-up setting layer.pop_set_photo
Login password input box .input_pw_login Log setting successful prompt.hint_suc_blogset
Text color.c_tx Public prompt.hint_gb
Asking a few simple questions can help us complete the naming:
1. "What type of Definition? "?? is an input box, input.
2. "Type supplementary explanation"?? If the description of a word is unclear, then supplementary explanation of the type, text input box, input_tx.
3. “Where to use?”?? Where is the location to be used? The search text input box on the homepage, input_search_index.
Combined with "modularization" related methods to define, in fact, there are not many names that need to be defined. For example: "hint_tx" represents the text definition of the prompt module, and "hit_tx_hint" represents the definition of text emphasis in the prompt. As for whether to change the color or make it bold, it depends on the needs of different prompt modules.