虛線靠的是邊框屬性中的 dashed 樣式。為了方便觀察,以下範例的寬度都設為 350px、高度 50px。
.hackhome { border: 1px dashed #000; height: 50px; width: 350px; }
HTML:<div class="hackhome">我的四邊為黑色虛線邊框</div>
這裡用的是邊框的縮寫形式,一次設定四邊為 1px 黑色虛線。
.hackhome-1 { border-left: 1px dashed #000; height: 50px; width: 350px; }
.hackhome-2 { border-right: 1px dashed #000; height: 50px; width: 350px; }
.hackhome-3 { border-top: 1px dashed #000; height: 50px; width: 350px; }
.hackhome-4 { border-bottom: 1px dashed #000; height: 50px; width: 350px; }
例如右邊完全不要邊框,其餘三邊保留黑色虛線:
.hackhome-5 {
border: 1px dashed #000;
border-right: 0;
height: 50px;
width: 350px;
}
寫法的關鍵在順序:先一口氣把四邊都設成 1px 虛線,再用 border-right:0 把其中一邊清掉。由於 CSS 後來者優先,最後就等於只設定了三邊。反過來寫(先設右邊為 0,再寫 border 縮寫)就會被整個覆蓋掉,這點要特別小心。
如果還想讓虛線更「虛」,可以試試 dotted,它呈現的是圓點而非短橫線,在分隔線或佔位區的場合很常用。