最直接画虚线用border-style: dashed,但需精确控制时推荐border-image或SVG。1. border-style: dashed生成默认虚线,快捷但不可控;2. border-image结合repeating-linear-gradient可自定义虚线长度、间距;3. SVG通过stroke-dasharray实现复杂图案,支持矢量缩放,适合高精度设计需求。

CSS中画虚线最直接的方法是使用
border-style: dashed;
border-image
dashed
要画虚线,最基础的办法就是利用CSS的
border-style
border-image
1. 基础虚线:border-style: dashed;
这是最简单、最直接的方式。你只需要给元素的
border-style
dashed
立即学习“前端免费学习笔记(深入)”;
.dashed-box {
  border: 2px dashed #333;
  padding: 20px;
  width: 200px;
  height: 100px;
  text-align: center;
}这种方法非常快,但它的缺点也很明显:你无法控制虚线的长度和间距,浏览器会根据自己的算法来渲染,不同浏览器之间可能会有细微的差异。
2. 高级定制:border-image
border-image
核心思路是创建一个包含虚线图案的渐变图像,然后用
border-image-slice
border-image-repeat
.custom-dashed-border-image {
  border: 5px solid transparent; /* 边框宽度必须有,且颜色不重要,因为会被图片覆盖 */
  border-image: linear-gradient(to right, #333 50%, transparent 50%) 1 / 5px 0 / 0 stretch;
  border-image-slice: 1; /* 关键:不切片,整个图片作为边框 */
  border-image-repeat: repeat; /* 让图案重复 */
  /*
    更详细的 border-image 简写属性解释:
    border-image: <'border-image-source'> || <'border-image-slice'> [ / <'border-image-width'>? [ / <'border-image-outset'> ]? ]? || <'border-image-repeat'>?
    这里是:
    linear-gradient(to right, #333 50%, transparent 50%) (source)
    1 (slice)
    / 5px 0 (width: top/bottom 5px, left/right 0)
    / 0 (outset)
    stretch (repeat: here it's repeat, not stretch)
    实际上,对于虚线,我们通常需要 repeat。
  */
  border-image: linear-gradient(to right, #333 50%, transparent 50%) 1 repeat; /* 更简洁的写法 */
  border-image-slice: 1; /* 这行是关键,确保整个渐变作为边框 */
  border-image-width: 5px; /* 边框的宽度 */
  border-image-repeat: repeat; /* 让渐变重复 */
  padding: 20px;
  width: 200px;
  height: 100px;
  text-align: center;
}
/* 更好的 border-image 虚线示例,控制虚线和间距 */
.custom-dashed-gradient {
  border: 5px solid transparent; /* 边框宽度 */
  border-image: repeating-linear-gradient(
    90deg,
    #333 0 10px, /* 虚线长度 10px */
    transparent 10px 20px /* 间距 10px */
  ) 1 stretch; /* slice 1, repeat stretch (等同于 repeat) */
  /* 或者这样写更清晰: */
  /* border-image-source: repeating-linear-gradient(90deg, #333 0 10px, transparent 10px 20px); */
  /* border-image-slice: 1; */
  /* border-image-width: 5px; */
  /* border-image-repeat: stretch; */ /* 这里用 stretch 会根据边框拉伸,repeat 更适合虚线 */
  /* 针对虚线,我个人更倾向于使用 repeat */
  border-image: repeating-linear-gradient(
    90deg,
    #333 0 10px,
    transparent 10px 20px
  ) 1 repeat;
  padding: 20px;
  width: 200px;
  height: 100px;
  text-align: center;
  margin-top: 20px;
}3. 终极自由:SVG背景图
如果说
border-image
.svg-dashed-border {
  /* 创建一个简单的SVG虚线图案 */
  background-image: url("data:image/svg+xml,%3Csvg width='10' height='2' xmlns='http://www.w3.org/2000/svg'%3E%3Crect width='5' height='2' fill='%23333'/%3E%3C/svg%3E");
  background-repeat: repeat-x; /* 水平重复 */
  background-position: left top, left bottom; /* 放置在顶部和底部 */
  background-size: 100% 2px; /* 控制虚线的高度 */
  /* 垂直虚线需要额外的背景图或伪元素 */
  padding: 20px;
  width: 200px;
  height: 100px;
  text-align: center;
  margin-top: 20px;
  position: relative; /* 用于伪元素定位 */
}
/* 如果需要四条边都是虚线,且样式统一,可以使用伪元素 */
.svg-dashed-border::before,
.svg-dashed-border::after {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  height: 2px;
  background-image: url("data:image/svg+xml,%3Csvg width='10' height='2' xmlns='http://www.w3.org/2000/svg'%3E%3Crect width='5' height='2' fill='%23333'/%3E%3C/svg%3E");
  background-repeat: repeat-x;
}
.svg-dashed-border::before {
  top: 0;
}
.svg-dashed-border::after {
  bottom: 0;
}
.svg-dashed-border .vertical-line-left,
.svg-dashed-border .vertical-line-right {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 2px;
  background-image: url("data:image/svg+xml,%3Csvg width='2' height='10' xmlns='http://www.w3.org/2000/svg'%3E%3Crect width='2' height='5' fill='%23333'/%3E%3C/svg%3E");
  background-repeat: repeat-y;
}
.svg-dashed-border .vertical-line-left {
  left: 0;
}
.svg-dashed-border .vertical-line-right {
  right: 0;
}这种SVG方案,如果只是简单的四边虚线,用
border-image
border-style: dashed;
说实话,
border-style: dashed;
首先,它对虚线的“点”和“间距”的控制几乎是零。浏览器有自己的一套算法来渲染
dashed
再者,
dashed
dashed
border-style: dashed;
border-image
border-image
我们通常会用
repeating-linear-gradient
.border-image-example {
  border: 5px solid transparent; /* 必须有边框宽度,但颜色不重要 */
  border-image: repeating-linear-gradient(
    90deg, /* 渐变方向,90deg表示水平方向 */
    #f00 0 10px, /* 从0到10px是红色实线 */
    transparent 10px 20px /* 从10px到20px是透明,形成间距 */
  ) 1 repeat; /* slice 1表示不切片,整个渐变作为边框;repeat表示重复 */
  width: 250px;
  height: 150px;
  padding: 20px;
  box-sizing: border-box;
  text-align: center;
  line-height: 100px;
  font-family: sans-serif;
  color: #333;
  margin: 20px auto;
}这里面的
repeating-linear-gradient(90deg, #f00 0 10px, transparent 10px 20px)
90deg
然后是
border-image
repeating-linear-gradient(...)
border-image-source
1
border-image-slice
1
1
repeat
border-image-repeat
repeat
round
stretch
repeat
通过调整
repeating-linear-gradient
.dot-dash-border-image {
  border: 4px solid transparent;
  border-image: repeating-linear-gradient(
    90deg,
    #007bff 0 4px, /* 点 */
    transparent 4px 8px, /* 点间距 */
    #007bff 8px 18px, /* 划 */
    transparent 18px 22px /* 划间距 */
  ) 1 repeat;
  width: 250px;
  height: 150px;
  padding: 20px;
  box-sizing: border-box;
  text-align: center;
  line-height: 100px;
  font-family: sans-serif;
  color: #333;
  margin: 20px auto;
}这种方法非常灵活,而且浏览器兼容性也相当不错(除了IE9及以下)。我觉得,掌握了
border-image
当
border-image
mask
最常见的做法是创建一个包含虚线图案的SVG,然后将其编码成Data URI,作为
background-image
stroke-dasharray
比如,一个简单的5px实线,5px间距的虚线:
<svg width="10" height="2" xmlns="http://www.w3.org/2000/svg"> <line x1="0" y1="1" x2="10" y2="1" stroke="#333" stroke-width="2" stroke-dasharray="5,5" /> </svg>
这段SVG代码定义了一个宽10px、高2px的画布,并在其中画了一条从(0,1)到(10,1)的直线。
stroke="#333"
stroke-width="2"
stroke-dasharray="5,5"
为了在CSS中使用,我们需要把这段SVG编码成Data URI。有很多在线工具可以做这个,或者手动编码(例如空格变
%20
#
%23
.svg-custom-dashed {
  position: relative;
  width: 250px;
  height: 150px;
  padding: 20px;
  box-sizing: border-box;
  text-align: center;
  line-height: 100px;
  font-family: sans-serif;
  color: #333;
  margin: 20px auto;
  /* 清除默认边框,使用伪元素模拟边框 */
  border: none;
}
/* 使用伪元素创建顶部和底部的虚线 */
.svg-custom-dashed::before,
.svg-custom-dashed::after {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  height: 2px; /* 虚线的高度 */
  background-image: url("data:image/svg+xml,%3Csvg width='10' height='2' xmlns='http://www.w3.org/2000/svg'%3E%3Cline x1='0' y1='1' x2='10' y2='1' stroke='%23333' stroke-width='2' stroke-dasharray='5%2C5'/%3E%3C/svg%3E");
  background-repeat: repeat-x; /* 水平重复 */
}
.svg-custom-dashed::before {
  top: 0;
}
.svg-custom-dashed::after {
  bottom: 0;
}
/* 使用伪元素创建左侧和右侧的虚线 */
.svg-custom-dashed .vertical-line-left,
.svg-custom-dashed .vertical-line-right {
  content: ''; /* 这里不能用 content:'' 因为已经是一个 div 了,所以需要一个真实的元素 */
  position: absolute;
  top: 0;
  bottom: 0;
  width: 2px; /* 虚线的宽度 */
  background-image: url("data:image/svg+xml,%3Csvg width='2' height='10' xmlns='http://www.w3.org/2000/svg'%3E%3Cline x1='1' y1='0' x2='1' y2='10' stroke='%23333' stroke-width='2' stroke-dasharray='5%2C5'/%3E%3C/svg%3E");
  background-repeat: repeat-y; /* 垂直重复 */
}
.svg-custom-dashed .vertical-line-left {
  left: 0;
}
.svg-custom-dashed .vertical-line-right {
  right: 0;
}这种方法提供了无与伦比的控制力。你可以创建任何形状的“点”(比如圆形、星形),任何复杂的重复模式。因为SVG是矢量图,所以缩放不会有任何失真,完美适应高DPI屏幕。缺点是,对于简单的虚线,代码量可能会略大,而且管理Data URI字符串也稍显繁琐。不过,对于那些对设计细节有极致追求的项目,SVG绝对是值得投入的。它不仅仅是画虚线,更像是给你的UI元素披上了一层定制的艺术外衣。
虚线在网页设计中扮演着很多角色,不仅仅是装饰。
常见应用场景:
注意事项:
border-image
border-style: dashed;
border-image
border-style: dashed;
以上就是CSS怎么画虚线_CSS虚线边框与自定义虚线样式教程的详细内容,更多请关注php中文网其它相关文章!
                        
                        每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号