- element[att]
- element[att=val]
- element[att~=val]
- element[att|=val]
第 1 種情況,當元素具有某項屬性時,該元素就具有設定的樣式,例如
div[title] { color : red ; } |
這樣當 <div> 中有設定 tittle 屬性的時候,文字就會是紅色。
第 2 種情況,屬性值完全對應到特定值,例如
div[title = color] { color : green ; } |
這樣當 <div> 中 tittle 屬性為 color 的時候,文字就會是綠色。
第 3 種情況,當屬性中用一連串空格相間的文字設定,然後符合其中一項的時候,例如
div[title ~= del] { color : white ; } |
這樣當 <div> 中 tittle 屬性有 del 的時候,文字就會是白色。
第 4 種情況,屬性開頭後用 - 結尾的時候,例如
div[title |= en] { color : blue ; } |
這樣當 <div> 中 tittle 屬性以 en- 開頭的時候,文字就會是藍色。
舉實例如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | div[title] { color : red ; } div[title = color] { color : green ; } div[title ~= del] { color : white ; } div[title |= en] { color : blue ; } /* 《程式語言教學誌》的範例程式 檔名:demo25.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 21 22 | < html > < head > < title >網頁標題</ title > < link rel = "stylesheet" type = "text/css" href = "demo25.css" > </ head > < body > You ever have that feeling where you're not sure if you're awake or still dreaming? < div title = "test" >Free your mind.</ div > < div title = "color" >Believe the unbelievable.</ div > < div title = "omit del" >There is no spoon.</ div > < div title = "en-US" >Because I choose to.</ div > </ body > </ html > <!-- 《程式語言教學誌》的範例程式 檔名:demo25.html 功能:示範 CSS 2.1 樣式表的使用 作者:張凱慶 時間:西元 2011 年 7 月 --> |
瀏覽器 (broswer) 開啟如下

中英文術語對照 | |
---|---|
屬性選取器 | attribute selector |
元素 | element |
屬性 | attrubute |
樣式 | style |
瀏覽器 | broswer |
您可以繼續參考
基本概念
單位
選取器
@import 規則
階層
元素呈現方式 display
相關目錄
CSS 2.1 快速導覽
HTML, CSS 教材
首頁
參考資料
http://www.w3.org/TR/CSS21/selector.html
https://developer.mozilla.org/en/CSS/Getting_Started/Selectors
沒有留言:
張貼留言