用CSS建设网站的实例(5)

http://www.itjxue.com  2015-08-06 00:13  来源:未知  点击次数: 

第五步:网页主要框架之外的附加结构的布局与表现:

第五步主要介绍除网页主要框架之外的附加结构的表现(Layout),包括以下内容:
1.主导航条;
2.标题(heading),包括网站名和内容标题;
3.内容;
4.页脚信息,包括版权,认证,副导航条(可选)。 Webjx.Com

加入这些结构时,为了不破坏原有框架,我们需要在css文件"body"标签(TAG)下加入:

 .hidden {  
display: none;  
}  
 .hidden {
display: none;
}

IT教学网

".hidden"即我们加入的类(class),这个类可以使页面上任意属于hidden类的元素(element)不显示。这些会在稍后使用,现在请暂时忘记它。

现在我们加入标题(heading):

先回到HTML的代码,<h1>到<h6>是我们常用的html标题代码。比如我们一般用<h1>网站名</h1>,<h2>网站副标题</h2>,<h3>内容主标题</h3>等。我们往html文件的Header层(Div)加入: IT教学网

<div id="header"> 
<h1>Enlighten Designs</h1> 
</div>    
 <div id="header">
<h1>Enlighten Designs</h1>
</div> Webjx.Com

刷新一下页面,你就可以看到巨大的标题,和标题周围的空白,这是因为<h1>>标签的默认大小和边距(margin)造成的,先要消除这些空白,需要加入:

 h1 {  
margin: 0;  
padding: 0;  
}  
  
 h1 {
margin: 0;
padding: 0;
} IT教学网

接下来是导航条:

控制导航条表现的css代码相对比较复杂,我们将在第九步或是第十步中详细介绍。现在html文件加入导航代码: Webjx.Com

<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> 
 <div id="main-nav"> Webjx.Com
<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>

IT教学网

(注:原教程使用了dl和dt,jorux在这使用了更常用的ul和li标签)
目前导航条的表现比较糟糕,但是要在以后的教程中介绍其特殊表现,故需要暂时隐藏导航条,于是加入:

<div id="main-nav"> 
<dl class="hidden"> 
<dt id="about"><a href="http://css.jorux.com/wp-admin/post.php#" >About</a></dt> 
<dt id="services"><a href="http://css.jorux.com/wp-admin/post.php#" >Services</a></dt> 
<dt id="portfolio"><a href="http://css.jorux.com/wp-admin/post.php#" >Portfolio</a></dt> 
<dt id="contact"><a href="http://css.jorux.com/wp-admin/post.php#" >Contact Us</a></dt> 
</dl> 
</div>
<div id="main-nav">


<dl class="hidden">
<dt id="about"><a href="http://css.jorux.com/wp-admin/post.php#" >About</a></dt>
<dt id="services"><a href="http://css.jorux.com/wp-admin/post.php#" >Services</a></dt>
<dt id="portfolio"><a href="http://css.jorux.com/wp-admin/post.php#" >Portfolio</a></dt>
<dt id="contact"><a href="http://css.jorux.com/wp-admin/post.php#" >Contact Us</a></dt>
</dl>
</div>

我们跳一步,先到页脚:

页脚包括两部分:左边的版权,认证和右边的副导航条。
我们先要让副导航条向右浮动,就像之前处理Sidebar和Content关系的一样,需要加入一个新的层(div):

IT教学网

<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>  IT教学网
</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> IT教学网

理论上,我们可以控制源文件上的任意元素的浮动,但由于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#" >Terms of Trade</a> 
</div> 
Copyright © Enlighten Designs  
Powered by <a href="http://www.enlightenhosting.com/" >Enlighten Hosting</a> and  
<a href="http://www.vadmin.co.nz/" >Vadmin 3.0 CMS</a></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> - IT教学网
<a href="http://css.jorux.com/wp-admin/post.php#" >Terms of Trade</a>
</div>
Copyright © Enlighten Designs
Powered by <a href="http://www.enlightenhosting.com/" >Enlighten Hosting</a> and
<a href="http://www.vadmin.co.nz/" >Vadmin 3.0 CMS</a></div> Webjx.Com

刷新你的页面,你将看到如下所示:

DIV CSS网页布局实例:十步学会用CSS建站 Webjx.Com

最后我们回到内容部分:用<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>
<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 /> Webjx.Com
    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>

Webjx.Com

刷新页面可以看到在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 {
 margin: 0;
 padding: 0;
}
#content p {
 margin: 0;
 padding: 0;
} Webjx.Com

这样我们就告诉浏览器,仅仅是隶属于content层的<h2><p>标签的margin和padding的值为0!

(责任编辑:IT教学网)

更多

推荐CSS教程文章