- 副檔名為 .css 的樣式表檔案
- HTML 文件中的 <style> 標籤
- HTML 文件中元素的 style 屬性
第一種是副檔名為 .css 的樣式表檔案,例如以下的 demo01.css
p.demo { color: white; background-color: black; } /* 《程式語言教學誌》的範例程式 http://pydoing.blogspot.com/ 檔名:demo01.css 功能:示範 CSS 2.1 樣式表的使用 作者:張凱慶 時間:西元 2011 年 7 月 */
就可以利用 HTML 文件中的 <link> 標籤 (tag) 載入 demo01.css
<html> <head> <title>網頁標題</title> <link rel="stylesheet" type="text/css" href="demo01.css"> </head> <body> <p class="demo"> 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 class="demo"> Free your mind.</p> <p>Believe the unbelievable.</p> <p class="demo"> There is no spoon.</p> <p>I can only show you the door, you have to walk through it.</p> <p class="demo"> 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 class="demo"> 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 class="demo"> Because I choose to.</p> <p>Everything that has a beginning has an end.</p> </body> </html> <!-- 《程式語言教學誌》的範例程式 http://pydoing.blogspot.com/ 檔名:demo01.html 功能:示範 CSS 2.1 樣式表的使用 作者:張凱慶 時間:西元 2011 年 7 月 -->
第二種是把樣式規則 (rule) 寫在HTML 文件中的 <style> 標籤裡,例如
<html> <head> <title>網頁標題</title> <style> p.demo { color: white; background-color: black; } </style> </head> <body> <p class="demo"> 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 class="demo"> Free your mind.</p> <p>Believe the unbelievable.</p> <p class="demo"> There is no spoon.</p> <p>I can only show you the door, you have to walk through it.</p> <p class="demo"> 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 class="demo"> 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 class="demo"> Because I choose to.</p> <p>Everything that has a beginning has an end.</p> </body> </html> <!-- 《程式語言教學誌》的範例程式 http://pydoing.blogspot.com/ 檔名:demo02.html 功能:示範 CSS 2.1 樣式表的使用 作者:張凱慶 時間:西元 2011 年 7 月 -->
第三種則是把樣式規則內嵌進 HTML 文件中元素 (element) 的 style 屬性 (attribute) 中
<html> <head> <title>網頁標題</title> </head> <body> <p style="color: white;background-color: black;"> 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 style="color: white;background-color: black;"> Free your mind.</p> <p>Believe the unbelievable.</p> <p style="color: white;background-color: black;"> There is no spoon.</p> <p>I can only show you the door, you have to walk through it.</p> <p style="color: white;background-color: black;"> 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 style="color: white;background-color: black;"> 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 style="color: white;background-color: black;"> Because I choose to.</p> <p>Everything that has a beginning has an end.</p> </body> </html> <!-- 《程式語言教學誌》的範例程式 http://pydoing.blogspot.com/ 檔名:demo03.html 功能:示範 CSS 2.1 樣式表的使用 作者:張凱慶 時間:西元 2011 年 7 月 -->
以上我們都用了相同的樣式規則,也就是白色的文字,黑色的背景,所以瀏覽器 (browser) 都會得到相同的結果
三種方式各有優缺點,例如利用元素的 style 屬性過於繁複,同時會擴大 HTML 文件的大小,然而這對局部小地方的調整卻是有效而且立即可見。一般來說設計網頁是要把結構跟樣式分開,因此建議採第一種方式,也就是在 HTML 文件之外寫樣式表檔案,然後再以 HTML 文件載入。
我們在這份《CSS 2.1 快速導覽》中,全部以第一種方式,額外的 .css 檔案作為示範。
中英文術語對照 | |
---|---|
樣式表 | style sheet |
標籤 | tag |
規則 | rule |
元素 | element |
屬性 | attribute |
瀏覽器 | browser |
您可以繼續參考
基本概念
單位
選取器
@import 規則
階層
元素呈現方式 display
相關目錄
CSS 2.1 快速導覽
HTML, CSS 教材
首頁
參考資料
http://www.w3.org/TR/CSS21/intro.html
沒有留言:
張貼留言