例如, <div> 元素 (element) 原本並沒有預設區塊 (block) 的尺寸大小,我們可以將 width 性質 (property) 用百分比設定,這樣一來,<div> 便會佔據視窗相對的百分比比例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | div { height : 20% ; background-color : gray ; } div.a { width : 80% ; } div.b { width : 60% ; } div.c { width : 40% ; } div.d { width : 20% ; } /* 《程式語言教學誌》的範例程式 檔名:demo16.css 功能:示範 CSS 2.1 樣式表的使用 作者:張凱慶 時間:西元 2011 年 7 月 */ |
我們以下面的 HTML 文件載入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | < html > < head > < title >網頁標題</ title > < link rel = "stylesheet" type = "text/css" href = "demo16.css" > </ head > < body > < div class = "d" ></ div > < div class = "c" ></ div > < div class = "b" ></ div > < div class = "a" ></ div > </ body > </ html > <!-- 《程式語言教學誌》的範例程式 檔名:demo16.html 功能:示範 CSS 2.1 樣式表的使用 作者:張凱慶 時間:西元 2011 年 7 月 --> |
瀏覽器 (broswer) 開啟如下

從上到下共有四個灰色背景的 <div> 元素,第一個寬佔視窗的 20% ,高佔 20% ,第二個寬佔 40% ,高佔 20% ,第三個寬佔 60% ,高佔 20% ,第四個寬佔 80% ,高佔 20% ,改變瀏覽器視窗大小,這四個 <div> 元素的大小也會隨之改變。
需要相對單位的地方都可以運用,例如行高
1 2 3 4 5 6 7 8 9 10 | div.demo { line-height : 200% ; } /* 《程式語言教學誌》的範例程式 檔名:demo17.css 功能:示範 CSS 2.1 樣式表的使用 作者:張凱慶 時間:西元 2011 年 7 月 */ |
我們以下面的 HTML 文件載入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | < html > < head > < title >網頁標題</ title > < link rel = "stylesheet" type = "text/css" href = "demo17.css" > </ head > < body > < div >There is no spoon.</ div > < div >There is no spoon.</ div > < div class = "demo" >There is no spoon.</ div > < div >There is no spoon.</ div > </ body > </ html > <!-- 《程式語言教學誌》的範例程式 檔名:demo17.html 功能:示範 CSS 2.1 樣式表的使用 作者:張凱慶 時間:西元 2011 年 7 月 --> |
瀏覽器開啟如下

由於我們將 demo 類別 (class) 設定成 200% 行高,因此設定為 demo 類別的 <div> 元素,其內文字具有 200% 的行高。
中英文術語對照 | |
---|---|
百分比 | percentage |
相對單位 | relative unit |
元素 | element |
區塊 | block |
性質 | property |
瀏覽器 | broswer |
您可以繼續參考
基本概念
單位
選取器
@import 規則
階層
元素呈現方式 display
相關目錄
CSS 2.1 快速導覽
HTML, CSS 教材
首頁
參考資料
http://www.w3.org/TR/CSS21/about.html
http://www.w3.org/TR/CSS21/syndata.html
沒有留言:
張貼留言