使用CSS的虚线/虚线圆形 - 在Chrome中无法渲染

Gab*_*hen 4 css internet-explorer svg google-chrome angularjs

我们正在尝试渲染一个可以放置数字的圆圈.我希望圆圈使用实线,虚线或虚线边框.此外,颜色可以变化,并且它将全部在CSS中定义,因此尝试使用图像将不是最佳的.

circle-score-label {
  height: 30px;
  width: 30px;
}

circle-score-label .circle {
  margin-left: auto;
  margin-right: auto;
  border-radius: 50%;
  width: 100%;
  height: 100%;
  position: relative;
  border: 3px solid black;
}

circle-score-label .solid-conf {
  border-style: solid;
}

circle-score-label .dotted-conf {
  border-style: dotted;
}

circle-score-label .dashed-conf {
  border-style: dashed;
}
Run Code Online (Sandbox Code Playgroud)

在IE11中它似乎渲染得很好.在Chrome(目前为42.0.2311.135米或Canary)中,圆圈顶部有一个间隙.

Chrome示例:

在此输入图像描述

IE示例:

在此输入图像描述

有没有人遇到过这个问题以及如何解决它?

Har*_*rry 8

这些差异是预期的,因为每个浏览器呈现它的方式,我们无法控制它.如果你还需要支持FireFox,那么我建议远离这种方法,因为FF现在不能显示虚线边框.

您最好的选择是使用SVG来实现这一点,因为SVG允许通过使用stroke-dasharray属性更好地控制点/破折号.

下面是一个示例片段:( 虽然你没有标记SVG,但我给出了这个,因为SVG可能是最适合这类的东西,这个例子可以指导你.)

svg{
  height: 100px;
  width: 100px;
}
circle {
  fill: transparent;
  stroke: green;
  stroke-width: 3;
}
.solid{
  stroke-dasharray: none;
}
.dashed {
  stroke-dasharray: 8, 8.5;
}
.dotted {
  stroke-dasharray: 0.1, 12.5;
  stroke-linecap: round;
}
div {
  height: 100px;
  width: 100px;
  color: green;
  font-weight: bold;
  text-align: center;
  line-height: 100px;
}
Run Code Online (Sandbox Code Playgroud)
<svg viewBox="0 0 120 120">
  <circle cx="55" cy="55" r="50" class="solid" />
  <foreignObject x="5" y="5" height="100px" width="100px">
    <div>100</div>
  </foreignObject>
</svg>

<svg viewBox="0 0 120 120">
  <circle cx="55" cy="55" r="50" class="dashed" />
  <foreignObject x="5" y="5" height="100px" width="100px">
    <div>100</div>
  </foreignObject>
</svg>

<svg viewBox="0 0 120 120">
  <circle cx="55" cy="55" r="50" class="dotted" />
  <foreignObject x="5" y="5" height="100px" width="100px">
    <div>100</div>
  </foreignObject>
</svg>
Run Code Online (Sandbox Code Playgroud)

几乎所有版本的Chrome,Firefox,Safari,Opera和IE9 +都支持SVG.

使用a foreignObject来定位文本是我发现更容易使用和样式但是它在IE中不起作用.因此,您可以使用textSVG元素,如下面的代码段所示.

svg{
  height: 100px;
  width: 100px;
}
circle {
  position: relative;
  fill: transparent;
  stroke: green;
  stroke-width: 3;
}
.solid{
  stroke-dasharray: none;
}
.dashed {
  stroke-dasharray: 8, 8.5;
}
.dotted {
  stroke-dasharray: 0.1, 12.5;
  stroke-linecap: round;
}
text {
  width: 100px;
  text-anchor: middle;
  fill: green;
  font-weight: bold;
  text-align: center;
}
Run Code Online (Sandbox Code Playgroud)
<svg viewBox="0 0 120 120">
  <circle cx="55" cy="55" r="50" class="solid" />
  <text x="55" y="60">
    100
  </text>
</svg>

<svg viewBox="0 0 120 120">
  <circle cx="55" cy="55" r="50" class="dashed" />
  <text x="55" y="60">
    100
  </text>
</svg>

<svg viewBox="0 0 120 120">
  <circle cx="55" cy="55" r="50" class="dotted" />
  <text x="55" y="60">
    100
  </text>
</svg>
Run Code Online (Sandbox Code Playgroud)

为了支持较低版本的IE,您可以查看一些像这样的库或参考这个SO答案.


这可以通过CSS使用除了border也可以使用其他属性来完成,但是这些要么需要大量的盒阴影或伪元素,对于较大的圆圈也不是很合适.

使用伪元素方法需要CSS transform,因此在不使用其他库的情况下仍然不支持IE8或更少.

虽然它具有更好的浏览器支持,但是盒式阴影方法的可扩展性不是很高.下面是使用box-shadow方法创建虚线边框的示例代码段.这是改编自The Pragmatick在这个帖子中的答案.

div {
  position: relative;
  height: 100px;
  width: 100px;
  margin: 10px;
  text-align: center;
  line-height: 100px;
  border-radius: 50%;
}
.dotted {
  box-shadow: 0px -55px 0px -48px blue, 0px 55px 0px -48px blue, 55px 0px 0px -48px blue, -55px 0px 0px -48px blue, -39px -39px 0px -48px blue, 39px -39px 0px -48px blue, -39px 39px 0px -48px blue, 39px 39px 0px -48px blue, -22px -51px 0px -48px blue, -51px 22px 0px -48px blue, 51px -22px 0px -48px blue, -51px -22px 0px -48px blue, 51px 22px 0px -48px blue, 22px 51px 0px -48px blue, -22px 51px 0px -48px blue, 22px -51px 0px -48px blue;
}
Run Code Online (Sandbox Code Playgroud)
<div class="dotted">
  100
</div>
Run Code Online (Sandbox Code Playgroud)