Cascading Style Sheets (CSS)

層疊樣式表(Cascading Style Sheets)簡稱 CSS 。
CSS 用以規範 HTML 文件的樣式(Style)。
CSS 用以描述 HTML 的元件如何呈現在網頁上的樣式。
樣式通常保存在外部的.css 文件中。 通過僅僅編輯一個簡單的CSS 文檔,外部樣式表使你有能力同時改變站點中所有頁面的佈局和外觀。

CSS 實例

CSS 規則由兩個主要的部分構成:選擇器,以及一條或多條聲明:
            h1 {color:blue; text-align:center;}
CSS聲明總是以分號(;)結束,聲明組以大括號({})括起來:
     p {color:red; text-align:center;}

註解(comment)

CSS 註釋放置在 <style> 元件內,以 /* 開頭,以 */ 結尾:
     p {color:red; text-align:center;}   /* 此段文本紅色,居中對齊 */

id 和class 選擇器

如果你要在HTML元件中設置CSS樣式,你需要在元件中設置"id" 和"class"選擇器。

id 選擇器

id 選擇器可以為標有特定 id 的HTML 元件指定特定的樣式。一張網頁只能有一個 ID屬性值。
HTML元件以 id 屬性來設置 id 選擇器, CSS 中 id 選擇器以"#" 來定義。
以下的樣式規則應用於元件屬性 id="para1":
<html>
<head>
<meta charset="big5">
<title>id 選擇器</title>
<style>
#para1
{
	text-align:center;
	color:red;
}
</style>
</head>

<body>
<p id="para1">Hello World!</p>
<p>這個段落不受該樣式的影響。</p>
<p id="para1">Welcome, CSS world!</p>
</body>
</html>

看效果

class 選擇器

class 選擇器用於描述一組元件的樣式,class 選擇器有別於 id選擇器,class可以在多個元件中使用。
class 選擇器在HTML中以class屬性表示, 在CSS 中,類選擇器以一個點"."號顯示:
在以下的例子中,所有擁有center 類的HTML 元件均為居中。
<html>
<head>
<meta charset="big5">
<title>class 選擇器1</title>
<style>
.center
{
	text-align:center;
}
</style>
</head>

<body>
<h1 class="center">標題居中</h1>
<p class="center">段落居中。</p>
</body>
</html>

看效果

也可以指定特定的HTML元件使用class。

<html>
<head>
<meta charset="big5">
<title>class 選擇器2</title>
<style>
p.center
{
	text-align:center;
}
</style>
</head>

<body>
<h1 class="center">這個標題不受影響</h1>
<p class="center">這個段落居中對齊。</p>
</body>
</html>

看效果

分組選擇器

在樣式表中有很多具有相同樣式的元件,可以使用分組選擇器,每個選擇器用逗號分隔。例如.
h1,h2,p
{
color:green;
}

嵌套選擇器

在下面的例子設置了三個樣式:
p
{
color:blue;
text-align:center;
}
.marked
{
background-color:red;
}
.marked p
{
color:white;
}

看效果

如何插入樣式表

插入樣式表的方法有三種:

外部樣式表

當樣式需要應用於很多頁面時,外部樣式表將是理想的選擇。 在使用外部樣式表的情況下,你可以通過改變一個文件來改變整個站點的外觀。 每個頁面使用 標籤鏈接到樣式表。 標籤在(文檔的)頭部:
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>

瀏覽器會從文件 mystyle.css 中讀到樣式聲明,並根據它來格式文檔。

外部樣式表可以在任何文本編輯器中進行編輯。 文件不能包含任何的 html 標籤。 樣式表應該以.css 擴展名進行保存。 下面是一個樣式表文件的例子:

hr {color:sienna;}
p {margin-left:20px;}
body {background-image:url("../images/back40.gif");}

附註: 不要在屬性值與單位之間留有空格。 假如你使用 "margin-left: 20 px" 而不是"margin-left: 20px" ,它僅在IE 6 中有效,但是在Mozilla/Firefox 或Netscape 中卻無法正常工作。

看效果

內部樣式表

當單個文檔需要特殊的樣式時,就應該使用內部樣式表。 你可以使用<style> 標籤在文檔頭部定義內部樣式表,就像這樣:
<head>
<style>
hr {color:brown;}
p {margin-left:20px; color:GreenYellow;}
body {background-image:url("gif/gas.jpg");}
</style>
</head>

看效果

內聯樣式

當樣式僅需要在一個元件上應用一次時。 要使用內聯樣式,你需要在相關的標籤內使用樣式(style)屬性。 Style 屬性可以包含任何CSS 屬性。

本例展示如何改變段落的顏色和左外邊距:

<p>This text does not have any style applied to it.</p>

<p style="color:sienna;margin-left:20px">This is a paragraph.</p>

<p style = "font-size: 20pt">This text has the
<em>font-size</em> style applied to it, making it 20pt.
</p>

<p style = "font-size: 20pt; color: blue">
This text has the <em>font-size</em> and
<em>color</em> styles applied to it, making it
20pt. and blue.</p>

看效果

多重樣式

如果某些屬性在不同的樣式表中被同樣的選擇器定義,那麼屬性值將從更具體的樣式表中被繼承過來。

例如,外部樣式表擁有針對 h3 選擇器的三個屬性:

h3
{
color:red;
text-align:left;
font-size:8pt;
}

而內部樣式表擁有針對h3 選擇器的兩個屬性:

h3
{
text-align:right;
font-size:20pt;
}

假如擁有內部樣式表的這個頁面同時與外部樣式鍊接,那麼 h3 得到的樣式是:

color:red;
text-align:right;
font-size:20pt;

即顏色屬性將被繼承於外部樣式表,而文字排列(text-alignment)和字體尺寸(font-size)會被內部樣式表中的規則取代。

多重樣式將層疊為一個

樣式表允許以多種方式規定樣式信息。 樣式可以規定在單個的HTML 元件中,在HTML 頁的頭元件中,或在一個外部的CSS 文件中。 甚至可以在同一個HTML 文檔內部引用多個外部樣式表。

層疊次序

當同一個 HTML 元件被不止一個樣式定義時,會使用哪個樣式呢?

一般而言,所有的樣式會根據下面的規則層疊於一個新的虛擬樣式表中,其中數字3 擁有最高的優先權。

瀏覽器預設設定(Default setting)

  1. 外部樣式表
  2. 內部樣式表(位於<head> 標籤內部)
  3. 內聯樣式(在HTML 元件內部)

因此,內聯樣式(在HTML 元件內部)擁有最高的優先權,這意味著它將優先於以下的樣式聲明: <head>標籤中的樣式聲明, 外部樣式表中的樣式聲明,或者瀏覽器中的樣式聲明(預設值)。

附註: 提示:如果你使用了外部文件的樣式在<head>中也定義了該樣式,則內部樣式表會取代外部文件的樣式。

元件定位

position: absolute;表示位置不隨其他元件移動。z-index值高的可以疊在z-index值低的。
如下例,circle.gif (z-index值2)可以疊在 i.gif (z-index值2)上,文字 (z-index值2)可以疊在circle.gif止上。
   <head>
     <title>Absolute Positioning</title>
   </head>

   <body>

      <p><img src = "i.gif" style = "position: absolute;
         top: 0px; left: 0px; z-index: 1"
         alt = "First positioned image" /></p>
      <p style = "position: absolute; top: 50px; left: 50px;
         z-index: 3; font-size: 20pt;">Positioned Text</p>
     <p><img src = "circle.gif" style = "position: absolute;
         top: 25px; left: 100px; z-index: 2" alt =
         "Second positioned image" /></p>

   </body>
Positioning Element -- z-index

此例使用相對定位,position: relative; 位置隨元件移動。如

      <style type = "text/css">

         p           { font-size: 1.3em;
                       font-family: verdana, arial, sans-serif }

         span        { color: red;
                       font-size: .6em;
                       height: 1em }

         .super      { position: relative;
                       top: -1ex }

         .sub        { position: relative;
                       bottom: -1ex }

         .shiftleft  { position: relative;
                       left: -1ex }

         .shiftright { position: relative;
                       right: -1ex }

      </style>
Positioning Element -- Relative Positioning

背景

背景可以嵌上圖片,background-repeat: no-repeat; 表示只貼一片。沒有標明 no-repeat,會貼滿全頁。
標明 repeat-x 貼滿一列,repeat-y 貼滿一行。
background-attachment: fixed; 表示位置固定,不隨視窗大小改變、移動或捲動而變。此例在右下角。
      <style type = "text/css">

         body  { background-image: url(logo.gif);
                 background-position: bottom right;
                 background-repeat: no-repeat;
                 background-attachment: fixed; }

         p     { font-size: 18pt;
                 color: #aa5588;
                 text-indent: 1em;
                 font-family: arial, sans-serif; }

         .dark { font-weight: bold; }

      </style>
Backgrounds

元件尺寸

定位之外,元件也可以有實際的尺寸,如 div 區塊的底部邊距有.5em。1em為預設字型的尺寸。
其他可用的單位有 in (英吋)、 cm (公分)、 mm (毫米)、 pt (points, 1pt=1/72 in)、pc (picas, 1pc=12pt)。
在第一個 div 區塊的內聯樣式規定該區塊寬度為屏幕的20%。
假如文字太長,超出範圍,稱為溢出(overflow)。在第三個 div 區塊規定 overflow: scroll,在文字溢出時可以捲動。
<html>
   <head>
      <style type = "text/css">
         div { background-color: #ffccff;
               margin-bottom: .5em }
      </style>
   <head>
   <body>

      <div style = "width: 20%">Here is some
      text that goes in a box which is
      set to stretch across twenty precent
      of the width of the screen.</div>

      <div style = "width: 80%; text-align: center">
      Here is some CENTERED text that goes in a box
      which is set to stretch across eighty precent of
      the width of the screen.</div>

      <div style = "width: 20%; height: 30%; overflow: scroll">
      This box is only twenty percent of
      the width and thirty percent of the height.
      What do we do if it overflows? Set the
      overflow property to scroll!
      </div>

   </body>
</html>
Element Dimensions

文本浮動

通常瀏覽器依照文本和元件在 html 文件中出現的次序顯示在屏冪上。
區塊使用屬性 float 可以浮動。下例中第二和第三個 div 區塊靠右移動。顯示時第二個 div 區塊在第三段文本的右側。
使用屬性 text-align 可使文本在區塊中靠左、靠右或居中對齊。如第一個區塊文本居中,第二、三區塊文本靠右。
<html>
   <head>
      <title>Flowing Text Around Floating Elements</title>

      <style type = "text/css">

         div { background-color: #ffccff;
               margin-bottom: .5em;
               font-size: 1.5em;
               width: 50% }

         p   { text-align: justify; }

      </style>

   </head>

   <body>

      <div style = "text-align: center">">
         Deitel & Associates, Inc.</div>

      <div style = "float: right; margin: .5em;
         text-align: right">
         Corporate Training and Publishing</div>

      <p>Deitel & Associates, Inc. is an internationally
      recognized corporate training and publishing organization
      specializing in programming languages, Internet/World
      Wide Web technology and object technology education.
      Deitel & Associates, Inc. is a member of the World Wide
      Web Consortium. The company provides courses on Java,
      C++, Visual Basic, C, Internet and World Wide Web
      programming, and Object Technology.</p>

      <div style = "float: right; padding: .5em;
         text-align: right">
         Leading-edge Programming Textbooks</div>

      <p>The company's clients include many Fortune 1000
      companies, government agencies, branches of the military
      and business organizations. Through its publishing
      partnership with Prentice Hall, Deitel & Associates,
      Inc. publishes leading-edge programming textbooks,
      professional books, interactive CD-ROM-based multimedia
      Cyber Classrooms, satellite courses and World Wide Web
      courses.<span style = "clear: right"> Here is some
      unflowing text. Here is some unflowing text.</span></p>

   </body>
</html>

Floating Elements

圖片浮動

元件的水平方向浮動,意味著元件只能左右移動而不能上下移動。
一個浮動元素會盡量向左或向右移動,直到它的外邊緣碰到包含框或另一個浮動框的邊框為止。
浮動元件之後的元件將圍繞它。
浮動元件之前的元素將不會受到影響。
<style>
.thumbnail
{
	float:left;
	width:110px;
	height:90px;
	margin:5px;
}
</style>

Floating Pictures

清除浮動- 使用 clear
元件浮動之後,周圍的元件會重新排列,為了避免這種情況,使用clear 屬性。
clear 屬性指定元件兩側不能出現浮動元件。

<style>
.thumbnail
{
	float:left;
	width:110px;
	height:90px;
	margin:5px;
}
.text_line
{
	clear:both;
	margin-bottom:2px;
}
</style>

Floating Pictures

網頁佈局

HTML Layout Elements

HTML 有幾個語義元件,用於定義網頁的不同部分: 有關語意元件( semantic elements), 詳見 HTML Semantics chapter.

HTML Layout Techniques

有四種不同技術做佈局:

使用網格(grid) 的網頁

使用 display:grid; 製作塊狀網格容器:
<html>
<head>
      <title>使用 grid 的網頁</title>
<style>
.grid-container {
  display: grid;
  grid-template-columns: auto auto auto;
  background-color: #2196F3;
  padding: 10px;
}

.grid-item {
  background-color: rgba(255, 255, 255, 0.8);
  border: 1px solid rgba(0, 0, 0, 0.8);
  padding: 20px;
  font-size: 30px;
  text-align: center;
}
</style>
</head>
<body>

<h1>display: grid</h1>

<p>Use display: grid; to make a block-level grid container:</p>

<div class="grid-container">
  <div class="grid-item">1<div>
  <div class="grid-item">2<div>
  <div class="grid-item">3<div>
  <div class="grid-item">4<div>
  <div class="grid-item">5<div>
  <div class="grid-item">6<div>
  <div class="grid-item">7<div>
  <div class="grid-item">8<div>
  <div class="grid-item">9<div>
</div>

</body>
</html>

使用 grid 的網頁設計

參考資料

Basic Concepts of grid layout

使用 float 的網頁佈局

整頁包在區塊 container 中,內含四個方塊: 標題 header、頁腳 rooter、左框 left 和內容 content。
左框 left 向左浮動。頁腳標明 clear:left,不受左框影響。

<html>
   <head>
      <title>使用 float 的網頁佈局</title>
      <style type = "text/css">
div.container
{
	width:100%;
	margin:0px;
	border:1px solid gray;
	line-height:150%;
}
div.header,div.footer
{
	padding:0.5em;
	color:white;
	background-color:gray;
	clear:left;
}
h1.header
{
	padding:0;
	margin:0;
}
div.left
{
	float:left;
	width:160px;
	margin:0;
	padding:1em;
}
div.content
{
	margin-left:190px;
	border-left:1px solid gray;
	padding:1em;
}
      </style>
   </head>

   <body>

<div class="container">
<div class="header"><h1 class="header">w3big.com</h1></div>
<div class="left"><p>"Never increase, beyond what is necessary, the number of entities required to explain anything." William of Ockham (1285-1349)</p></div>
<div class="content">
<h2>Free Web Building Tutorials</h2>
<p>At w3cschool you will find all the Web-building tutorials you need,
from basic HTML and XHTML to advanced XML, XSL, Multimedia and WAP.</p>
<p>w3cschool - The Largest Web Developers Site On The Net!</p></div>
<div class="footer">Copyright 1999-2005 by Refsnes Data.</div>
</div>

   </body>
</html>

模仿有框架的網頁

方盒模型

方盒模型(Box Model)封裝周圍的HTML元件,它包括:邊距,邊框,填充,和實際內容。

下面的圖片說明了盒子模型(Box Model):

box-model.gif

不同部分的說明:

Margin(外邊距)屬性定義元件周圍的空間。
margin可以單獨改變元件的上,下,左,右邊距。 也可以一次改變所有的屬性。

Border(邊框)樣式屬性指定要顯示什麼樣的邊界。
border-style屬性用來定義邊框的樣式,border-width定義邊框的寬度: thin、medium 或 thick。

當元件的Padding(填充)(內邊距)被清除時,所"釋放"的區域將會受到元件背景顏色的填充。
單獨使用填充屬性可以改變上下左右的填充。

<html>
   <head>
      <title>Borders</title>

      <style type = "text/css">

         body    { background-color: #ccffcc }

         div     { text-align: center;
                   margin-bottom: 1em;
                   padding: .5em }

         .thick  { border-width: thick }

         .medium { border-width: medium }

         .thin   { border-width: thin }

         .groove { border-style: groove }

         .inset  { border-style: inset }

         .outset { border-style: outset }

         .red    { border-color: red }

         .blue   { border-color: blue }

      </style>
   </head>

   <body>

      <div class = "thick groove">This text has a border</div>
      <div class = "medium groove">This text has a border</div>
      <div class = "thin groove">This text has a border</div>

      <p class = "thin red inset">A thin red line...</p>
      <p class = "medium blue outset">
         And a thicker blue line</p>

   </body>
</html>
Borders

各種樣式的邊框

<html>
   <head>
      <title>Various Borders</title>

      <style type = "text/css">

         body    { background-color: #ccffcc }

         div     { text-align: center;
                   margin-bottom: .3em;
                   width: 50%;
                   position: relative;
                   left: 25%;
                   padding: .3em }
      </style>
   </head>

   <body>

      <div style = "border-style: solid">Solid border</div>
      <div style = "border-style: double">Double border</div>
      <div style = "border-style: groove">Groove border</div>
      <div style = "border-style: ridge">Ridge border</div>
      <div style = "border-style: inset">Inset border</div>
      <div style = "border-style: outset">Outset border</div>

   </body>
</html>
各種樣式的邊界

CSS 預處理器

CSS 預處理器可以說是 CSS 語法的擴充。為了彌補 CSS 在大型專案維護性的不足,CSS 預處理器中新增了變數、混入、繼承、嵌套等寫法, 讓開發者可以更有結構地撰寫簡潔、清晰且好維護的 CSS 程式碼。

現今較為主流的 CSS 預處理器有三種,分別是 Sass/SCSS、Less、Stylus,其中的 Sass/SCSS 是目前最多人使用也相對較成熟的選擇。

==> SCSS 簡介

參考資料