萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> Div CSS 教學

CSS border 虛線邊框完整用法:四邊、單邊、三邊組合

border-style: dashed 產生虛線邊框。透過單向屬性(border-top/right/bottom/left)可精確控制哪幾邊顯示虛線。

速查表

需求CSS 寫法說明
四邊虛線border: 1px dashed #000;縮寫,四邊同樣式
左邊虛線border-left: 1px dashed #000;單邊
右邊虛線border-right: 1px dashed #000;單邊
上邊虛線border-top: 1px dashed #000;單邊
下邊虛線border-bottom: 1px dashed #000;單邊
三邊虛線(無右邊)border: 1px dashed #000; border-right: 0;先設四邊,再覆蓋移除右邊,**順序很重要**

完整範例結構

.hackhome     { border: 1px dashed #000; height: 50px; width: 350px; }
.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; }

HTML 只需對應 class:<div class="hackhome-5">右邊無邊框,其它三邊虛線</div>

補充:虛線間距控制

標準 CSS 無法直接調整虛線長度/間距。若需精確控制,可用:

  • background-image: linear-gradient(...) 模擬虛線(可控制 dash/gap 長度)。
  • border-image 使用 SVG 圖片定義虛線樣式。
  • SVG <path stroke-dasharray="5,3"> 最靈活。
copyright © 萬盛學電腦網 all rights reserved