製作 1x2px 或 2x1px 圖片(半透明/半實色),利用 background-repeat 平鋪:
.dashed-hr {
height: 1px;
background: url('dashed.png') repeat-x 0 0;
/* 或 repeat-y 垂直虛線 */
}
/ * 表格邊框 * /
table.dashed { border: 1px dashed #000; }
/ * 區塊底線 * /
.dashed-bottom { border-bottom: 1px dashed #000; }
/ * 自訂虛線長短(CSS3,部分瀏覽器支援) * /
.dashed-custom {
border: 1px dashed #000;
/* Firefox 支援 */
border-image: linear-gradient(90deg, #000 50%, transparent 50%) 1 100%;
}
dashed。<table style="border-left: 1px dashed #000; border-right: 1px dashed #000;
border-top: 1px dashed #000; border-bottom: 1px dashed #000;">
...
</table>
.precise-dashed {
border: 2px solid transparent;
border-image: repeating-linear-gradient(
90deg,
currentColor 0 6px,
transparent 6px 10px
) 1;
}
.hr-dashed {
height: 2px;
background: repeating-linear-gradient(
90deg,
currentColor 0 8px,
transparent 8px 12px
) center / 100% 2px no-repeat;
}
border-bottom: 1px dashed #ccc; 即可,接受瀏覽器渲染差異。repeating-linear-gradient + border-image 或 background,並提供 IE 降級(實線或圖片)。<pattern> 或 CSS repeating-linear-gradient,效能優於圖片。border-collapse: collapse; 避免雙線重疊。