来谈谈:英文教程:CSS的Float
结构清晰,容易被搜索引擎搜索到,天生优化了seo网页制造poluoluo文章简介:英文教程:CSS的Float.
Whatis“Float”?
FloatisaCSSpositioningproperty.Tounderstanditspurposeandorigin,wecanlooktoprintdesign.Inaprintlayout,imagesmaybesetintothepagesuchthattextwrapsaroundthemasneeded.Thisiscommonlyandappropriatelycalled“textwrap”.Hereisanexampleofthat.
Inpagelayoutprograms,theboxesthatholdthetextcanbetoldtohonorthetextwrap,ortoignoreit.Ignoringthetextwrapwillallowthewordstoflowrightovertheimagelikeitwasn’teventhere.Thisisthedifferencebetweenthatimagebeingpartoftheflowofthepage(ornot).Webdesignisverysimilar.
Inwebdesign,pageelementswiththeCSSfloatpropertyappliedtothemarejustliketheimagesintheprintlayoutwherethetextflowsaroundthem.Floatedelementsremainapartoftheflowofthewebpage.Thisisdistinctlydifferentthanpageelementsthatuseabsolutepositioning.Absolutelypositionedpageelementsareremovedfromtheflowofthewebpage,likewhenthetextboxintheprintlayoutwastoldtoignorethepagewrap.Absolutelypositionedpageelementswillnotaffectthepositionofotherelementsandotherelementswillnotaffectthem,whethertheytoucheachotherornot.
SettingthefloatonanelementwithCSShappenslikethis:
#sidebar{float:right;}Therearefourvalidvaluesforthefloatproperty.LeftandRightfloatelementsthosedirectionsrespectively.None(thedefault)ensurestheelementwillnotfloatandInheritwhichwillassumethefloatvaluefromthatelementsparentelement.
Whatarefloatsusedfor?
Asidefromthesimpleexampleofwrappingtextaroundimages,floatscanbeusedtocreateentireweblayouts.
Floatsarealsohelpfulforlayoutinsmallerinstances.Takeforexamplethislittleareaofawebpage.Ifweusefloatforourlittleavatarimage,whenthatimagechangessizethetextintheboxwillreflowtoaccomodate:
Thissamelayoutcouldbeaccomplishedusingrelativepositioningoncontainerandabsolutepositioningontheavataraswell.Indoingitthisway,thetextwouldbeunafftedbytheavatarandnotbeabletoreflowonasizechange.
ClearingtheFloat
Float’ssisterpropertyisclear.Anelementthathastheclearpropertysetonitwillnotmoveupadjacenttothefloatlikethefloatdesires,butwillmoveitselfdownpastthefloat.Againanillustrationprobablydoesmoregoodthanwordsdo.
Intheaboveexample,thesidebarisfloatedtotherightandisshorterthanthemaincontentarea.Thefooterthenisrequiredtojumpupintothatavailablespaceasisrequiredbythefloat.Tofixthisproblem,thefootercanbeclearedtoensureitstaysbeneathbothfloatedcolumns.
#footer{clear:both;}
Clearhasfourvalidvaluesaswell.Bothismostcommonlyused,whichclearsfloatscomingfromeitherdirection.LeftandRightcanbeusedtoonlyclearthefloatfromonedirectionrespectively.Noneisthedefault,whichistypicallyunnecessaryunlessremovingaclearvaluefromacascade.Inheritwouldbethefifth,butisstranglynotsupportedinInternetExplorer.Clearingonlytheleftorrightfloat,whilelesscommonlyseeninthewild,definitelyhasitsuses.
TheGreatCollapse
Oneofthemorebewilderingthingsaboutworkingwithfloatsishowtheycanaffecttheelementthatcontainsthem(their“parent”element).Ifthisparentelementcontainednothingbutfloatedelements,theheightofitwouldliterallycollapsetonothing.Thisisn’talwaysobviousiftheparentdoesn’tcontainanyvisuallynoticeablebackground,butitisimportanttobeawareof.
Asanti-intuitiveascollapsingseemstobe,thealternativeisworse.Considerthisscenario:
Iftheblockelementontopwheretohaveautomaticallyexpandedtoaccomodatethefloatedelement,wewouldhaveanunnaturalspacingbreakintheflowoftextbetweenparagraphs,withnopracticalwayoffixingit.Ifthiswerethecase,usdesignerswouldbecomplainingmuchharderaboutthisbehaviorthanwedoaboutcollapsing.
Collapsingalmostalwaysneedstobedealtwithtopreventstrangelayoutandcross-browserproblems.Wefixitbyclearingthefloatafterthefloatedelementsinthecontainerbutbeforethecloseofthecontainer.
TechniquesforClearingFloats
Ifyouareinasituationwhereyoualwaysknowwhatthesucceedingelementisgoingtobe,youcanapplytheclear:both;valuetothatelementandgoaboutyourbusiness.Thisisidealasitrequiresnofancyhacksandnoadditionalelementsmakingitperfectlysemantic.Ofcoursethingsdon’ttypicallyworkoutthatwayandweneedtohavemorefloat-clearingtoolsinourtoolbox.
[*]TheEmptyDivMethodis,quiteliterally,anemptydiv.<divstyle="clear:both;"><div>.Sometimesyou’llseea<br/>elementorsomeotherrandomelementused,butdivisthemostcommonbecauseithasnobrowerdefaultstyling,doesn’thaveanyspecialfunction,andisunlikelytobegenericallystyledwithCSS.Thismethodisscornedbysemanticpuristssinceitspresencehasnocontexualmeaningatalltothepageandistherepurelyforpresentation.Ofcourseinthestrictestsensetheyareright,butitgetsthejobdonerightanddoesn’thurtanybody.
[*]TheOverflowMethodreliesonsettingtheoverflowCSSpropertyonaparentelement.Ifthispropertyissettoautoorhiddenontheparentelement,theparentwillexpandtocontainthefloats,effectivelyclearingitforsucceedingelements.Thismethodcanbebeautifullysemanticasitmaynotrequireanadditionalelements.Howeverifyoufindyourselfaddinganewdivjusttoapplythis,itisequallyasunsemanticastheemptydivmethodandlessadaptable.Alsobearinmindthattheoverflowpropertyisn’tspecificallyforclearingfloats.Becarefulnottohidecontentortriggerunwantedscrollbars.
[*]TheEasyClearingMethodusesacleverCSSpseduoselector(:after)toclearfloats.Ratherthansettingtheoverflowontheparent,youapplyanadditionalclasslike“clearfix”toit.ThenapplythisCSS:.clearfix:after{content:".";visibility:hidden;display:block;height:0;clear:both;}Thiswillapplyasmallbitofcontent,hiddenfromview,aftertheparentelementwhichclearsthefloat.Thisisn’tquitethewholestory,asadditionalcodeneedstobeusedtoaccomodateforolderbrowsers.
Differentscenarioscallfordifferentfloatclearningmethods.Takeforexampleagridofblocks,eachofdifferenttypes.
Tobettervisuallyconnectthesimilarblocks,wewanttostartanewrowasweplease,inthiscasewhenthecolorchanges.Wecoulduseeithertheoverfloworeasyclearningmethodifeachofthecolorgroupshadaparentelement.Or,weusetheemptydivmethodinbetweeneachgroup.Threewrappingdivsthatdidn’tpreviouslyexistorthreeafterdivsthatdidn’tpreviouslyexist.I’llletyoudecidewhichisbetter.
ProblemswithFloats
Floatsoftengetbeatonforbeingfragile.ThemajorityofthisfragilitycomesfromIE6andtheslewoffloat-relatedbugsithas.AsmoreandmoredesignersaredroppingsupportforIE6,youmaynotcare,butforthefolksthatdocarehereisaquickrundown.
[*]Pushdownisasymptomofanelementinsideafloateditembeingwiderthanthefloatitself(typicallyanimage).Mostbrowserswillrendertheimageoutsidethefloat,butnothavethepartstickingoutaffectotherlayout.IEwillexpandthefloattocontaintheimage,oftendrasticallyaffectinglayout.Acommonexampleisanimagestickingoutofthemaincontentpushthesidebardownbelow.
Quickfix:Makesureyoudon’thaveanyimagesthatdothis,useoverflow:hiddentocutoffexcess.
[*]DoubleMarginBug–AnotherthingtorememberwhendealingwithIE6isthatifyouapplyamargininthesamedirectionasthefloat,itwilldoublethemargin.Quickfix:setdisplay:inlineonthefloat,anddon’tworryitwillremainablock-levelelement.
[*]The3pxJogiswhentextthatisupnexttoafloatedelementismysteriouslykickedawayby3pxlikeaweirdforcefieldaroundthefloat.Quickfix:setawidthorheightontheaffectedtext.
[*]InIE7,theBottomMarginBugiswhenifafloatedparenthasfloatedchildreninsideit,bottommarginonthosechildrenisignoredbytheparent.Quickfix:usingbottompaddingontheparentinstead.
Alternatives
Ifyouneedtextwrappingaroundimages,therereallyaren’tanyalternativesforfloat.Speakingofwhich,checkoutthisratherclevertechniqueforwrappingtextaroundirregularshapes.Butforpagelayout,theredefinitelyarechoices.EricSolrighthereonAListAparthasanarticleonFauxAbsolutePositioning,whichisaveryinterestingtechniquethatinmanywayscombinestheflexibilityoffloatswiththestrengthofabsolutepositioning.CSS3hastheTemplateLayoutModulethat,whenwidelyadopted,willprovetobethepagelayouttechniqueofchoice.
在Div+CSS标准化的影响下,网页设计人员已经把这一要求作为行业标准。那么什么是Div+CSS标准?Div+CSS的标准化设计到底有什么好处? 布局表格、绘制层、制作CSS样式表等,这些内容迷糊了我的视线,真是没想到制作一个精巧别致的网页是如此的困难,可是心有不甘。 难以逾越的障碍会大大打击你的学习积极性。这就需要你的恒心,坚持不懈的决心。在自己无法解决某些问题时,就需要虚心请教别人. HTML技术的不断发展和完善,随之而产生了众多网页编辑器,从网页编辑器基本性质可以分为所见即所得网页编辑器和非所见即所得网页编辑器(则原始代码编辑器) 在刚开始接触网页的第一堂课上,听说网页制作会是一门很无聊并且很难听懂的过程,而事实却印证了这样一个事实。 是当我和赵丽芬崔明艳老师进行交流后,很快就了解了还不太清楚的内容和并不了解的知识(像布局表格、绘制布局单元格等)。 足见市场的反响和MACROMEDIA公司对它们的自信。说到Dreamweaver8我们应该了解一下网页编辑器的发展过程,随着互联网(Internet) 的家喻户晓。 Dreamweaver是集网页制作和管理网站于一身的所见即所得网页编辑器,在编辑时能同时看到源码和设计界面,非常方便新手学习制作网页。 使用内容管理系统进行开发并实现快速、精确的浏览器兼容性测试。
页:
[1]