h1 {color:blue; text-align:center;}
p {color:red; text-align:center;}
p {color:red; text-align:center;} /* 此段文本紅色,居中對齊 */
<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>
<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 中卻無法正常工作。
<head>
<style>
hr {color:brown;}
p {margin-left:20px; color:GreenYellow;}
body {background-image:url("gif/gas.jpg");}
</style>
</head>
本例展示如何改變段落的顏色和左外邊距:
<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)會被內部樣式表中的規則取代。
一般而言,所有的樣式會根據下面的規則層疊於一個新的虛擬樣式表中,其中數字3 擁有最高的優先權。
瀏覽器預設設定(Default setting)
因此,內聯樣式(在HTML 元件內部)擁有最高的優先權,這意味著它將優先於以下的樣式聲明: <head>標籤中的樣式聲明, 外部樣式表中的樣式聲明,或者瀏覽器中的樣式聲明(預設值)。
附註: 提示:如果你使用了外部文件的樣式在<head>中也定義了該樣式,則內部樣式表會取代外部文件的樣式。
<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
<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
<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>
<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>
<style>
.thumbnail
{
float:left;
width:110px;
height:90px;
margin:5px;
}
</style>
清除浮動- 使用 clear
元件浮動之後,周圍的元件會重新排列,為了避免這種情況,使用clear 屬性。
clear 屬性指定元件兩側不能出現浮動元件。
<style>
.thumbnail
{
float:left;
width:110px;
height:90px;
margin:5px;
}
.text_line
{
clear:both;
margin-bottom:2px;
}
</style>
<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>
整頁包在區塊 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):
不同部分的說明:
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 預處理器有三種,分別是 Sass/SCSS、Less、Stylus,其中的 Sass/SCSS 是目前最多人使用也相對較成熟的選擇。
==> SCSS 簡介