按 ESC 概覽
html, body {
position: relative;
padding: 0; margin: 0;
overflow: hidden;
font-family: '文泉驛微米黑', 'LiHei Pro', '微軟正黑體', sans-serif;
font-weight: 200;
letter-spacing: -0.02em;
color: #eee; width: 100%; height: 100%;
min-height: 400px;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
Source: W3Schools
<link>
<head>
...
<link rel="stylesheet" type="text/css" href="path/to/file.css">
...
</head>
<style>
<style>
可放在 <head>
或 <body>
均可。
元素的最後外觀,來自於多個樣式定義
後者蓋過前者。
拿掉瀏覽器預設的外觀定義
統一各瀏覽器的外觀定義 規則說明
vertical-align
, z-index
width
, min-width
, max-width
height
, min-height
, max-height
background
, border-radius
background
url()
可以填絕對路徑(http
開頭、/
開頭)、相對路徑。
相對路徑以 CSS 檔的位置起算;../
是「上一層資料夾」。
background: #000 url(images/bg.gif) no-repeat top right;
background-color: #000;
background-image: url(images/bg.gif);
background-repeat: no-repeat;
background-position: top right;
background
中沒有定義顏色 → transparent
。
background-color: red;
/* background 會展開為下列三條規則 */
background-color: transparent;
background-image: url(http://mrorz.github.io/webdev/images/css/selector.gif);
background-position: left center;
px
: 1px = 螢幕上的像素高em
: 1em = 當前字型大小%
: 父元素值的百分比inherit
所有 CSS 屬性,均可設為「inherit
」。
部分 CSS 屬性預設不會繼承自父元素,明確指定 inherit
才會繼承。
inherit
(續)input
,textarea
,select
,button
等 replaced element,樣式不會自動 inherit 自父元素。
font-family
的特殊值
serif
, sans-serif
, cursive
, fantacy
, monospace
瀏覽器對各種 Generic font family 有預設字體
有襯線;明體
無襯線;黑體
monospace
字型的每個字元都一樣寬,適合排版程式碼。
color
屬性
font-family
font-family: '文泉驛微米黑', 'LiHei Pro', '微軟正黑體', sans-serif;
字體在使用者電腦裡沒有 → 用下一個
最後一個通常放 Generic font family。
font-size: 12px
line-height: 1.4
font-family: Arial, sans-serif
/* All-in-one shorthand */
font: 12px/1.4 Arial, sans-serif;
line-height
:純數字(幾倍於字體大小)或 數字px
與字體本身相關的,為 font-
開頭。
font-style: normal | italic
font-weight: normal | bold | 數字
font-variant: small-caps
font
font: italic bold .8em/1.2 Arial, sans-serif;
font-style: italic;
font-weight: bold;
font-size: .8em;
line-height: 1.2;
font-family: Arial, sans-serif;
text-decoration: none
| underline
| line-through
text-align: left
| right
| center
background
, width
, height
font
, line-height
, font-size
, font-style
, font-weight
em
, px
, %
)、顏色、特殊值 inherit
tag
, .class
, #id
tag
, .class
, #id
div.tall
有 tall
class 之 div
#target.highlight
有 lightlight
class 且 ID 為 target
之 element
.box.widget
同時有 box
和 widget
兩個 class 之 element
用逗號 ,
將 Selector 分開
margin
, border
, padding
圖片來源:W3C Box Model
注意 .heart
的寬度與高度。
背景顏色寬(高)度 = 內容寬(高) + padding 寬(高)
和 Word / PowerPoint 不同。
margin
、padding
margin
是下面四個 property 的 shorthand:
margin-top
margin-bottom
margin-left
margin-right
padding
以此類推。
margin
、padding
shorthand1:全部;2:上下、左右;3:上側下;4:上右下左。
border
border
為下面三個 property 的 shorthand:
border-style
樣式(預設: none
)border-width
粗細(預設: medium
)border-color
顏色(預設: 與 color
相同)border
上下左右 border 的寬度、顏色、樣式可以分開設定。
border-bottom
是 border-bottom-style
、border-bottom-width
、border-bottom-color
的 shorthand。
(以此類推 border-top
、border-left
、border-right
)
border
shorthandborder-style
、border-width
、border-color
都可以設定 1~4 個值。
規則同 margin
。
border
與 outline
border
會佔有空間、推開周圍元素;outline
不會。
垂直邊界有時候會重疊在一起,只表現比較大的那一個。
(margin-left
、margin-right
永遠不會重疊。)
Margin collapsing 出沒之處:
width
與 height
box-sizing: border-box
瀏覽器支援
width = border + padding + content
以下屬性都可以設定百分比。
width
& height
margin
padding
那 100% 是以什麼為準?
Box A 的 Containing block = A 的 ancestor 中,與 A 最近的 Block-level box 的 content edge。
Block-level box:display
為 block
之類的 box。
網頁呈現在螢幕時,就是視窗螢幕(viewport)
垂直的 margin
與 padding
若為 percentage value,
100% = containing block 的寬度。
display
屬性display
Property<p>
的 box 呈上下排列<em>
的 box 排在字裡行間display
有關<p>
,<h1>
~<h6>
,<div>
預設 display: block;
<em>
,<strong>
,<span>
預設 display: inline;
display
的值block
自成一行,寬度佔滿滿。inline
塞在字裡行間,會被斷行。寬度同內容。inline-block
塞在字裡行間,自成一格不分離。寬度同內容。display
與 margin
block
上下左右皆退開。寬度變小了。inline
只有左右有影響。inline-block
上下左右皆退開。display
與 padding
block
上下左右都加上填充、擠開內容。inline
上下左右都加上填充,但只有左右會擠開旁邊內容。inline-block
上下左右都加上填充、擠開內容。display
與 width
、height
block
寬度與高度都有效果、推開旁邊內容。inline
寬度與高度都沒效果。inline-block
寬度與高度都有效果、推開旁邊內容。實務上最常接觸到的會是 div
。
其預設為 display: block
。
以下詳細介紹 display: block
的行為。
display: block
的寬度display: block
元素在寬度上,需要符合這個等式:
margin-left
+ border-left-width
+ padding-left
+ width
+ padding-right
+ border-right-width
+ margin-right
= width of containing block
display: block
預設值margin
:0border-width
:0padding
: 0width
: auto
auto
「請瀏覽器幫我算」
auto
的auto
的實際寬度auto
值,就用等式來解 auto
。width: auto
,則其他任何 auto
都會被計算為 0
,然後用等式來算 width
auto
的值,則忽略 margin-right
的設定,改成用等式計算 margin-right
。(看上去會像是靠左對齊)margin-left
與 margin-right
同時是 auto
,那設寬度時會使 margin-left
= margin-right
。display: block
置中與置右margin-left
和 margin-right
同為 auto
時,元素會水平置中。(前提是左右邊界之間的寬度不要寬過 containing block)
auto
的實際高度margin-top
、margin-bottom
的 auto
等於 0
。height: auto
,則高度就會被設成「上 content edge」到內容底端的距離。(「內容」僅包含「在 Normal flow 內」的元素)height
則為 0。綜合以上,形成 block 元素上的 height、垂直 padding 與垂直 margin 「推開」上下元素的效果。
display: block
百分比高度auto
(看內容文字或子元素高度)如果設定 height: 50%
,那元素與其 containing block 的高度會發生什麼事情?
--> 如果 containing block 沒有指定高度,height
就會被視為 auto
(所以會看內容文字或子元素高度)。
display
大複習值 | block |
inline-block |
inline |
---|---|---|---|
排版 方向 |
上到下 | 左到右,隨文字斷行 | |
寬 | 預設以 margin edge 貼齊容器寬 | 預設和內容同寬,margin、padding 會撐開左右鄰居。 | width 無用,必和內容同寬。margin, padding 行為同左。 |
高 | 預設和內容同高,margin、padding 撐開上下鄰居 | height 無用,以 line-height 為準。垂直的 padding, margin 不影響行高。
|
display
值none
: 連 box 都沒有。完全沒有容身之處。flex-direction
) 排列 boxflex
)justify-content
、align-items
)order
)display: flex
display: flex
的元素flex-direction
flex-direction: row | column | row-reverse | column-reverse
row
flex-wrap
flex-direction: nowrap | wrap | wrap-reverse
nowrap
nowrap
:無論如何,將所有 flex item 排成一條線wrap
:flex item 可以有很多行,寬度不夠就換行wrap-reverse
:同 wrap
,但是從最後一行開始排到第一行。上面 2 個 CSS property 可以合併成下面 shorthand
flex-flow: <flex-direction> <flex-wrap>
flex-basis
flex-basis: content | width 與 height 能用的值
auto
content
:內容大小。auto
:看 main size property(width
或 height
);如果其為 auto
,則視為 content
。flex-grow
flex-grow: 0 | <positive number>
0
flex-basis
排列好後,如果 main axis 方向排不滿一行(flex line),瀏覽器會依照每個 flex item 所寫的數字為比例來增加該 flex line 每個 flex item 的 main size,直到佔滿 flex line。flex-shrink
flex-shrink: 0 | <positive number>
1
flex-basis
排列好後,若 main axis 方向的大小超出 flex container,瀏覽器會依照每個 flex item 所寫的數字、乘上寬度後,依比例來減少 main size,直到恰好佔滿 main axis 的一行(flex line)。flex-shrink
多行的狀況下⋯⋯
flex-shrink
不為 0
height
或 width
)align-items
的設定align-items
align-items: flex-start | flex-end
stretch
justify-content
justify-content: flex-start | flex-end
flex-start
position
屬性文字由左邊往右邊,超過邊界就折到下一行
由上而下、由左而右 → Normal Flow
display: block
之元素垂直排列,display: inline / inline-block
之元素水平排列。
position: static
| relative
float: none
除了 Normal flow 之外,還有:
position: absolute
| fixed
float: left
| right
position: static
毫無反應,就是個預設值。
position: relative
left
, top
, bottom
, right
left
、right
之中只能設定一個;top
、bottom
之中只能設定一個。無論如何元素寬度與高度都不會被影響。position: static
| relative
float: none
除了 Normal flow 之外,還有:
position: absolute
| fixed
float: left
| right
position: absolute
:脫離 Normal flow
position: fixed
:脫離 Normal flow,黏在 viewport 上
Containing block 的定義,會隨著 position
不同而變化!
position: static
, relative
containing block 為 ancestor 中,最接近的 block-level box(之前的 slide)。
position: fixed
containing block 為 viewport。
position: absolute
containing block 為 ancestor 中,最接近而且 position
不是 static
的元素的 padding edge)。
position: absolute
的寬度position: absolute
的元素在寬度上,需要符合這個等式:
left
+ margin-left
+ border-left-width
+ padding-left
+ width
+ padding-right
+ border-right-width
+ margin-right
+ right
= width of containing block
left
, right
可以是 auto。
position: absolute
與 left
, right
left
, top
, bottom
, right
.child
的第一個 position != static
祖先是 .world
。
.child
的 containing block 是 .world
。
因此 .child
的 bottom
、right
是以 .world
為準。
position: fixed
left
, top
, bottom
, right
同 position: absolute
Containing block = 視窗內容(viewport)。
position
與容器高度height: auto
計算高度時,只會計算 Normal flow 內的元素高度。
float
屬性position: static
| relative;
float: none
;
除了 Normal flow 之外,還有:
position: absolute
| fixed
float: left
| right
使 .outer
變為 float 或 absolute positioning,可以脫離 normal flow。
float
:文繞圖float
clear
屬性可終止圍繞。float
(2)不會撐開 parent,要 clear
或用 overflow: auto|hidden
。
position
, display
& float
力氣排行榜上面的屬性會蓋過下面的。
display: none
position: absolute | fixed
float: left | right
display: block | inline | inline-block | ...
決定誰在「前面」誰在「後面」
「Stacking Context」: 疊東西的地方=樓層
一開始所有元素都在同一樓層
(Root stacking context = <html>
)
z-index
= 樓層號碼(整數)
position: 非 static
z-index != auto
或 opacity < 1
「Stacking Context」: 疊東西的地方。
特別感謝 Lucien - 淺談 CSS 開發與除錯
z-index
蓋房子順序:
地基 → 地下室 → 低樓層 → 高樓層
z-index
的 elementz-index:0 | auto
的 elementz-index
的 elementz-index 相同的元素,用下面規則分高下。
(由低至高)
position
!= static
的 element (Positioned elements)若兩元素同一層,則先寫在 HTML 檔裡的,會被壓在下面。
Positioned 元素 > Inline 元素 > float 元素 > block in Normal Flow
加上 z-index JS Bin
可實用。
想要做的目標網頁
.sidebar
佔據螢幕右邊 33%
左欄:.intro
與 .main
;右欄:.sidebar
把 .intro
和 .main
包起來、然後水平排列左欄和右欄
水平排列 → float
或 display: inline-block
左欄:.intro
與 .main
;右欄:.sidebar
不修改 HTML 的作法
.intro
和 .main
66%
右欄 position: absolute
移到視窗右邊
li
元素float
display: inline-block
<li>
裡可以有 <ul>
:hover
magic.root div
: .root
的子孫中為 div
者。.root>div
: .root
的孩子裡為 div
者。*
:任意元素。
.root *
: .root
的子孫。
中括號; [屬性 運算子 值]
https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors
依狀態套用的「假的」class
幾乎所有元素都可用 :hover
a
元素::link
, :visited
, :active
避免定義相互覆蓋的順序:LoVe HAte
老大 :first-child
, 老么 :last-child
老 N :nth-child()
content
屬性。::before
像是一個 span
元素::after
像是一個 span
元素:before
, :after
):checked
: 「被勾選」之 pseudo-class。input + *
選出 input
的最大的弟弟妹妹。input ~ *
選出 input
的所有弟弟妹妹。tag
, #id
, .class
,
[attr]
, *
:before
, :hover
A B
, A>B
, A+B
, A~B
Value 不同的兩個定義,被套用在同一個元素上時……
Specificity:Selector 精準與否
Source: CSS Tricks
用字典順序比較 Specificity。
Specificity 相同的話,後定義者蓋過之前定義者。
div
會被 selector .loud
和 div
選中color
.loud
: 0,0,1,0div
: 0,0,0,1.loud
對 color
的定義Specificty 越小越好
.btn, .panel, .media-box
#header .title
#header .banner nav ul li .title /* Avoid! */
box-shadow
text-shadow
設定 duration 和 property
blur
saturate
給不同 device 不同的 CSS
「max-width: Npx
」 就是 「width <= Npx
」
「min-width: Npx
」 就是 「Npx <= width
」
若網頁寬度 > 螢幕寬度
手機瀏覽器會自動縮小網頁
<meta name="viewport" content="width=device-width, initial-scale=1">
Browser Compability
inline-block
:checked
pseudo class.body {
color: red; /* all browsers, of course */
color : green\9; /* IE8 and below */
*color : yellow; /* IE7 and below */
_color : orange; /* IE6 */
}
<!--[if lt IE 9]>
According to the conditional comment this is IE lower than 9<br />
<![endif]-->
微軟官方推薦的作法
只有 IE 會理會