Writing efficient CSS for use in the Mozilla UI

Source: Internet
Author: User

The following document outlines rules for optimizing CSS files for use in the Mozilla UI. the first section is a general discussion of how the Mozilla style system categorizes rules. with an understanding of this system, the following sections in guidelines for writing rules that take advantage of Mozilla's style system implementation.

How the style system breaks up rules

The style system breaks rules up into four primary categories. It is critical to understand these categories, as they are the first line of defense as far as rule matching is concerned. I use the termKey SelectorIn the paragraphs that follow. the key selector is defined to be the rightmost part of the selector (the part that matches the element being matched, rather than one of its ancestors ). for example, in the rule:

A img, div> P, H1 + [title] {}

The key selectors are "IMG", "P", and "[title]".

Id rules

The first category consists of those rules that have an id selector as their key selector.

Examples

 
Button # backbutton {}/* This is an ID-categorized Rule */# urlbar [type = "AutoComplete"] {}/ * This is an ID-categorized Rule */treeitem> treerow> treecell # mycell: active {}/* This is an ID-categorized Rule */

Class rules

If a rule has a class specified as its key selector, then it falls into this category.

Examples

Button. toolbarbutton {}/* a class-based rule */. fancytext {}/* a class-based rule */menuitem>. menu-left [checked = "true"] {}/ * a class-based rule */

Tag rules

If no class or ID is specified as the key selector, then the next potential category for a rule is the tag category. if a rule has a tag specified as its key selector, then the rule falls into this category.

Examples

 
TD {}/* a tag-based rule */treeitem> treerow {}/* a tag-based rule */input [type = "checkbox"] {}/* a tag -based rule */

Universal rules

All other rules fall into this category.

Examples

  [Hidden = "true"] {}/* a universal rule */* {}/* a universal rule */tree> [collapsed = "true"] {}/* universal rule */

How the style system matches rules

The style system matches a rule by starting with the rightmost selector and moving to the left through the rule's selectors. as long as your little subtree continues to check out, the style system will continue moving to the left until it either matches the rule or bails out because of a mismatch.

Your first line of defense is the rule filtering that occurs based on the type of the key selector. the purpose of this categorization is to filter out rules so that you don't even have to waste time trying to match them. this is the key to dramatically increasing performance. the fewer rules that you even have to check for a given element, the faster style resolution will be. as an example, if your element has an ID, then only ID rules that match your element's ID will be checked. only class rules for a class found on your element will be checked. only tag rules that match your tag will be checked. universal rules will always be checked.

Guidelines for efficient CSS

Avoid universal rules!

Make sure a rule doesn' t end up in the universal category!

Don't qualify ID-categorized rules with tag names or classes

If you have a style rule that has an ID selector as its key selector, don't bother also adding the tag name to the rule. IDs are unique, so you're slowing down the matching for no real reason. (an exception to this wocould be when you wish to change the class of an element dynamically in order to apply different styles to the element under different situations, but where you also wish to share the same class with other elements .)

    • Bad-Button # backbutton {}
    • Bad-. Menu-left # newmenuicon {}
    • Good-# Backbutton {}
    • Good-# Newmenuicon {}

Don't qualify class-categorized rules with tag names

Similar to the rule above, all of our classes will be unique. one convention you can use is to include the tag name in the class name (however, this may cost some flexibility in that if design changes are made to change the tag to a different type, the class names must be changed as well -- it is even better to choose strictly semantic names, as such flexibility is one of the aims of separate stylesheets ).

    • Bad-Treecell. indented {}
    • Good-. Treecell-indented {}
    • Best-. Hierarchy-deep {}

Try to put rules into the most specific category you can!

The single biggest cause of slowdown in our system is that we have too rule rules in the tag category. by adding classes to our elements, we can further subdivide these rules into class categories, and then we no longer waste time trying to match as your rules for a given tag.

    • Bad-Treeitem [mailfolder = "true"]> treerow> treecell {}
    • Good-. Treecell-mailfolder {}

Avoid the descendant selector!

The descendant selector is the most expensive selector in CSS. it is dreadfully expensive, especially if a rule using the selector is in the tag or universal category. frequently what is really desired is the Child selector. the use of the descendant selector is banned in Ui CSS without the explicit approval of your skin's module owner.

    • Bad-Treehead treerow treecell {}
    • Better, but still bad (see next guideline )-Treehead> treerow> treecell {}

Tag-categorized rules shoshould never contain a child selector!

Avoid using the child selector with Tag-categorized rules. You will dramatically increase the matching time (especially if the rule is likely to be matched more often than not) for all occurrences of that element.

    • Bad-Treehead> treerow> treecell {}
    • Best-. Treecell-header {}

Question all usages of the Child selector!

Be careful about using the child selector. if you can come up with a way to avoid having to use it, do so. in particle, the Child selector is frequently used with RDF trees and menus like so.

    • Bad-Treeitem [isimapserver = "true"]> treerow>. Tree-folderpane-Icon {}

Remember that attributes from RDF can be duplicated in a template! Take advantage of this fact to duplicate RDF properties on child XUL elements that wish to change based off that attribute.

    • Good-. Tree-folderpane-Icon [isimapserver = "true"] {}

Rely on inheritance!

Learn which properties inherit, and allow them to do so! We have explicitly set up XUL widgetry so that you can put list-style-image (just one example) or font rules on the parent tag, and it will filter in to the anonymous content. you don't have to waste time writing a rule that talks directly to the anonymous content.

    • Bad-# Bookmarkmenuitem>. Menu-left {list-style-image: URL (blah );}
    • Good-# Bookmarkmenuitem {list-style-image: URL (blah );}

In the above example, the desire to style the anonymous content (without understanding that list-style-image inherits) resulted in a rule that was in the class category, when this rule really shocould have ended up being in the most specific category of all, the ID category.

Remember, especially with anonymous content, that they all have the same classes! The bad rule above causes the icon of every menu to be checked to see if it is contained in the bookmarks menu item. this is hideously expensive (since there are checking menus); this rule never shold have even been checked by any menu other than the bookmarks menu.

Use-moz-image-region!

Putting a bunch of images into a single image file and selecting them-Moz-image-regionPerforms significantly better than putting each image into its own file.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.