Skip to content


十步学会用css建站——第五步:网页主要框架之外的附加结构的布局与表现;

声明:《十步学会用css建站》这篇文章非我原创,原文来自Steve DennisCreating a CSS Layout from scratch,由jorux翻译。我转贴此文除了想丰富我的blog内容之外也是希望更多的朋 友能够在学习css中获得一些帮助。

第五步主要介绍除网页主要框架之外的附加结构的表现(Layout),包括以下内容:
1.主导航条;
2.标题(heading),包括网站名和内容标题;
3.内容;
4.页脚信息,包括版权,认证,副导航条(可选)。
加入这些结构时,为了不破坏原有框架,我们需要在css文件”body”标签(TAG)后加入:.hidden {display: none;}
“.hidden”即我们加入的类(class),这个类可以使页面上任意属于hidden类的元素(element)不显示。这些会在稍后使用,现在请暂时忘记它。
现在我们加入标题(heading):


先回到HTML的代码,<h1>到<h6>是我们常用的html标题代码。比如我们一般用<h1>网站名</h1>,<h2>网站副标题</h2>,<h3>内容主标题</h3>等。我们往html文件的Header层(Div)加入:
<div id="header"><h1>Enlighten Designs</h1></div>
刷新一下页面,你就可以看到巨大的标题,和标题周围的空白,这是因为<h1>标签的默认大小和边距(margin)造成的,先要消除这些空白,需要加入:
h1 {margin: 0;padding: 0;}
接下来是导航条:
控制导航条表现的css代码相对比较复杂,我们将在第九步或是第十步中详细介绍。现在html文件加入导航代码:
<div id="main-nav"><ul><li id="about"><a href="http://css.jorux.com/wp-admin/post.php#" >About</a></li><li id="services"><a href="http://css.jorux.com/wp-admin/post.php#" >Services</a></li><li id="portfolio"><a href="http://css.jorux.com/wp-admin/post.php#" >Portfolio</a></li><li id="contact"><a href="http://css.jorux.com/wp-admin/post.php#" >Contact Us</a></li></ul></div>
(注:原教程使用了dl和dt,jorux在这使用了更常用的ul和li标签)
目前导航条的表现比较糟糕,但是要在以后的教程中介绍其特殊表现,故需要暂时隐藏导航条,于是加入:
<div id="main-nav"><ul class="hidden"><li id="about"><a href="http://css.jorux.com/wp-admin/post.php#" >About</a></li><li id="services"><a href="http://css.jorux.com/wp-admin/post.php#" >Services</a></li><li id="portfolio"><a href="http://css.jorux.com/wp-admin/post.php#" >Portfolio</a></li><li id="contact"><a href="http://css.jorux.com/wp-admin/post.php#" >Contact Us</a></li></ul></div>
我们跳一步,先到页脚:
页脚包括两部分:左边的版权,认证和右边的副导航条。
我们先要让副导航条向右浮动,就像之前处理Sidebar和Content关系的一样,需要加入一个新的层(div):
<div id="footer"><div id="altnav"><a href="http://css.jorux.com/wp-admin/post.php#" >About</a> - <a href="http://css.jorux.com/wp-admin/post.php#" >Services</a> - <a href="http://css.jorux.com/wp-admin/post.php#" >Portfolio</a> - <a href="http://css.jorux.com/wp-admin/post.php#" >Contact Us</a> -<a href="http://css.jorux.com/wp-admin/post.php#" >Terms of Trade</a></div></div>
理论上,我们可以控制源文件上的任意元素的浮动,但由于IE浏览器的BUG,被浮动层需要首先出现在源文件上,也就是说我们把副标题放在版权和认证的前面:
<div id="footer"><div id="altnav"><a href="http://css.jorux.com/wp-admin/post.php#" >About</a> - <a href="http://css.jorux.com/wp-admin/post.php#" >Services</a> - <a href="http://css.jorux.com/wp-admin/post.php#" >Portfolio</a> - <a href="http://css.jorux.com/wp-admin/post.php#" >Contact Us</a> - <a href="http://css.jorux.com/wp-admin/post.php#" >of Trade</a></div>Copyright © Enlighten DesignsPowered by <a href="http://www.enlightenhosting.com/" >Enlighten Hosting</a> and<a href="http://www.vadmin.co.nz/" >Vadmin 3.0 CMS</a></div>
刷新你的页面,你将看到如下所示(点击看大图):

最后我们回到内容部分:用<h2<>表现内容标题–”About”,”Contact us”;用<p>表现段落;用</br>断行。
<div id="content"><h2>About</h2><p><strong>Enlighten Designs</strong> is an Internet solutions provider that specialises in front and back end development. To view some of the web sites we have created view our portfolio.</p><p>We are currently undergoing a 'face lift', so if you have any questions or would like more information about the services we provide please feel free to contact us.</p><h2>Contact Us</h2><p>Phone: (07) 853 6060<br />Fax: (07) 853 6060<br />Email: <a href="mailto:info@enlighten.co.nz" >info@enlighten.co.nz</a><br />P.O Box: 14159, Hamilton, New Zealand</p><p><a href="http://css.jorux.com/wp-admin/post.php#" >More contact information…</a></p></div>
刷新页面可以看到在Content层中又出现一些空白,这是由于<h2><p>标签的默认边距(margin)造成的,我们必须消除这些恼人的空白,当又不想把网页中所有的<h2><p>标签地边距都设为0,这就需要使用css的子选择器(“child css selector”),在html的文件结构中,我们想控制的<h2><p>标签(child)是属于#content层(parent)的,因此在css文件中写入: #content h2 {margin: 0;padding: 0;}#content p {margin: 0;padding: 0;}
这样我们就告诉浏览器,仅仅是隶属于content层的<h2><p>标签的margin和padding的值为0!

Posted in blogspot.


3 回复

如果关注本文发展,欢迎订阅本文评论 RSS feed.

  1. Anonymous says

    为了不破坏原有框架,我们需要在css文件”body”标签(TAG)下加入:.hidden {display: none;}
    具体是哪里啊???

  2. 痴人痴语 says

    原文的翻译确实不太准确,谢谢你的提醒。
    其实这个位置并不是很严格,此处是一个类的定义。所以与id类似,单独放在外部样式表中即可。此处浏览器在读取并无顺序的区别。所谓的顺序是为了规范,方便阅读、修改而已。
    若不明白此时也可以在样式表的最顶部加入:
    .hidden {display: none;}
    也可达到同样的效果。

  3. Anonymous says



评论中可以使用HTML(点此看详情)

或是使用 引用通告(trackback).