手机
当前位置:查字典教程网 >网页设计 > Div+Css教程 >CSS对Web页面载入效率的影响分析总结
CSS对Web页面载入效率的影响分析总结
摘要:我们罗列了十几条相关的知识与注意点,大家可以系统的探讨一下,让我们编写的Web页面打开更加流畅。请不要告诉我,你看不懂E文,只是你不愿意看!...

我们罗列了十几条相关的知识与注意点,大家可以系统的探讨一下,让我们编写的Web页面打开更加流畅。

请不要告诉我,你看不懂E文,只是你不愿意看!!!

1、Howthestylesystembreaksuprules

Thestylesystembreaksrulesupintofourprimarycategories.Itiscriticaltounderstandthesecategories,astheyarethefirstlineofdefenseasfarasrulematchingisconcerned.Iusethetermkeyselectorintheparagraphsthatfollow.Thekeyselectorisdefinedtobetherightmostoccurrenceofanidselector,aclassselector,oratagselector.

1.1、IDRules

ThefirstcategoryconsistsofthoserulesthathaveanIDselectorastheirkeyselector.

button#backButton{}/*ThisisanID-categorizedrule*/

#urlBar[type="autocomplete"]{}/*ThisisanID-categorizedrule*/

treeitem>treerow>treecell#myCell:active{}/*ThisisanID-categorizedrule*/

1.2、ClassRules

Ifarulehasaclassspecifiedasitskeyselector,thenitfallsintothiscategory.

button.toolbarButton{}/*Aclass-basedrule*/

.fancyText{}/*Aclass-basedrule*/

menuitem>.menu-left[checked="true"]{}/*Aclass-basedrule*/

1.3、TagRules

IfnoclassorIDisspecifiedasthekeyselector,thenthenextpotentialcategoryforaruleisthetagcategory.Ifarulehasatagspecifiedasitskeyselector,thentherulefallsintothiscategory.

td{}/*Atag-basedrule*/

treeitem>treerow{}/*Atag-basedrule*/

input[type="checkbox"]{}/*Atag-basedrule*/

1.4、UniversalRules

Allotherrulesfallintothiscategory.

:table{}/*Auniversalrule*/

[hidden="true"]{}/*Auniversalrule*/

*{}/*Auniversalrule*/

tree>[collapsed="true"]{}/*Auniversalrule*/

2、HowtheStyleSystemMatchesRules

Thestylesystemmatchesarulebystartingwiththerightmostselectorandmovingtotheleftthroughtherule'sselectors.Aslongasyourlittlesubtreecontinuestocheckout,thestylesystemwillcontinuemovingtotheleftuntiliteithermatchestheruleorbailsoutbecauseofamismatch.

Yourfirstlineofdefenseistherulefilteringthatoccursbasedonthetypeofthekeyselector.Thepurposeofthiscategorizationistofilteroutrulessothatyoudon'tevenhavetowastetimetryingtomatchthem.Thisisthekeytodramaticallyincreasingperformance.Thefewerrulesthatyouevenhavetocheckforagivenelement,thefasterstyleresolutionwillbe.Asanexample,ifyourelementhasanID,thenonlyIDrulesthatmatchyourelement'sIDwillbechecked.Onlyclassrulesforaclassfoundonyourelementwillbechecked.Onlytagrulesthatmatchyourtagwillbechecked.Universalruleswillalwaysbechecked.

3、GuidelinesforEfficientCSS

3.1、AvoidUniversalRules!

Makesurearuledoesn'tendupintheuniversalcategory!

3.2、Don'tqualifyID-categorizedruleswithtagnamesorclasses

IfyouhaveastylerulethathasanIDselectorasitskeyselector,don'tbotheralsoaddingthetagnametotherule.IDsareunique,soyou'reslowingdownthematchingfornorealreason.

复制代码 代码如下:

BAD-button#backButton{}

BAD-.menu-left#newMenuIcon{}

GOOD-#backButton{}

GOOD-#newMenuIcon{}

3.3、Don'tqualifyclass-categorizedruleswithtagnames

Similartotheruleabove,allofourclasseswillbeunique.Theconventionyoushoulduseistoincludethetagnameintheclassname.

复制代码 代码如下:

BAD-treecell.indented{}

GOOD-.treecell-indented{}

3.4、Trytoputrulesintothemostspecificcategoryyoucan!

Thesinglebiggestcauseofslowdowninoursystemisthatwehavetoomanyrulesinthetagcategory.Byaddingclassestoourelements,wecanfurthersubdividetheserulesintoclasscategories,andthenwenolongerwastetimetryingtomatchasmanyrulesforagiventag.

BAD-treeitem[mailfolder="true"]>treerow>treecell{}

GOOD-.treecell-mailfolder{}

3.5、Avoidthedescendantselector!

ThedescendantselectoristhemostexpensiveselectorinCSS.Itisdreadfullyexpensive,especiallyifaruleusingtheselectorisinthetagoruniversalcategory.Frequentlywhatisreallydesiredisthechildselector.TheuseofthedescendantselectorisbannedinUICSSwithouttheexplicitapprovalofyourskin'smoduleowner.

BAD-treeheadtreerowtreecell{}

BETTER,BUTSTILLBAD(seenextguideline)-treehead>treerow>treecell{}

3.6、Tag-categorizedrulesshouldnevercontainachildselector!

Avoidusingthechildselectorwithtag-categorizedrules.Youwilldramaticallyincreasethematchingtime(especiallyiftheruleislikelytobematchedmoreoftenthannot)foralloccurrencesofthatelement.

BAD-treehead>treerow>treecell{}

BEST-.treecell-header{}

3.7、Questionallusagesofthechildselector!

Becarefulaboutusingthechildselector.Ifyoucancomeupwithawaytoavoidhavingtouseit,doso.Inparticular,thechildselectorisfrequentlyusedwithRDFtreesandmenuslikeso.

BAD-treeitem[IsImapServer="true"]>treerow>.tree-folderpane-icon{}

RememberthatattributesfromRDFcanbeduplicatedinatemplate!TakeadvantageofthisfacttoduplicateRDFpropertiesonchildXULelementsthatwishtochangebasedoffthatattribute.

GOOD-.tree-folderpane-icon[IsImapServer="true"]{}

3.8、Relyoninheritance!

Learnwhichpropertiesinherit,andallowthemtodoso!WehaveexplicitlysetupXULwidgetrysothatyoucanputlist-style-image(justoneexample)orfontrulesontheparenttag,anditwillfilterintotheanonymouscontent.Youdon'thavetowastetimewritingarulethattalksdirectlytotheanonymouscontent.

BAD-#bookmarkMenuItem>.menu-left{list-style-image:url(blah);}

GOOD-#bookmarkMenuItem{list-style-image:url(blah);}

Intheaboveexample,thedesiretostyletheanonymouscontent(withoutunderstandingthatlist-style-imageinherits)resultedinarulethatwasintheclasscategory,whenthisrulereallyshouldhaveendedupbeinginthemostspecificcategoryofall,theIDcategory.

Remember,especiallywithanonymouscontent,thattheyallhavethesameclasses!Thebadruleabovecausestheiconofeverymenutobecheckedtoseeifitiscontainedinthebookmarksmenuitem.Thisishideouslyexpensive(sincetherearemanymenus);thisrulenevershouldhaveevenbeencheckedbyanymenuotherthanthebookmarksmenu.

3.9、Use-moz-image-region!

Puttingabunchofimagesintoasingleimagefileandselectingthemwith-moz-image-regionperformssignificantlybetterthanputtingeachimageintoitsownfile.

OriginalDocumentInformation-Author:DavidHyatt

【CSS对Web页面载入效率的影响分析总结】相关文章:

CSS中的!important属性用法

常用CSS缩写语法总结

用CSS实现表格背景颜色渐变效果

8个对学习CSS3非常实用的帮助手册

如何用CSS自定义鼠标显示的形状

CSS分页数字放大效果

CSS 控制因Html页面高度导致抖动的问题解决方法

CSS基础教程: CSS的属性缩写

CSS-float的属性

CSS代码当前页效果的实现

精品推荐
分类导航