CSS编写的网页打开流畅相关知识与注意点
网站建设 2023-01-28 21:38www.1681989.com免费网站
我们都熟知JavaScript可能会对Web页面的加载与显示产生较大的影响,我们常常关注JavaScript是不是会占用客户端较多的资源,大部分都会忽略的一件有趣的事情,CSS也会对Web页面载入的效率产生影响。
我们罗列了十几条相关的知识与注意点,大家可以系统的探讨一下,让我们编写的Web页面打开更加流畅。
请不要告诉我,你看不懂E文,只是你不愿意看!!!
1、Howthestylesystembreaksuprules
Thestylesystembreaksrulesuptofourprimarycategories.Itiscriticaltounderstandthesecategories,astheyarethefirstleofdefenseasfarasrulematchgisconcerned.Iusethetermkeyselectortheparagraphsthatfollow.Thekeyselectorisdefedtobetherightmostourrenceofanidselector,aclassselector,oratagselector.
1.1、IDRules
ThefirstcategoryconsistsofthoserulesthathaveanIDselectorastheirkeyselector.
button#backButton{}/ThisisanID-categorizedrule/
#urlBar[type="aulete"]{}/ThisisanID-categorizedrule/
treeitem>treerow>treecell#myCell:active{}/ThisisanID-categorizedrule/
1.2、ClassRules
Ifarulehasaclassspecifiedasitskeyselector,thenitfallstothiscategory.
button.toolbarButton{}/Aclass-basedrule/
.fancyText{}/Aclass-basedrule/
menuitem>.menu-left[checked="true"]{}/Aclass-basedrule/
1.3、TagRules
IfnoclassorIDisspecifiedasthekeyselector,thenthenextpotentialcategoryforaruleisthetagcategory.Ifarulehasatagspecifiedasitskeyselector,thentherulefallstothiscategory.
td{}/Atag-basedrule/
treeitem>treerow{}/Atag-basedrule/
put[type="checkbox"]{}/Atag-basedrule/
1.4、UniversalRules
Allotherrulesfalltothiscategory.
:table{}/Auniversalrule/
[hidden="true"]{}/Auniversalrule/
{}/Auniversalrule/
tree>[collapsed="true"]{}/Auniversalrule/
2、HowtheStyleSystemMatchesRules
Thestylesystemmatchesarulebystartgwiththerightmostselectorandmovgtotheleftthroughtherule'sselectors.Aslongasyourlittlesubtreecontuestocheckout,thestylesystemwillcontuemovgtotheleftuntiliteithermatchestheruleorbailsoutbecauseofamismatch.
Yourfirstleofdefenseistherulefiltergthatoursbasedonthetypeofthekeyselector.Thepurposeofthiscategorizationistofilteroutrulessothatyoudon'tevenhavetowastetimetrygtomatchthem.Thisisthekeytodramaticallycreasgperformance.Thefewerrulesthatyouevenhavetocheckforagivenelement,thefasterstyleresolutionwillbe.Asanexample,ifyourelementhasanID,thenonlyIDrulesthatmatchyourelement'sIDwillbechecked.Onlyclassrulesforaclassfoundonyourelementwillbechecked.Onlytagrulesthatmatchyourtagwillbechecked.Universalruleswillalwaysbechecked.
3、GuidelesforEfficientCSS
3.1、AvoidUniversalRules!
Makesurearuledoesn'tenduptheuniversalcategory!
3.2、Don'tqualifyID-categorizedruleswithtagnamesorclasses
IfyouhaveastylerulethathasanIDselectorasitskeyselector,don'tbotheralsoaddgthetagnametotherule.IDsareunique,soyou'reslowgdownthematchgfornorealreason.
BAD-button#backButton{}
BAD-.menu-left#newMenuIcon{}
GOOD-#backButton{}
GOOD-#newMenuIcon{}
3.3、Don'tqualifyclass-categorizedruleswithtagnames
Similartotheruleabove,allofourclasseswillbeunique.Theconventionyoushoulduseistocludethetagnametheclassname.
BAD-treecell.dented{}
GOOD-.treecell-dented{}
3.4、Tryutrulestothemostspecifiategoryyoucan!
Thesglebiggestcauseofslowdownoursystemisthatwehavetoomanyrulesthetagcategory.Byaddgclassestoourelements,wecanfurthersubdividetheserulestoclasscategories,andthenwenolongerwastetimetrygtomatchasmanyrulesforagiventag.
BAD-treeitem[mailfolder="true"]>treerow>treecell{}
GOOD-.treecell-mailfolder{}
3.5、Avoidthedescendantselector!
ThedescendantselectoristhemostexpensiveselectorCSS.Itisdreadfullyexpensive,especiallyifaruleusgtheselectoristhetagoruniversalcategory.Frequentlywhatisreallydesiredisthechildselector.TheuseofthedescendantselectorisbannedUICSSwithouttheexplicitapprovalofyoursk'smoduleowner.
BAD-treeheadtreerowtreecell{}
BETTER,BUTSTILLBAD(seenextguidele)-treehead>treerow>treecell{}
3.6、Tag-categorizedrulesshouldnevercontaachildselector!
Avoidusgthechildselectorwithtag-categorizedrules.Youwilldramaticallycreasethematchgtime(especiallyiftheruleislikelytobematchedmoreoftenthannot)forallourrencesofthatelement.
BAD-treehead>treerow>treecell{}
BEST-.treecell-header{}
3.7、Questionallusagesofthechildselector!
Becarefulaboutusgthechildselector.Ifyoucaneupwithawaytoavoidhavgtouseit,doso.Inparticular,thechildselectorisfrequentlyusedwithRDFtreesandmenuslikeso.
BAD-treeitem[IsImapServer="true"]>treerow>.tree-folderpane-icon{}
RememberthatattributesfromRDFcanbeduplicatedatemplate!TakeadvantageofthisfacttoduplicateRDFpropertiesonchildXULelementsthatwishtochangebasedoffthatattribute.
GOOD-.tree-folderpane-icon[IsImapServer="true"]{}
3.8、Relyonheritance!
Learnwhichpropertiesherit,andallowthemtodoso!WehaveexplicitlysetupXULwidgetrysothatyoucanputlist-style-image(justoneexample)orfontrulesontheparenttag,anditwillfiltertotheanonymouscontent.Youdon'thavetowastetimewritgarulethattalksdirectlytotheanonymouscontent.
BAD-#bookmarkMenuItem>.menu-left{list-style-image:url(blah);}
GOOD-#bookmarkMenuItem{list-style-image:url(blah);}
Intheaboveexample,thedesiretostyletheanonymouscontent(withoutunderstandgthatlist-style-imageherits)resultedarulethatwastheclasscategory,whenthisrulereallyshouldhaveendedupbegthemostspecifiategoryofall,theIDcategory.
Remember,especiallywithanonymouscontent,thattheyallhavethesameclasses!Thebadruleabovecausestheiconofeverymenutobecheckedtoseeifitiscontaedthebookmarksmenuitem.Thisishideouslyexpensive(scetherearemanymenus);thisrulenevershouldhaveevenbeencheckedbyanymenuotherthanthebookmarksmenu.
3.9、Use-moz-image-region!
Puttgabunchofimagestoasgleimagefileandselectgthemwith-moz-image-regionperformssignificantlybetterthanputtgeachimagetoitsownfile.
OrigalDocumentInformation-Author:DavidHyatt
我们罗列了十几条相关的知识与注意点,大家可以系统的探讨一下,让我们编写的Web页面打开更加流畅。
请不要告诉我,你看不懂E文,只是你不愿意看!!!
1、Howthestylesystembreaksuprules
Thestylesystembreaksrulesuptofourprimarycategories.Itiscriticaltounderstandthesecategories,astheyarethefirstleofdefenseasfarasrulematchgisconcerned.Iusethetermkeyselectortheparagraphsthatfollow.Thekeyselectorisdefedtobetherightmostourrenceofanidselector,aclassselector,oratagselector.
1.1、IDRules
ThefirstcategoryconsistsofthoserulesthathaveanIDselectorastheirkeyselector.
button#backButton{}/ThisisanID-categorizedrule/
#urlBar[type="aulete"]{}/ThisisanID-categorizedrule/
treeitem>treerow>treecell#myCell:active{}/ThisisanID-categorizedrule/
1.2、ClassRules
Ifarulehasaclassspecifiedasitskeyselector,thenitfallstothiscategory.
button.toolbarButton{}/Aclass-basedrule/
.fancyText{}/Aclass-basedrule/
menuitem>.menu-left[checked="true"]{}/Aclass-basedrule/
1.3、TagRules
IfnoclassorIDisspecifiedasthekeyselector,thenthenextpotentialcategoryforaruleisthetagcategory.Ifarulehasatagspecifiedasitskeyselector,thentherulefallstothiscategory.
td{}/Atag-basedrule/
treeitem>treerow{}/Atag-basedrule/
put[type="checkbox"]{}/Atag-basedrule/
1.4、UniversalRules
Allotherrulesfalltothiscategory.
:table{}/Auniversalrule/
[hidden="true"]{}/Auniversalrule/
{}/Auniversalrule/
tree>[collapsed="true"]{}/Auniversalrule/
2、HowtheStyleSystemMatchesRules
Thestylesystemmatchesarulebystartgwiththerightmostselectorandmovgtotheleftthroughtherule'sselectors.Aslongasyourlittlesubtreecontuestocheckout,thestylesystemwillcontuemovgtotheleftuntiliteithermatchestheruleorbailsoutbecauseofamismatch.
Yourfirstleofdefenseistherulefiltergthatoursbasedonthetypeofthekeyselector.Thepurposeofthiscategorizationistofilteroutrulessothatyoudon'tevenhavetowastetimetrygtomatchthem.Thisisthekeytodramaticallycreasgperformance.Thefewerrulesthatyouevenhavetocheckforagivenelement,thefasterstyleresolutionwillbe.Asanexample,ifyourelementhasanID,thenonlyIDrulesthatmatchyourelement'sIDwillbechecked.Onlyclassrulesforaclassfoundonyourelementwillbechecked.Onlytagrulesthatmatchyourtagwillbechecked.Universalruleswillalwaysbechecked.
3、GuidelesforEfficientCSS
3.1、AvoidUniversalRules!
Makesurearuledoesn'tenduptheuniversalcategory!
3.2、Don'tqualifyID-categorizedruleswithtagnamesorclasses
IfyouhaveastylerulethathasanIDselectorasitskeyselector,don'tbotheralsoaddgthetagnametotherule.IDsareunique,soyou'reslowgdownthematchgfornorealreason.
BAD-button#backButton{}
BAD-.menu-left#newMenuIcon{}
GOOD-#backButton{}
GOOD-#newMenuIcon{}
3.3、Don'tqualifyclass-categorizedruleswithtagnames
Similartotheruleabove,allofourclasseswillbeunique.Theconventionyoushoulduseistocludethetagnametheclassname.
BAD-treecell.dented{}
GOOD-.treecell-dented{}
3.4、Tryutrulestothemostspecifiategoryyoucan!
Thesglebiggestcauseofslowdownoursystemisthatwehavetoomanyrulesthetagcategory.Byaddgclassestoourelements,wecanfurthersubdividetheserulestoclasscategories,andthenwenolongerwastetimetrygtomatchasmanyrulesforagiventag.
BAD-treeitem[mailfolder="true"]>treerow>treecell{}
GOOD-.treecell-mailfolder{}
3.5、Avoidthedescendantselector!
ThedescendantselectoristhemostexpensiveselectorCSS.Itisdreadfullyexpensive,especiallyifaruleusgtheselectoristhetagoruniversalcategory.Frequentlywhatisreallydesiredisthechildselector.TheuseofthedescendantselectorisbannedUICSSwithouttheexplicitapprovalofyoursk'smoduleowner.
BAD-treeheadtreerowtreecell{}
BETTER,BUTSTILLBAD(seenextguidele)-treehead>treerow>treecell{}
3.6、Tag-categorizedrulesshouldnevercontaachildselector!
Avoidusgthechildselectorwithtag-categorizedrules.Youwilldramaticallycreasethematchgtime(especiallyiftheruleislikelytobematchedmoreoftenthannot)forallourrencesofthatelement.
BAD-treehead>treerow>treecell{}
BEST-.treecell-header{}
3.7、Questionallusagesofthechildselector!
Becarefulaboutusgthechildselector.Ifyoucaneupwithawaytoavoidhavgtouseit,doso.Inparticular,thechildselectorisfrequentlyusedwithRDFtreesandmenuslikeso.
BAD-treeitem[IsImapServer="true"]>treerow>.tree-folderpane-icon{}
RememberthatattributesfromRDFcanbeduplicatedatemplate!TakeadvantageofthisfacttoduplicateRDFpropertiesonchildXULelementsthatwishtochangebasedoffthatattribute.
GOOD-.tree-folderpane-icon[IsImapServer="true"]{}
3.8、Relyonheritance!
Learnwhichpropertiesherit,andallowthemtodoso!WehaveexplicitlysetupXULwidgetrysothatyoucanputlist-style-image(justoneexample)orfontrulesontheparenttag,anditwillfiltertotheanonymouscontent.Youdon'thavetowastetimewritgarulethattalksdirectlytotheanonymouscontent.
BAD-#bookmarkMenuItem>.menu-left{list-style-image:url(blah);}
GOOD-#bookmarkMenuItem{list-style-image:url(blah);}
Intheaboveexample,thedesiretostyletheanonymouscontent(withoutunderstandgthatlist-style-imageherits)resultedarulethatwastheclasscategory,whenthisrulereallyshouldhaveendedupbegthemostspecifiategoryofall,theIDcategory.
Remember,especiallywithanonymouscontent,thattheyallhavethesameclasses!Thebadruleabovecausestheiconofeverymenutobecheckedtoseeifitiscontaedthebookmarksmenuitem.Thisishideouslyexpensive(scetherearemanymenus);thisrulenevershouldhaveevenbeencheckedbyanymenuotherthanthebookmarksmenu.
3.9、Use-moz-image-region!
Puttgabunchofimagestoasgleimagefileandselectgthemwith-moz-image-regionperformssignificantlybetterthanputtgeachimagetoitsownfile.
OrigalDocumentInformation-Author:DavidHyatt
网站设计
- 静宁会SEO的网站建设公司:全面提升您的网络影
- 提升在线业务的关键:选择最佳的丽水网站建设
- 浙江网站优化发展潜力如何
- 井研专业的网站建设公司:打造您的在线品牌
- 灵山SEO网站建设公司:提升您的在线业务表现
- 蒙城网站建设优化公司:提升您网站表现的理想
- 阳谷企业网站优化:提升线上业务力的关键
- 樟树专业的网站建设公司:打造您在线业务的坚
- 通河百度SEO排名的策略与技巧
- 重庆百度快照排名如何进行精准的客户引流
- 重庆百度快照排名
- 常宁便宜的建站公司:助您轻松打造在线业务
- 巫溪百度网站优化:提升网站曝光率与流量的关
- 湖北整站优化怎么做才能放大客户需求
- 闸北网站建设多少钱?全面解析与预算规划
- 辽宁企业网站优化怎么做电话营销