CSS 2.1 快速導覽 - 群組

若是有多個 HTML 標籤 (tag) 具有完全一樣的樣式規則 (rule) ,此時可將這些標籤以群組 (grouping) 的方式組織在一起。



這就是利用逗號 , 分開不同的標籤,也就是把每個選取項 (selector) 以逗號分開,如下例
1
2
3
4
5
6
7
8
9
10
11
p, div, blockquote {
    color: white;
    background-color: black;
}
 
/* 《程式語言教學誌》的範例程式
    檔名:demo07.css
    功能:示範 CSS 2.1 樣式表的使用
    作者:張凱慶
    時間:西元 2011 年 7 月 */


此例中將 <p> 、 <div> 、 <blockquote> 三種 HTML 元素 (element) 都設定為黑底白字,我們以下面的 HTML 文件載入 demo07.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="demo07.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>
<div>There is no spoon.</div>
<div>I can only show you the door, you have to walk
through it.</div>
<div>Choice. The problem is choice.</div>
<div>Then tomorrow we may all be dead, but how would that
be different from any other day?</div>
<blockquote>I have dreamed a dream, but now that dream has gone
from me.</blockquote>
<blockquote>Choice is an illusion, created between those with
power, and those without.</blockquote>
<blockquote>Because I choose to.</blockquote>
<blockquote>Everything that has a beginning has an end.</blockquote>
</body>
</html>
 
<!-- 《程式語言教學誌》的範例程式
     檔名:demo07.html
     功能:示範 CSS 2.1 樣式表的使用
     作者:張凱慶
     時間:西元 2011 年 7 月 -->


瀏覽器 (broswer) 開啟如下



如果不用群組的寫法,就得逐一給每個標籤進行相同的樣式 (style) 定義,例如
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
p {
    color: white;
    background-color: black;
}
 
div {
    color: white;
    background-color: black;
}
 
blockquote {
    color: white;
    background-color: black;
}
 
/* 《程式語言教學誌》的範例程式
    檔名:demo08.css
    功能:示範 CSS 2.1 樣式表的使用
    作者:張凱慶
    時間:西元 2011 年 7 月 */


我們以下面的 HTML 文件載入 demo08.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="demo08.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>
<div>There is no spoon.</div>
<div>I can only show you the door, you have to walk
through it.</div>
<div>Choice. The problem is choice.</div>
<div>Then tomorrow we may all be dead, but how would that
be different from any other day?</div>
<blockquote>I have dreamed a dream, but now that dream has gone
from me.</blockquote>
<blockquote>Choice is an illusion, created between those with
power, and those without.</blockquote>
<blockquote>Because I choose to.</blockquote>
<blockquote>Everything that has a beginning has an end.</blockquote>
</body>
</html>
 
<!-- 《程式語言教學誌》的範例程式
     檔名:demo08.html
     功能:示範 CSS 2.1 樣式表的使用
     作者:張凱慶
     時間:西元 2011 年 7 月 -->


這兩份 HTML 文件的外觀是一樣的



中英文術語對照
標籤tag
規則rule
群組grouping
選取器selector
元素element
瀏覽器broswer
樣式style


您可以繼續參考

基本概念
單位
選取器
@import 規則
階層
元素呈現方式 display


相關目錄
CSS 2.1 快速導覽
HTML, CSS 教材
首頁


參考資料

http://www.w3.org/TR/CSS21/syndata.html
http://www.w3.org/TR/CSS21/selector.html

沒有留言: