第 2 頁 寫入整體層結構與CSS

來源:互聯網
上載者:User

接下來我們在案頭建立一個檔案夾,命名為“DIV+CSS布局練習”,在檔案夾下建立兩個空的記事本文檔,輸入以下內容:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標題文檔</title>
<link href="css.css" rel="stylesheet" type="text/css" />
</head>

<body>
</body>
</html>

這是XHTML的基本結構,將其命名為index.htm,另一個記事本文檔則命名為css.css。

下面,我們在<body></body>標籤對中寫入DIV的基本結構,代碼如下:

<div id="container">[color=#aaaaaa]<!--頁面層容器-->[/color]
  <div id="Header">[color=#aaaaaa]<!--頁面頭部-->[/color]
  </div>
  <div id="PageBody">[color=#aaaaaa]<!--頁面主體-->[/color]
    <div id="Sidebar">[color=#aaaaaa]<!--側邊欄-->[/color]
    </div>
    <div id="MainBody">[color=#aaaaaa]<!--主體內容-->[/color]
    </div>
  </div>
  <div id="Footer">[color=#aaaaaa]<!--頁面底部-->[/color]
  </div>
</div>

為了使以後閱讀代碼更簡易,我們應該添加相關注釋,接下來開啟css.css檔案,寫入CSS資訊,代碼如下:

/*基本資料*/
body {font:12px Tahoma;margin:0px;text-align:center;background:#FFF;}

/*頁面層容器*/
#container {width:100%}

/*頁面頭部*/
#Header {width:800px;margin:0 auto;height:100px;background:#FFCC99}

/*頁面主體*/
#PageBody {width:800px;margin:0 auto;height:400px;background:#CCFF00}

/*頁面底部*/
#Footer {width:800px;margin:0 auto;height:50px;background:#00FFFF}

 

把以上檔案儲存,用瀏覽器開啟,這時我們已經可以看到基礎結構了,這個就是頁面的架構了。

關於以上CSS的說明(詳細請參考CSS2.0中文手冊,網上有下載):

1、請養成良好的注釋習慣,這是非常重要的;

2、body是一個HTML元素,頁面中所有的內容都應該寫在這標籤對之內,我就不多說了;

3、講解一些常用的CSS代碼的含義:

font:12px Tahoma
這裡使用了縮寫,完整的代碼應該是:font-size:12px;font-family:Tahoma;說明字型為12像素大小,字型為Tahoma格式;

margin:0px
也使用了縮寫,完整的應該是:

margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px

margin:0px 0px 0px 0px

順序是 上 / 右 / 下 / 左,你也可以書寫為margin:0(縮寫);
以上樣式說明body部分對上右下左邊距為0像素,如果使用auto則是自動調整邊距,

另外還有以下幾種寫法:
margin:0px auto;
說明上下邊距為0px,左右為自動調整;
我們以後將使用到的padding屬性和margin有許多相似之處,他們的參數是一樣的,
只不過各自表示的含義不相同,margin是外部距離,而padding則是內部距離。

text-align:center
文字對齊,可以設定為左、右、中,這裡我將它設定為置中對齊。

background:#FFF
設定背景色為白色,這裡顏色使用了縮寫,完整的應該是background:#FFFFFF。
background可以用來給指定的層填充背景色、背景圖片,以後我們將用到如下格式:
background:#ccc url('bg.gif') top left no-repeat;
表示:使用#CCC(灰階色)填充整個層,使用bg.gif做為背景圖片,
top left
表示圖片位於當前層的左上端,no-repeat表示僅顯示圖片大小而不填充滿整個層。
top/right/left/bottom/center
用於定位背景圖片,分別表示 上 / 右 / 下 / 左 / 中;還可以使用
background:url('bg.gif') 20px 100px;
表示X座標為20像素,Y座標為100像素的精確定位;
repeat/no-repeat/repeat-x/repeat-y
分別表示 填充滿整個層 / 不填充 / 沿X軸填充 / 沿Y軸填充。

height / width / color
分別表示高度(px)、寬度(px)、字型顏色(HTML色系表)。

4、如何使頁面置中?

大家將代碼儲存後可以看到,整個頁面是置中顯示的,那麼究竟是什麼原因使得頁面置中顯示呢?
是因為我們在#container中使用了以下屬性:
margin:0 auto;
按照前面的說明,可以知道,表示上下邊距為0,左右為自動,因此該層就會自動置中了。
如果要讓頁面居左,則取消掉auto值就可以了,因為預設就是居左顯示的。
通過margin:auto我們就可以輕易地使層自動置中了。

5、這裡我只介紹這些常用的CSS屬性了,其他的請參看CSS2.0中文手冊。

相關文章

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.