search
HomeWeb Front-endHTML TutorialMozilla recommended CSS attribute writing order and naming rules_html/css_WEB-ITnose

传说中的Mozilla推荐 

Java代码  

  1. /* mozilla.org Base Styles 
  2.  * maintained by fantasai 
  3.  */  
  4. /* Suggested order: 
  5.  * display 
  6.  * list-style 
  7.  * position 
  8.  * float 
  9.  * clear 
  10.  * width 
  11.  * height 
  12.  * margin 
  13.  * padding 
  14.  * border 
  15.  * background 
  16.  * color 
  17.  * font 
  18.  * text-decoration 
  19.  * text-align 
  20.  * vertical-align 
  21.  * white-space 
  22.  * other text 
  23.  * content 
  24.  * 
  25.  */  
  26. ...  



来源:

Java代码  

  1. http://www.mozilla.org/css/base/content.css  



在怿飞’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代码  

  1.  1. Display & Flow  
  2.  2. Positioning  
  3.  3. Dimensions  
  4.  4. Margins, Padding, Borders, Outline  
  5.  5. Typographic Styles  
  6.  6. Backgrounds  
  7.  7. Opacity, Cursors, Generated Content  
  8.   
  9. 例子:  
  10.   
  11. el {  
  12.     display: ;  
  13.     visibility: ;  
  14.     float: ;  
  15.     clear: ;  
  16.   
  17.     position: ;  
  18.     top: ;  
  19.     right: ;  
  20.     bottom: ;  
  21.     left: ;  
  22.     z-index: ;  
  23.   
  24.     width: ;  
  25.     min-width: ;  
  26.     max-width: ;  
  27.     height: ;  
  28.     min-height: ;  
  29.     max-height: ;  
  30.     overflow: ;  
  31.   
  32.     margin: ;  
  33.     margin-top: ;  
  34.     margin-right: ;  
  35.     margin-bottom: ;  
  36.     margin-left: ;  
  37.   
  38.     padding: ;  
  39.     padding-top: ;  
  40.     padding-right: ;  
  41.     padding-bottom: ;  
  42.     padding-left: ;  
  43.   
  44.     border: ;  
  45.     border-top: ;  
  46.     border-right: ;  
  47.     border-bottom: ;  
  48.     border-left: ;  
  49.   
  50.     border-width: ;  
  51.     border-top-width: ;  
  52.     border-right-width: ;  
  53.     border-bottom-width: ;  
  54.     border-left-width: ;  
  55.   
  56.     border-style: ;  
  57.     border-top-style: ;  
  58.     border-right-style: ;  
  59.     border-bottom-style: ;  
  60.     border-left-style: ;  
  61.   
  62.     border-color: ;  
  63.     border-top-color: ;  
  64.     border-right-color: ;  
  65.     border-bottom-color: ;  
  66.     border-left-color: ;  
  67.   
  68.     outline: ;  
  69.     list-style: ;  
  70.   
  71.     table-layout: ;  
  72.     caption-side: ;  
  73.     border-collapse: ;  
  74.     border-spacing: ;  
  75.     empty-cells: ;  
  76.   
  77.     font: ;  
  78.     font-family: ;  
  79.     font-size: ;  
  80.     line-height: ;  
  81.     font-weight: ;  
  82.     text-align: ;  
  83.     text-indent: ;  
  84.     text-transform: ;  
  85.     text-decoration: ;  
  86.     letter-spacing: ;  
  87.     word-spacing: ;  
  88.     white-space: ;  
  89.     vertical-align: ;  
  90.     color: ;  
  91.   
  92.     background: ;  
  93.     ;  
  94.     background-image: ;  
  95.     background-repeat: ;  
  96.     background-position: ;  
  97.   
  98.     opacity: ;  
  99.     cursor: ;  
  100.     content: ;  
  101.     quotes: ;  
  102.     }  



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

  1. Module prefix:
  2. * Pop up
  3. * Public global, gb
  4. * Title title, tit
  5. * Hint
  6. * Menu menu
  7. * Information info
  8. * Preview pvw
  9. * Tips tips
  10. * Navigation nav
  11. Type:
  12. * button bt
  13. * text tx
  14. * paragraph p
  15. * icon icon
  16. * input input
  17. * Color color,c
  18. * Background bg
  19. * Border bor
  20. Function:
  21. * Setting set
  22. * Add add
  23. * Delete del
  24. * Operation op
  25. * Password pw
  26. * Import inc
  27. Status:
  28. * Success suc
  29. * Failure Lost
  30. * Transparent tran
  31. Position:
  32. * public gb
  33. * border bor
  34. * paragraph p
  35. * popup
  36. * title title,tit
  37. * menu menu
  38. * content cont
  39. * navigation nav



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.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.