仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 571|回复: 8
打印 上一主题 下一主题

[DIV+CSS] 来谈谈:英文教程:CSS的Float

[复制链接]
萌萌妈妈 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-15 23:52:11 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
结构清晰,容易被搜索引擎搜索到,天生优化了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:
  1. #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.
  1. #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:
    1. .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&ndash;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的标准化设计到底有什么好处?
透明 该用户已被删除
沙发
发表于 2015-1-17 23:43:16 | 只看该作者
布局表格、绘制层、制作CSS样式表等,这些内容迷糊了我的视线,真是没想到制作一个精巧别致的网页是如此的困难,可是心有不甘。
山那边是海 该用户已被删除
板凳
发表于 2015-1-24 15:12:26 | 只看该作者
难以逾越的障碍会大大打击你的学习积极性。这就需要你的恒心,坚持不懈的决心。在自己无法解决某些问题时,就需要虚心请教别人.
乐观 该用户已被删除
地板
发表于 2015-2-1 19:49:51 | 只看该作者
HTML技术的不断发展和完善,随之而产生了众多网页编辑器,从网页编辑器基本性质可以分为所见即所得网页编辑器和非所见即所得网页编辑器(则原始代码编辑器)
小妖女 该用户已被删除
5#
发表于 2015-2-7 16:20:51 | 只看该作者
在刚开始接触网页的第一堂课上,听说网页制作会是一门很无聊并且很难听懂的过程,而事实却印证了这样一个事实。
谁可相欹 该用户已被删除
6#
发表于 2015-2-22 20:18:11 | 只看该作者
是当我和赵丽芬崔明艳老师进行交流后,很快就了解了还不太清楚的内容和并不了解的知识(像布局表格、绘制布局单元格等)。
飘灵儿 该用户已被删除
7#
发表于 2015-3-7 04:18:09 | 只看该作者
足见市场的反响和MACROMEDIA公司对它们的自信。说到Dreamweaver8我们应该了解一下网页编辑器的发展过程,随着互联网(Internet) 的家喻户晓。
小女巫 该用户已被删除
8#
发表于 2015-3-14 13:14:37 | 只看该作者
Dreamweaver是集网页制作和管理网站于一身的所见即所得网页编辑器,在编辑时能同时看到源码和设计界面,非常方便新手学习制作网页。
9#
发表于 2015-3-21 09:49:02 | 只看该作者
使用内容管理系统进行开发并实现快速、精确的浏览器兼容性测试。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-12-29 21:59

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表