<!-- Feedsky FEED发布代码开始 -->
如果您喜欢这些文章,欢迎点击此处订阅本Blog <!-- FEED自动发现标记开始 --> <link title="RSS 2.0" type="application/rss+xml" href="http://feed.feedsky.com/softwave" rel="alternate"> <!-- FEED自动发现标记结束 -->
<!--Google 468*60横幅广告开始--><script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; google_ad_width = 468; google_ad_height = 60; google_ad_format = "468x60_as"; google_ad_type = "image"; //2007-07-26: CSDN google_ad_channel = "6063905817"; google_color_border = "6699CC"; google_color_bg = "E6E6E6"; google_color_link = "FFFFFF"; google_color_text = "333333"; google_color_url = "AECCEB"; google_ui_features = "rc:6"; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script><!--Google 468*60横幅广告结束-->
CSS框架已经出现很长时间了,关于这些框架的用处也被我们讨论了很多遍了。有人说,CSS框架不够先进,还有人说这些框架大大的节省了他们的开发时间。在此,我们将不再讨论这个问题。
前段时间,我了解到了CSS框架。经过对Malo、BluePrint和960做了实验对比后,我得出一个结论:我最喜欢960CSS框架。
本教程将解释这个框架的基本原理,这样你就可以用960来快速进入开发。(注:960网格系统官方网站点击进入)
基本原理
你必须知道一些基本原理来“学习这个框架是如何工作的”。你可以通过实验(或者是用firebug)来学习它,不过我也将会在这里为你介绍它。让我们开始吧。
不要编辑960.css文件
首先是一个小提示:不要编辑960.css文件,否则,将来你将不能更新这个框架。因为尽管我们需要布局我们的HTML,我们将创建一个独立的CSS文件。
加载网格
因为我们可以使用一个外部文件的CSS代码,我们必须在我们的HTML网站中加载它们,我们可以通过以下代码来实现:
- <linkrel=”stylesheet”type=”text/css”media=”all”href=”path/to/960/reset.css”/>
-
- <linkrel=”stylesheet”type=”text/css”media=”all”href=”path/to/960/960.css”/>
-
- <linkrel=”stylesheet”type=”text/css”media=”all”href=”path/to/960/text.css”/>
-
这些做好了之后,我们必须添加我们自己的CSS文件。例如,你可以叫这个文件为
style.css或
site.css或者其它任何名字。用下面代码引用这个文件:
- <linkrel=”stylesheet”type=”text/css”media=”all”href=”path/to/style.css”/>
容器
在960框架中,你可以选择名为.container_12和.container_16的两个容器class。他们都是960px的宽度(这就是为什么叫960),它们的不同是分的列数不同。.container_12被分割为12列,.container_16被分割为16列。这些960px宽的容器是水平居中的。
网格/列
有很多列宽可供选择,而且在这两个容器里,这些宽度也不相同。你可以通过打开960.css文件来查看这些宽度。但是这对于设计一个网站来说是不必要的。有一个小技巧可以让这个框架更加易用。
比如,你想要在你的容器里建两列(叫sidebar/content)。你可以这样做:
- <divclass="container_12">
- <divclass="grid_4">sidebar</div>
- <divclass="grid_8">maincontent</div>
- </div>
可以看到,你的第一列(grid_4)的数字加上第二列(grid_8)的数字正好是12。也就是说,你不必知道每一列的宽度,你可以选择列宽通过一些简单的数学计算。
如果我们要建一个4列的布局,代码可以是这样的:
- <divclass="container_12">
- <divclass="grid_2">sidebar</div>
- <divclass="grid_6">maincontent</div>
- <divclass="grid_2">photo’s</div>
- <divclass="grid_2">advertisement</div>
- </div>
正如你所看到的那样,这个系统依然很完美。但是如果你想使用嵌套的列的话,你会发现它是有问题的。比如,如果后面三列都属于content列:
- <divclass="container_12">
- <divclass="grid_2">sidebar</div>
- <divclass="grid_10">
- <divclass="grid_6">maincontent</div>
- <divclass="grid_2">photo’s</div>
- <divclass="grid_2">advertisement</div>
- </div>
- </div>
你会发现这错位了,不过不用着急,这正是我们下一节要说的。
间距
默认情况下,每列之间都有间距。每一个grid_(这里代表数字)class左右都有10个像素的间距。也就是说,两列之间,总共有20px的间距。
20px间距对创建一个有足够宽的空白间距的布局来说是很棒的,它可以让一切看起来很自然。这也是我喜欢使用960的原因之一。
在上面的例子中,我们遇到了个问题,现在我们就来解决它。
问题是,每一列都有左右边距。而嵌套的三列中,第一列和最后一列是不需要边距的,解决方法是:
- <divclass="container_12">
- <divclass="grid_2">sidebar</div>
- <divclass="grid_10">
- <divclass="grid_6alpha">maincontent</div>
- <divclass="grid_2">photo’s</div>
- <divclass="grid_2omega">advertisement</div>
- </div>
- </div>
我们可以简单的添加”alpha“样式来去掉左边的间距,添加“omega”样式来去除右边的间距。这样我们刚刚创建的这个例子在任何浏览器里面就很完美了(当然包括IE6)。
样式
好了,你现在已经完全了解如果用960框架来创建一个网格布局的基本原理了。当然,我们也可以添加一些样式到我们的布局中。
- <divclass="container_12">
- <divid="sidebar"class="grid_2">sidebar</div>
- <divid="content"class="grid_10">
- <divid="main_content"class="grid_6alpha">maincontent</div>
- <divid="photo"class="grid_2">photo’s</div>
- <divid="advertise"class="grid_2omega">advertisement</div>
- </div>
- </div>
因为CSS使用特性来确定哪一个样式声明具有高于其它样式的优先级。”id“比class更重要。
用这种方法,我们可以在自己的文件中重写那些被class设定的规则(比如宽度,padding,边框等)。
补充
以下是我做的一个简单的测试页面,代码如下:
- <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <htmlxmlns="http://www.w3.org/1999/xhtml">
- <head>
- <metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/>
- <title>TESTPage</title>
- <linkhref="css/960.css"rel="stylesheet"type="text/css"/>
- <linkhref="css/reset.css"rel="stylesheet"type="text/css"/>
- <linkhref="css/text.css"rel="stylesheet"type="text/css"/>
- </head>
- <body>
- <divclass="container_16">
- <divclass="grid_8"style="background-color:#FF0000">
- <divclass="grid_6omega"style="background-color:#0000FF">0</div>
- <divclass="grid_2omega"style="background-color:#FF00FF">00</div>
- </div>
- <divclass="grid_4"style="background-color:#00FF00">a</div>
- <divclass="grid_2"style="background-color:#666666">b</div>
- <divclass="grid_2"style="background-color:#666666">c</div>
- </div>
- </body>
- </html>
以下是960官方的实例文档,可以看看它的网格是如何写的:
-
- <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <htmlxmlns="http://www.w3.org/1999/xhtml"dir="ltr"lang="en-US">
- <headprofile="http://gmpg.org/xfn/11">
- <metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"/>
- <title>TravisIsaacs|MyLifeInPixels</title>
- <metaname="keywords"content="photoshop,wordpress,userexperience,ux,uxd,ixd,interactiondesigner,interactiondesign,keynote,powerpoint,xhtml,css,webdesign,mac,timemachine,superduper,backup,aperture,canon,40d,viewzi,viewmix,views,lensrentals,lensrentals.com,infinitescrolling,cameralensrentaldallas"/>
- <linkrel="stylesheet"href="http://css.travisisaacs.com/2009/style.css"media="all"/>
- <!--[ifIE]>
- <linkrel="stylesheet"href="http://css.travisisaacs.com/2009/ie.css"media="all"/>
- <![endif]-->
- <linkrel="alternate"type="application/rss+xml"title="TravisIsaacs|MyLifeInPixelsRSSFeed"href="http://travisisaacs.com/feed/"/>
- <linkrel="pingback"href="http://travisisaacs.com/xmlrpc.php"/>
- <linkrel="shortcuticon"href="http://travisisaacs.com/favicon.ico"/>
- </head>
- <bodyid="TheIsaacsFamilyChristmasCard">
- <divid="header">
- <divclass="container_12">
- <ahref="http://travisisaacs.com"id="return-home"title="Gobacktothehomepage">Thework<spanclass="amp">&</span>thoughtsof<strong>TravisB.Isaacs</strong></a>
- <ulid="navigation">
- <liid="tab-work"><ahref="http://travisisaacs.com/work/">Work</a></li>
- <liid="tab-blog"><ahref="http://travisisaacs.com/thoughts/">Thoughts</a></li>
- <liid="tab-photography"><ahref="http://travisisaacs.com/photography/">Photography</a></li>
- <liid="tab-about"><ahref="http://travisisaacs.com/about/">About</a></li>
- <liid="tab-contact"><ahref="http://travisisaacs.com/contact/">Contact</a></li>
- <liid="tab-search"><scripttype="text/javascript"language="Javascript"src="http://vfp.viewzi.com/includes/879F-4A21-011D.js"></script></li>
- </ul>
- </div>
- </div>
- <divclass="ie-center">
- <divclass="container_12">
- <divid="super">
- <h1>Hello.I'm<ahref="http://travisisaacs.com/about/">TravisIsaacs</a>,andIdesigngreatuserexperiences.</h1>
- </div>
- <divid="content">
- <divid="recent-post"class="grid_8alpha">
- <divclass="post">
- <h2>
- <ahref="http://travisisaacs.com/2008/12/13/the-isaacs-family-christmas-card/"rel="bookmark"title="PermanentLinktoTheIsaacsFamilyChristmasCard">
- TheIsaacsFamilyChristmasCard</a>
- </h2>
- <divclass="post-date">
- Dec13th//
- <spanclass="comment-count">
- 11comments</span>
- </div>
- <p>WhenplanningourfamilyChristmascardthisyear,mywifeandIdiscusstheoptionofhiringaprofessionalphotographertotakephotosofourdaughter.However,wethoughtitwouldbemuchmorefunandrewardingtodoitourselves.</p>
- <ahref="http://travisisaacs.com/2008/12/13/the-isaacs-family-christmas-card/"rel="bookmark"title="PermanentLinktoTheIsaacsFamilyChristmasCard"><strong>Keepreading»</strong></a>
- </div>
- </div>
- <divid="recent-work"class="grid_4omega">
- <divclass="m_group"id="hello">
- <p>I'mTravisIsaacs,adesignerwhodabblesincode,informationarchitecture,andinteractiondesign.<ahref="http://travisisaacs.com/about/">Moreaboutme</a></p>
- </div>
- <ahref="http://travisisaacs.com/feed/"id="subscribe">Subscribe</a>
- </div>
- <divclass="clear"></div>
- <h2>FeaturedWork</h2>
- <ahref="http://travisisaacs.com/work/projects/tattoo-information-form/"id="featured-work-img"title="Viewthisproject">
- <imgsrc="http://img.travisisaacs.com/2009/featured-work-large.jpg"alt="featuredwork:tattoinformationsheet"/>
- </a>
- <divclass="clear"></div>
- </div>
- <divclass="clear"></div>
- </div>
- <divclass="clear"></div>
- <divclass="clear"></div>
- <divid="footer"class="container_12">
- <ul>
- <li><ahref="http://travisisaacs.com">Home</a></li>
- <li><ahref="http://travisisaacs.com/work/">Work</a></li>
- <li><ahref="http://travisisaacs.com/thoughts/">Thoughts</a></li>
- <li><ahref="http://travisisaacs.com/photography/">Photography</a></li>
- <li><ahref="http://travisisaacs.com/about/">About</a></li>
- <li><ahref="http://travisisaacs.com/contact/">Contact</a></li>
- </ul>
- <divclass="copyright">©2008TravisIsaacs.AllRightsReserved.</div>
- </div>
- </div>
- <scripttype="text/javascript">
- vargaJsHost=(("https:"==document.location.protocol)?"https://ssl.":"http://www.");
- document.write(unescape("%3Cscriptsrc='"+gaJsHost+"google-analytics.com/ga.js'type='text/javascript'%3E%3C/script%3E"));
- </script>
- <scripttype="text/javascript">
- try{
- varpageTracker=_gat._getTracker("UA-703764-4");
- pageTracker._trackPageview();
- }catch(err){}</script>
-
- <scripttype="text/javascript">
- var_wh=((document.location.protocol=='https:')?"https://sec1.woopra.com":"http://static.woopra.com");
- document.write(unescape("%3Cscriptsrc='"+_wh+"/js/woopra.js'type='text/javascript'%3E%3C/script%3E"));
- </script>
-
- </body>
- </html>
<!--新Google 468*60横幅广告开始--><script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; /* 468x60, 创建于 08-8-6 */ google_ad_slot = "7368701459"; google_ad_width = 468; google_ad_height = 60; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script><!--新Google 468*60横幅广告结束-->
<!--新Google 468x15 横链接单元开始--><script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; /* 468x15 横链接单元 */ google_ad_slot = "5785741422"; google_ad_width = 468; google_ad_height = 15; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script><!--新Google 468x15 横链接单元结束-->
<!-- Google Reader shared发布代码开始 --><script type="text/javascript" src="http://www.google.com/reader/ui/publisher.js"></script><script type="text/javascript" src="http://www.google.com/reader/public/javascript/user/00697638153916680411/state/com.google/broadcast?n=5&callback=GRC_p(%7Bc%3A%22green%22%2Ct%3A%22%5Cu8FD9%5Cu4E9B%5Cu6587%5Cu7AE0%5Cu4E5F%5Cu503C%5Cu5F97%5Cu4E00%5Cu770B%22%2Cs%3A%22false%22%7D)%3Bnew%20GRC"></script><!-- Google Reader shared发布代码结束 -->