這份筆記把 <div> 排版時最常用到的 CSS 屬性依功能分類整理,適合當作速查表使用。
color: #999999; /* 文字顏色 */
font-family: 宋體; /* 字型 */
font-size: 10pt; /* 文字大小 */
font-style: italic; /* 斜體 */
font-variant: small-caps; /* 小型大寫字母 */
letter-spacing: 1pt; /* 字元間距 */
line-height: 200%; /* 行高 */
font-weight: bold; /* 粗體 */
vertical-align: sub; /* 下標 */
vertical-align: super; /* 上標 */
text-decoration: line-through; /* 刪除線 */
text-decoration: overline; /* 頂線 */
text-decoration: underline; /* 底線 */
text-decoration: none; /* 移除裝飾線 */
text-transform: capitalize; /* 首字母大寫 */
text-transform: uppercase; /* 全大寫 */
text-transform: lowercase; /* 全小寫 */
text-align: right; /* 靠右對齊 */
text-align: left; /* 靠左對齊 */
text-align: center; /* 置中對齊 */
其中 line-height 與 height 設成相同值,就能讓單行文字垂直置中;再搭配 overflow:hidden 隱藏溢出部分,就能做出固定高度的標題列。
background-color: black; /* 背景顏色 */
background-image: url(image/bg.gif); /* 背景圖片 */
background-attachment: fixed; /* 固定背景(不隨捲動) */
background-repeat: repeat; /* 雙向平鋪(預設) */
background-repeat: no-repeat; /* 不平鋪 */
background-repeat: repeat-x; /* 水平平鋪 */
background-repeat: repeat-y; /* 垂直平鋪 */
background-position: 90% 90%; /* 背景圖的 x 與 y 位置 */
A { } /* 所有超連結 */
A:link { } /* 未訪問過 */
A:visited { } /* 已訪問過 */
A:active { } /* 按下的瞬間 */
A:hover { } /* 滑鼠移入 */
撰寫順序請遵守 LVHA(link → visited → hover → active),否則 hover 效果會被 visited 蓋掉。
border-top: 1px solid black;
border-bottom: 1px solid #6699cc;
border-left: 1px solid #6699cc;
border-right: 1px solid #6699cc;
border: 1px solid #6699cc; /* 四邊 */
border: 1px dashed pink; /* 虛線 */
border: 1px solid pink; /* 實線 */
margin: [top][right][bottom][left],也可分別用 margin-left/margin-right/margin-top/margin-bottom。box-sizing:border-box。block(區塊)或 none(隱藏且不佔位)。left/right/none,決定 div 在頁面上的流向。static:預設值,遵循一般文件規則。relative:相對定位。可用 top/left/right/bottom 設定相對於自身原本位置的偏移,元素仍佔據原空間。absolute:絕對定位。以最近的非 static 祖先元素為基準,元素脫離排版流,不佔空間。fixed:固定定位。相對於視窗定位,捲動時不動。font-family: 字型名稱;
font-weight: bold; /* bold|bolder|lighter */
font-size: 16px;
font-style: italic; /* italic|normal|oblique */
color: #333;
text-align: center; /* center|left|right|justify */
text-decoration: underline; /* none|underline|overline|line-through|blink */
text-indent: 2em; /* 首行縮排 */
text-transform: capitalize; /* lowercase|uppercase|capitalize|none */
direction: ltr; /* ltr|rtl */
line-height: 1.5;
word-spacing: 5px; /* 字間距 */
overflow: scroll; /* 永遠顯示捲軸 */
overflow: visible; /* 不顯示捲軸,溢出部分可見(預設) */
overflow: auto; /* 內容超出時才顯示捲軸 */
overflow: hidden; /* 超出部分隱藏 */
z-index: 10; /* 層疊順序,需搭配定位使用 */
cursor: pointer; /* 游標樣式 */
clip: rect(0, 100px, 100px, 0); /* 剪輯矩形,需 position:absolute */
opacity: 0.5; /* 透明度 */
filter: alpha(opacity=50); /* IE 專用的透明度寫法 */
絕對定位讓你能精確地把元素放在頁面的獨立座標上,而不考慮其他元素的配置:
h4{ position: absolute; left: 100px; top: 43px; }
left 是距瀏覽器(或定位參考元素)左側的距離,top 是距頂部的距離,可使用各種長度單位或百分比(百分比相對於定位參考元素的尺寸)。任何元素都能被定位——段落、文字、圖片、影片皆可。
相對定位則是相對於元素「原本該在的位置」做偏移:
i{ position: relative; left: 40px; top: 10px; }
相對定位的元素仍佔據原本的空間,所以後續元素的排版不會被影響。這也是它能用來做「微調」而不破壞版面的原因。使用時要留意偏移量別太大,否則容易與其他元素重疊,把頁面弄得凌亂。
寬高與可視性:
div{
position: absolute;
left: 200px;
top: 40px;
width: 150px; /* 限制文字向右流動的寬度 */
height: 150px;
}
h4{ visibility: hidden; }
visibility 有三個值:visible(可見)、hidden(隱藏,但仍佔據原有空間)、inherit(繼承父元素,為預設值)。因為 hidden 仍佔位,如果把文字包在一張隱藏的圖片周圍,畫面上會看到文字繞著一塊空白——這與 display:none 的行為完全不同,是兩者最關鍵的差異。
層疊順序 z-index:預設情況下,元素的堆疊順序就是它在 HTML 中的出現順序,後出現的疊在先出現的上面。z-index 用整數定義同層元素的堆疊順序,值大的在上層;正值群疊在父元素之上,負值群疊在父元素之下。使用時該元素必須是 positioned(relative/absolute/fixed),否則 z-index 不生效。
剪輯 clip:只顯示元素的某個矩形區域,其餘透明。語法為 clip: rect(top, right, bottom, left),四個值相對於元素左上角,可用長度或 auto,不能用百分比,且必須搭配 position:absolute。這在多媒體頁面中常用來做文字「劃入」、圖片漸顯等效果。
溢出 overflow:當固定了 width 與 height,內容可能超出可用空間。此時 overflow 決定瀏覽器如何處理:預設值容許內容溢出可見;設為 clip 會在底部與右側裁切;設為 scroll 同樣裁切,但應該同時提供捲軸讓使用者能捲動查看被裁掉的內容。