web標準布局執行個體教程,用定位輕鬆解決CSS複雜布局

來源:互聯網
上載者:User

我相信來經典論壇學習標準的朋友,99%都不是為了進W3C,不是為了成為專業的研究人員,那麼大家來的目的是什麼呢?很簡單,其實都是想通過標準的學習讓自己多增加一項技能而已,讓自己在找工作的時候不會在被“需要熟練掌握web標準”而難倒,在下面的執行個體中xhtml並沒有什麼語意,文章的目的只是讓然大家能更好的掌握CSS中相對、絕對位置的用法(新手問此問題的確實很多),及如何有效結合背景,實現比較複雜的布局。不足之處還望前輩們能指點一二,感謝!

--------------------------------------------------第一步-------------------------------------------

先來隨便設計一個布局複雜點的(布局不規整)demo:

--------------------------------------------------第二步-------------------------------------------
我們來進行代碼的編寫
<HTML>
<HEAD>
<TITLE>model</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">
<style>
html,body{
margin:0;
padding:0;
background:#A1A9B6 url(images/allbg.jpg);
text-align:center;
}
#main{
margin:0 auto;
position:relative; -------------------------------->我是相對定位
width:780px;
height:659px;
background:url(images/model.jpg) no-repeat;
}
#main #menu{
position:absolute;
right:12px;
width:354px;
height:115px;
background:url(images/menu.gif) no-repeat;
}
#main #menu a,#main #menu a#page{
float:left;
width:112px;
height:30px;
}
#main #menu a#about{
width:80px;
height:55px;
}
#main #menu a#services{
width:70px;
height:80px;
}
#main #menu a#contacts{
width:80px;
height:115px;
}
#main #menu a:hover{
background: url(images/menu.gif) no-repeat;
}
#main #menu a#page:hover{
background-position: 0px -115px;
}
#main #menu a#about:hover{
background-position: -112px -115px;
}
#main #menu a#services:hover{
background-position: -192px -115px;
}
#main #menu a#contacts:hover{
background-position: -262px -115px;
}
#main h1{
position:absolute;-------->我是絕對位置,我相對與我的上一級#main來進行定位,因為他設定了position:relative;如果在我的父一級中沒有找到position:relative;那我就相對與body來定位了...

top:0;left:1px;
width:151px;
height:56px;
background:url(images/logo.jpg) no-repeat;
}
#main h2{
position:absolute;------------------>我也是絕對位置 我和上面提到的用法一樣
top:133px;
right:7px;
width:404px;
height:190px;
background:url(images/about.gif) no-repeat;
}
#main h6{
position:absolute;------------------>我也是絕對位置 我和上面提到的用法一樣
bottom:75px;
right:10px;
width:265px;
height:44px;
background:url(images/cars-logo.gif) no-repeat;
}
</style>
</HEAD>
<BODY>
<div id="main">
<div id="menu">
------------------------------------------------------------------------------>這個菜單是怎麼工作的呢?
一張圖是怎麼能實現背景切換呢?
呵呵,其實也很簡單?大家知道背景圖象中有:映像橫縱座標位置設定,此例子也正是運用這個特性,將擁有不同ID的連結A的背景進行位移,以達到背景切換的目的。

<a href="#" id="page" title="page"></a>
<a href="#" id="about" title="about"></a>
<a href="#" id="services" title="services"></a>
<a href="#" id="contacts" title="contacts"></a>
</div>
<h1></h1>
<h2></h2>
<h6></h6>
</div>
</BODY>
</HTML>
-----------------------------------------------實際頁面效果:---------------------------------------<meta http-equiv="Content-Type" content="text/html; charset=gb2312"><br /><style> html,body{ margin:0; padding:0; background:#A1A9B6 url(http://www.okrmt.com/demo7.8/images/allbg.jpg); text-align:center; } #main{ margin:0 auto; position:relative; width:780px; height:659px; background:url(http://www.okrmt.com/demo7.8/images/model.jpg) no-repeat; } #main #menu{ position:absolute; right:12px; width:354px; height:115px; background:url(http://www.okrmt.com/demo7.8/images/menu.gif) no-repeat; } #main #menu a,#main #menu a#page{ float:left; width:112px; height:30px; } #main #menu a#about{ width:80px; height:55px; } #main #menu a#services{ width:70px; height:80px; } #main #menu a#contacts{ width:80px; height:115px; } #main #menu a:hover{ background: url(http://www.okrmt.com/demo7.8/images/menu.gif) no-repeat; } #main #menu a#page:hover{ background-position: 0px -115px; } #main #menu a#about:hover{ background-position: -112px -115px; } #main #menu a#services:hover{ background-position: -192px -115px; } #main #menu a#contacts:hover{ background-position: -262px -115px; } #main h1{ position:absolute; top:0;left:1px; width:151px; height:56px; background:url(http://www.okrmt.com/demo7.8/images/logo.jpg) no-repeat; } #main h2{ position:absolute; top:133px; right:7px; width:404px; height:190px; background:url(http://www.okrmt.com/demo7.8/images/about.gif) no-repeat; } #main h6{ position:absolute; bottom:75px; right:10px; width:265px; height:44px; background:url(http://www.okrmt.com/demo7.8/images/cars-logo.gif) no-repeat; } </style><br /><br /><br /><p>

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.