border-left 是 border 的細分屬性,只控制左側那一條邊。當設計只需要一條左側線條時,它比 border 更直接。
border-left: border-width || border-style || border-color;
三個值分別是寬度、樣式、顏色,順序不拘。
div { border-left: 2px solid red; }
意思是:左邊框 2px 寬、實線、紅色。
拆開寫:
border-left-width: 2px;
border-left-color: #00F;
border-left-style: dashed;
合併寫:
border-left: 2px dashed #00F;
程式碼量明顯下降,效果完全相同,也更有利於頁面載入。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>border-left 範例</title>
<style>
.exp { border-left: 3px solid #F00; }
</style>
</head>
<body>
<div class="exp">左邊框範例</div>
</body>
</html>
左邊框最常見的用途就是做引用樣式:
blockquote {
border-left: 4px solid #E5E5E5;
padding: 8px 0 8px 16px;
margin: 16px 0;
color: #555;
}
一條左側色條加上內距,就能讓引言從內文中清楚獨立出來,且不干擾右側的閱讀節奏。
border-left 的語法、結構、用法與 border 完全一致,唯一差別是作用範圍只有左邊。其他三邊對應 border-right、border-top、border-bottom,寫法可以互相類推。