具有两种颜色的 SVG/CSS 描边虚线 - 可能吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12574787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 20:15:18  来源:igfitidea点击:

SVG/CSS stroke dashed line with two colors - is it possible?

csscolorssvgline

提问by Andrew Mao

Is it possible to use CSS to define a line (or shape edge) with two alternating colors that are dashed? That is, if 1 and 2 are different colored pixels, then

是否可以使用 CSS 来定义具有两种交替颜色的虚线(或形状边缘)?也就是说,如果 1 和 2 是不同颜色的像素,那么

1212121212121212 or 112211221122

1212121212121212 或 112211221122

I basically want some way to use stroke-dasharray with two colors. The line itself is completely colored.

我基本上想要一些方法来使用两种颜色的stroke-dasharray。线条本身是完全彩色的。

If this is not possible, what is a good way to approximate it? For example, I could create a repeated linear gradient with two colors alternating, but this would be hard to set the two colors from javascript.

如果这是不可能的,那么近似它的好方法是什么?例如,我可以创建一个具有两种颜色交替的重复线性渐变,但是这很难从 javascript 设置这两种颜色。

回答by methodofaction

This is not possible in SVG with just one element, but you can use two different rects and then apply a stroke-dashoffset: x...

这在只有一个元素的 SVG 中是不可能的,但是您可以使用两个不同的矩形,然后应用stroke-dashoffset: x...

<svg xmlns="http://www.w3.org/2000/svg">
    <rect class="stroke-red" x="10" y="10" width="101" height="101" />
    <rect class="stroke-green" x="10" y="10" width="101" height="101" />
</svg>


rect.stroke-red {
  stroke: red;
  fill: none;
  stroke-width: 5;
}

rect.stroke-green {
  stroke: green;
  fill: none;
  stroke-dasharray: 5,5; 
  stroke-width: 5;
}

?demo http://jsfiddle.net/aMCsY/

? 演示http://jsfiddle.net/aMCsY/

回答by Colin Young

Building on the answer from @duopixel, you can use the stroke-dasharrayproperty to build up a fairly complex pattern with multiple colors:

基于@duopixel 的答案,您可以使用该stroke-dasharray属性来构建具有多种颜色的相当复杂的图案:

.L4 {
    stroke: #000;
    stroke-dasharray: 20,10,5,5,5,10;
}
.L5 {
    stroke: #AAA;
    stroke-dasharray: 0,20,10,15,10,0
}
.L6 {
    stroke: #DDD;
    stroke-dasharray: 0,35,5,15
}

See http://jsfiddle.net/colin_young/Y38u9/demonstrating lines and a circle with the composite dash pattern.

请参阅http://jsfiddle.net/colin_young/Y38u9/演示线条和带有复合虚线图案的圆圈。

Updated with SO snippet:

更新了 SO 片段:

svg {
    width: 100%;
    height: 160px;
}
path, circle {
    stroke-width: 4;
}
text {
    alignment-baseline: central;
    font-family: sans-serif;
    font-size: 10px;
    stroke-width: 0;
    fill: #000;
    text-anchor: middle;
}
.dim {
    stroke: #AAA;
    stroke-width: 1;
    stroke-dasharray: 1, 1;
}
circle.dim {
    fill: #FFF;
}
.L4 {
    stroke: #000;
    stroke-dasharray: 20, 10, 5, 5, 5, 10;
}
.L5 {
    stroke: #AAA;
    stroke-dasharray: 0, 20, 10, 15, 10, 0
}
.L6 {
    stroke: #DDD;
    stroke-dasharray: 0, 35, 5, 15
}
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
    <g fill="none" stroke="black">
        <path class="dim" d="M5 20 l0 80" />
        <path class="dim" d="M25 20 l0 80 l-10 20" />
        <path class="dim" d="M35 20 l0 80 l-10 30" />
        <path class="dim" d="M40 20 l0 120" />
        <path class="dim" d="M45 20 l0 80 l10 30" />
        <path class="dim" d="M50 20 l0 80 l10 20" />
        <path class="dim" d="M60 20 l0 80 l15 10" />

        <text x="5" y="110">0</text>
        <text x="5" y="125">20</text>
        <text x="25" y="135">30</text>
        <text x="40" y="150">35</text>
        <text x="55" y="140">40</text>
        <text x="65" y="125">45</text>
        <text x="82" y="115">55</text>

        <path class="L4" d="M5 20 l215 0" />
        <path class="L5" d="M5 20 l215 0" />
        <path class="L6" d="M5 20 l215 0" />

        <!-- separated to show composition -->
        <text x="5" y="70" style="text-anchor:start">Separated to show composition:</text>
        <path class="L4" d="M5 80 l215 0" />
        <path class="L5" d="M5 90 l215 0" />
        <path class="L6" d="M5 100 l215 0" />

        <circle class="L4" cx="400" cy="80" r="60" />
        <circle class="L5" cx="400" cy="80" r="60" />
        <circle class="L6" cx="400" cy="80" r="60" />
    </g>
</svg>

回答by reisio

One way:

单程:

<!doctype html>
<html>
	<head>
		<title></title>
		<style>
div {
	width: 100px;
	height: 100px;
}
.dashbox {
	border: 4px dashed blue;
	background: orange;
}
.dashbox > div {
	background: white;
}
		</style>
	</head>
	<body>
		<div class="dashbox">
			<div></div>
		</div>
	</body>
</html>

That is, layer one element with one color behind another with another color [in the form of a dashed stroke].

也就是说,将具有一种颜色的一个元素叠加在另一种颜色的另一个元素之后[以虚线笔划的形式]。

回答by eddieberklee

For a border that has 50 dashes along the bottom, create 50 divs with increasing margin-left, and an overall container with overflow:hidden.

对于底部有 50 个破折号的边框,创建 50 个 divmargin-left并带有overflow:hidden.