DIV 是最基本的排版元素,圍繞它的日常操作卻相當瑣碎。以下把常見需求整理成可查的清單。
相關屬性:
overflow: visible | auto | hidden | scroll
overflow-x: 橫向
overflow-y: 縱向
四個值的意義:
<div style="overflow: auto;">...</div>
<div style="overflow-y: auto;">...</div>
<!-- 只要水平滾動條,需搭配禁止換行 -->
<div style="overflow-x:auto; height:40px; width:100px; white-space:nowrap;"></div>
IE 專屬的顏色屬性:
scrollbar-3dlight-color 亮邊框顏色
scrollbar-highlight-color 3D 亮邊顏色
scrollbar-face-color 3D 表面顏色
scrollbar-arrow-color 方向箭頭顏色
scrollbar-shadow-color 3D 暗邊顏色
scrollbar-darkshadow-color 暗邊框顏色
scrollbar-base-color 基準顏色
scrollbar-track-color 拖曳區域顏色
Chrome / Safari 使用 webkit 私有偽元素:
::-webkit-scrollbar-track-piece {
background-color: #fff;
-webkit-border-radius: 0;
}
::-webkit-scrollbar {
width: 8px;
height: 8px;
}
::-webkit-scrollbar-thumb:vertical {
height: 50px;
background-color: #999;
-webkit-border-radius: 4px;
outline: 2px solid #fff;
outline-offset: -2px;
border: 2px solid #fff;
}
::-webkit-scrollbar-thumb:hover {
background-color: #9f9f9f;
}
::-webkit-scrollbar-thumb:horizontal {
width: 5px;
background-color: #ccc;
-webkit-border-radius: 6px;
}
Firefox 現在也支援標準化的 scrollbar-width 與 scrollbar-color,可優先使用這兩個屬性。
function BigDiv(event) {
if (event.stopPropagation) {
event.stopPropagation();
} else {
event.cancelBubble = true; // IE 舊版
}
}
event.preventDefault();
/* IE 舊版:event.returnValue = false; */
常用於攔截表單提交或連結跳轉。
offsetTop 到上層容器頂部的距離(唯讀)
offsetLeft 到上層容器左側的距離(唯讀)
要寫入位置必須用 style.top / style.left。兩者的差異:
offsetTop 回傳數字,style.top 回傳帶單位的字串,通常需 parseInt 轉換。offsetTop 唯讀,style.top 可讀寫。style.top 回傳空字串。注意:在 Firefox 中,設定的值也必須是帶 px 的字串。
static:預設值,依正常文檔流排列。
relative:以自身原本的位置為基準偏移。偏移量是相對於元素 margin 的左上角計算的。
#B {
position: relative;
padding: 5px;
top: 5px;
left: 5px;
}
absolute:脫離文檔流,以「最近的非 static 祖先元素」為定位基準。若所有祖先都是 static,則以 body(視窗)為基準。這裡常被誤解——「absolute 永遠相對視窗定位」是錯的,那是 fixed 的行為。
<div id="A">
<div id="B">
</div>
</div>
當 A 設為 position: relative,B 的 left: 0; top: 0 就是相對於 A,而非視窗。這個技巧常用來做「限定於某容器內的浮層」。
fixed:特殊的 absolute,永遠以視窗為基準,捲動時位置不變。
var top = $(event.target).offset().top;
var left = $(event.target).offset().left;
offset() 回傳相對於文件的座標。若要把提示框放在事件源旁邊,記得手動補上偏移量,例如 top + 20,否則會蓋住事件源本身。
display: none; /* 完全移除,不佔空間 */
visibility: hidden; /* 保留空間,僅隱形 */
div 預設是塊級、寬度 100%,因此垂直堆疊。加上 float: left 後變成水平排列。
float: none | left | right
none: 預設值,不浮動
left: 向左浮動,文本流向右側
right: 向右浮動,文本流向左側
要在中途切回垂直排列,用 clear:
clear: both | left | right | none;
<div>
<div style="float:left">aaaaaaaaaaaaaa</div>
<div style="float:left">bbbbbbbbbbbbbb</div>
<div style="clear:both"></div> <!-- 清除浮動 -->
<div>這個層垂直排列</div>
<div>這個層垂直排列</div>
</div>
word-spacing 單字間距 normal | <length>
letter-spacing 字母間距 normal | <length>
text-decoration 文字裝飾 none | underline | overline | line-through | blink
vertical-align 垂直對齊 baseline | sub | super | top | middle | bottom | <percentage>
text-transform 大小寫轉換 capitalize | uppercase | lowercase | none
text-align 對齊方式 left | right | center | justify
text-indent 首行縮排 <length> | <percentage>
line-height 行高 normal | <number> | <length> | <percentage>
<p style="letter-spacing:5px; text-align:justify; text-indent:4em; line-height:17pt">
我們看到經過文本屬性處理的文本,字與字之間多了間距,行與行之間多了行高,
對齊方式變成了兩端對齊,並且段首又多縮進了兩格。
</p>
關於 line-height:它決定了行與行之間的呼吸。一般建議設為字級的 1.5 到 2 倍。中文長段落若沒有設定行高,閱讀體驗會很差。CSS 中的行高是均分後加在該行的上下兩側——設為 20px 時,每行文字上下各有 10px 的空間。
<a href="#" title="說明一
說明二
說明三">建站學</a>
或在需要換行處插入 (LF)或 (CR):
<a href="#" title="說明一 說明二 說明三">建站學</a>
Chrome 與 Firefox 預設讓中鍵控制垂直滾動。若 div 只有水平滾動條,需要自己實作。
// 非 Firefox
element.onmousewheel = fireElementScrolled;
// Firefox
element.addEventListener('DOMMouseScroll', fireElementScrolled, false);
fireElementScrolled = function (event) {
var rolled = 0;
if (event.wheelDelta) {
rolled = event.wheelDelta;
} else {
rolled = -event.detail * 30; // Firefox 需換算
}
var htmlElement = document.getElementById('test');
htmlElement.scrollLeft = htmlElement.scrollLeft - rolled;
event.preventDefault();
return false;
};
原理就是取得滾輪位移量,換算後寫入 scrollLeft。Firefox 使用 DOMMouseScroll 事件且 detail 方向相反,需額外處理。
純文字與圖片最簡單,父容器設 text-align: center 即可。單一子 div 則用:
<div id="a" style="width:400px; height:300px; border:1px solid green;">
<div id="b" style="margin:0 auto; width:200px; border:1px solid blue;">
簡單的一個子 DIV 居中問題
</div>
</div>
多個子元素時,常見做法是多加一層:
<div style="width:400px; height:300px; border:1px solid red;">
<div style="margin:0 auto; position:relative; left:50%; float:left;">
<div style="position:relative; left:-50%; float:left">
<div style="border:1px solid blue; text-align:center;">1</div>
<div style="border:1px solid blue; text-align:center;">2</div>
<div style="border:1px solid blue; text-align:center;">3</div>
<div style="border:1px solid blue; float:left;">4</div>
<div style="border:1px solid blue; float:left;">5</div>
<div style="border:1px solid blue; float:left;">6</div>
</div>
</div>
</div>
父元素設 position: relative,子元素絕對定位後用 50% 搭配負 margin:
<div style="width:600px; height:300px; position:absolute;
top:50%; left:50%; margin-top:-150px; margin-left:-300px;
border:1px solid red;">
居中方法
</div>
父元素必須先寫上 width 與 height,否則某些瀏覽器會出現錯位。子元素也必須明確設定 height 與 width,才能算出負 margin 的一半值。
float 排版、負 margin 居中、手寫滾動條樣式,多數已被更好的工具取代:
/* 水平與垂直同時居中 */
.parent {
display: grid;
place-items: center;
}
/* 橫排且間距均等 */
.row {
display: flex;
gap: 8px;
justify-content: space-between;
}
事件處理也不必再判斷瀏覽器——addEventListener 與 event.stopPropagation() 已是唯一寫法。而自訂滾動條樣式請優先使用標準的 scrollbar-width 與 scrollbar-color。
不過底層屬性的行為(offsetTop 與 style.top 的差異、absolute 的定位基準、line-height 的計算方式)並沒有過時,這些仍是除錯時的基本工具。