Writing efficient CSS for use in the Mozilla UI

本文提供了一套针对Mozilla UI的CSS代码优化指南,详细解释了如何利用Mozilla样式系统的实现来编写高效规则,并介绍了样式系统如何匹配规则以及各类规则的作用。
[size=large][size=large]This document provides guidelines for optimizing CSS code for use in the Mozilla UI. It applies to CSS for XUL code, not for general-purpose HTML pages; see the main CSS section for information on general-purpose CSS.

The first section is a general discussion of how the Mozilla style system categorizes rules. The following sections contain 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:
1.ID Rules
2.Class Rules
3.Tag Rules
4.Universal Rules

It is critical to understand these categories, as they are the fundamental building blocks of rule matching.

I use the term key selector in the paragraphs that follow. The key selector is the last part of the selector (the part that matches the element being matched, rather than its ancestors).

For example, in the rule…



view plaincopy to clipboardprint?
01.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.


Example


view plaincopy to clipboardprint?
01.button#backButton {…} /* This is an ID-categorized rule */
02.#urlBar[type="autocomplete"] {…} /* This is an ID-categorized rule */
03.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.


Example


view plaincopy to clipboardprint?
01.button.toolbarButton {…} /* A class-based rule */
02..fancyText {…} /* A class-based rule */
03.menuitem > .menu-left[checked="true"] {…} /* A class-based rule */


Tag Rules

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


Example


view plaincopy to clipboardprint?
01.td {…} /* A tag-based rule */
02.treeitem > treerow {…} /* A tag-based rule */
03.input[type="checkbox"] {…} /* A tag-based rule */


Universal Rules

All other rules fall into this category.


Example


view plaincopy to clipboardprint?
01.[hidden="true"] {…} /* A universal rule */
02.* {…} /* A universal rule */
03.tree > [collapsed="true"] {…} /* A universal rule */


How the Style System Matches Rules

The style system matches rules by starting with the key selector, then moving to the left (looking for any ancestors in the rule’s selector). As long as the selector’s subtree continues to check out, the style system continues moving to the left until it either matches the rule, or abandons because of a mismatch.

The most fundamental concept to learn is this rule filtering. The categories exist in order to filter out irrelevant rules (so the style system doesn’t waste time trying to match them).

This is the key to dramatically increasing performance. The fewer rules required to check for a given element, the faster style resolution will be.

For example, if an element has an ID, then only ID rules that match the element’s ID will be checked. Only Class Rules for a class found on the element will be checked. Only Tag Rules that match the 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 Rules with tag names or classes

If a rule has an ID selector as its key selector, don’t add the tag name to the rule. Since IDs are unique, adding a tag name would slow down the matching process needlessly.

Exception: When it’s desirable to change the class of an element dynamically in order to apply different styles in different situations, but the same class is going to be shared with other elements.
BAD button#backButton {…} BAD .menu-left#newMenuIcon {…} GOOD #backButton {…} GOOD #newMenuIcon {…}

Don’t qualify Class Rules with tag names

The previous concept also applies here. All class names are unique.

One convention you can use is to include the tag name in the class name. However, this may cost some flexibility; if design changes are made to the tag, the class names must be changed as well. (It’s best 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 {…}

Use the most specific category possible

The single biggest cause of slowdown is too many rules in the Tag Category. By adding classes to our elements, we can further subdivide these rules into Class Categories, which eliminates time spent trying to match 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 the selector is in the Tag or Universal Category.

Frequently, what is really desired is the child selector. Using the descendant selector is banned in User Interface 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 Category rules should never contain a child selector

Avoid using the child selector with Tag Category rules. This will dramatically lengthen the match time (especially if the rule is likely to be matched) for all occurrences of that element.
BAD treehead > treerow > treecell {…} GOOD .treecell-header {…}

Question all usages of the child selector

Exercise caution when using the child selector. Avoid it if you can.

In particular, the child selector is frequently used with RDF trees and menus like so:
BAD treeitem[IsImapServer="true"] > treerow > .tree-folderpane-icon {…}
Remember that REF attributes can be duplicated in a template! Take advantage of this. Duplicate RDF properties on child XUL elements in order to change them based on the attribute.
GOOD .tree-folderpane-icon[IsImapServer="true"] {…}

Rely on inheritance

Learn which properties inherit, and allow them to do so!

For example, XUL widgets are explicitly set up such that a parent’s list-style-image or font rules will filter down to anonymous content. It’s not necessary to waste time on rules that talk directly to 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 anonymous content (without leveraging the inheritance of list-style-image) resulted in a rule that was in the Class Category, when the rule should have ended up in the ID Category—the most specific category of all!

Remember: Elements all have the same classes—especially anonymous content!

The above “bad” rule forces every menu’s icons to be tested for containment within the bookmarks menu item. Since there are many menus, this is extraordinarily expensive. Instead, the “good” rule limits the testing to the bookmarks menu.


Use -moz-image-region!

Putting a bunch of images into a single image file and selecting them with -moz-image-region performs significantly better than putting each image into its own file.


Use scoped stylesheets

If you specify a stylesheet as an XBL resource, the styles only apply to the bound elements and their anonymous content. This reduces the inefficiency of universal rules and child selectors because there are fewer elements to consider.
[size=medium][/size][/size][size=x-large][/size][/size]
学生社团系统-学生社团“一站式”运营管理平台-学生社团管理系统-基于SSM的学生社团管理系统-springboot学生社团管理系统.zip-Java学生社团管理系统开发实战-源码 更多学生社团系统: SpringBoot+Vue学生社团“一站式”运营管理平台源码(活动管理+成员考核+经费审批) Java学生社团管理系统开发实战:SSM升级SpringBoot(招新报名+场地预约+数据看板) 基于SpringSecurity的社团管理APP(移动端签到+权限分级+消息推送) 企业级社团数字化平台解决方案(SpringBoot+Redis缓存+Elasticsearch活动搜索) 微信小程序社团服务系统开发(活动直播+社团文化墙+成员互动社区) SpringBoot社团核心源码(多角色支持+工作流引擎+API接口开放) AI赋能社团管理:智能匹配兴趣标签+活动热度预测+成员贡献度分析(附代码) 响应式社团管理平台开发(PC/移动端适配+暗黑模式+无障碍访问) 完整学生社团系统源码下载(SpringBoot3+Vue3+MySQL8+Docker部署) 高校垂直领域社团平台:百团大战系统+社团星级评定+跨校活动联盟 适用对象:本代码学习资料适用于计算机、电子信息工程、数学等专业正在做毕设的学生,需要项目实战练习的学习者,也适用于课程设计、期末大作业。 技术栈:前端是vue,后端是springboot,项目代码都经过严格调试,代码没有任何bug! 核心管理:社团注册、成员管理、权限分级 活动运营:活动发布、报名签到、场地预约 资源服务:经费申请、物资管理、文档共享 数据分析:成员活跃度、活动效果评估、社团影响力排名
请重新以技术写作风格重写以下信件。 Dear Sir/Madam, I am writing to inquire about the status of my application for the position of Software Engineer at your company. I applied for the position on February 1st and have not heard back from your team regarding the status of my application. I am very interested in this position and believe that my skills and experience make me an ideal candidate for the role. I have over 5 years of experience in software development, with a strong focus on web applications and cloud computing. I also have experience working in Agile development environments and am familiar with a variety of programming languages, including Java, Python, and PHP. I would appreciate any information you can provide regarding the status of my application. Thank you for your time and consideration. Sincerely, [Your Name] 尊敬的先生/女士: 我写信是为了了解我在贵公司软件工程师职位申请的情况。我在2月1日申请了该职位,但至今未收到来自贵公司团队关于我的申请状态的反馈。 我对这个职位非常感兴趣,并认为我的技能和经验使我成为该职位的理想候选人。我有超过5年的软件开发经验,主要集中在Web应用和云计算方面。我还有在敏捷开发环境中工作的经验,并熟悉各种编程语言,包括Java、Python和PHP。 我将感激您提供关于我的申请状态的任何信息。感谢您抽出时间考虑我的申请。 诚挚地, [您的姓名]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值