
宣告區中由為數不等的宣告 (declaration) 組成,每一個宣告以分號結尾,如下例
1 2 3 4 5 6 7 8 9 10 11 | p { color : white ; background-color : black ; } /* 《程式語言教學誌》的範例程式 檔名:demo06.css 功能:示範 CSS 2.1 樣式表的使用 作者:張凱慶 時間:西元 2011 年 7 月 */ |
第 1 行到第 4 行便是一條選取項加上宣告區的規則,分行與空格都不是必要的,這是我們為了清楚列出每一組宣告,因此規則也可以寫成
1 | p { color : white ; background-color : black ;} |
宣告由性質 (property) 、冒號與數值 (value) 組成,性質就是哪一種樣式,例如 color 為文字顏色的樣式, background-color 為背景顏色,性質後接冒號與數值區隔開來。
CSS 允許的數值依性質不同各有不同,這裡的 white 與 black 都是指定顏色的關鍵字 (keyword) , white 表示白色, black 表示黑色。
所謂的關鍵字就是預先定義好的詞彙,特定詞彙表示特定的樣式。
左大括弧 { 與右大括弧 } 圍起來的地方被稱為區塊 (block) ,因此其內被稱為宣告區。中括弧 [] 與小括弧 () 另有用處,括弧在 CSS 中都是特殊記號。
兩個單引號 '' 或兩個雙引號 "" 圍起來的內容被稱為字串 (string) , CSS 中很多地方都需要用到字串,例如網址、字元等。
由於 <p> 為區塊元素,表示 HTML 的段落,我們以下面的 HTML 文件載入 demo06.css
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 28 29 30 31 32 33 | < html > < head > < title >網頁標題</ title > < link rel = "stylesheet" type = "text/css" href = "demo06.css" > </ head > < body > < p >You ever have that feeling where you're not sure if you're awake or still dreaming?</ p > < p >A prison for your mind.</ p > < p >Free your mind.</ p > < p >Believe the unbelievable.</ p > < p >There is no spoon.</ p > < p >I can only show you the door, you have to walk through it.</ p > < p >Choice. The problem is choice.</ p > < p >Then tomorrow we may all be dead, but how would that be different from any other day?</ p > < p >I have dreamed a dream, but now that dream has gone from me.</ p > < p >Choice is an illusion, created between those with power, and those without.</ p > < p >Because I choose to.</ p > < p >Everything that has a beginning has an end.</ p > </ body > </ html > <!-- 《程式語言教學誌》的範例程式 檔名:demo06.html 功能:示範 CSS 2.1 樣式表的使用 作者:張凱慶 時間:西元 2011 年 7 月 --> |
瀏覽器 (broswer) 開啟如下

以上結果可以清楚看到每一個 <p> 都是黑底白字,符合 demo06.css 的規則。
中英文術語對照 | |
---|---|
樣式 | style |
規則 | rule |
選取器 | selector |
宣告區 | declaration block |
元素 | element |
宣告 | declaration |
性質 | property |
數值 | value |
關鍵字 | keyword |
瀏覽器 | broswer |
區塊 | block |
字串 | string |
您可以繼續參考
基本概念
單位
選取器
@import 規則
階層
元素呈現方式 display
相關目錄
CSS 2.1 快速導覽
HTML, CSS 教材
首頁
參考資料
http://www.w3.org/TR/CSS21/syndata.html
https://developer.mozilla.org/en/CSS/Getting_Started/Selectors
沒有留言:
張貼留言