如何用css画一条虚线?


97

如何使用CSS绘制虚线?

Answers:


131

例如:

hr {
  border:none;
  border-top:1px dotted #f00;
  color:#fff;
  background-color:#fff;
  height:1px;
  width:50%;
}

另见的样式<hr>与CSS


3
由于IE 6(不记得IE7)不会理解“虚线”样式,因此您可以告诉他改用“虚线”,当然要使用条件注释来瞄准IE6,而不使用其他浏览器。
FelipeAls

高度:0px;可在Chrome上使用,因为边框与高度是分开的。
2012年

您应该了解,虚线在许多浏览器中看起来可能有所不同。这与虚线更相关。
Rantiev '16

17
<style>
    .dotted {border: 1px dotted #ff0000; border-style: none none dotted; color: #fff; background-color: #fff; }
</style>
<hr class='dotted' />

15

使用HTML:

<div class="horizontal_dotted_line"></div>

并在styles.css中:

.horizontal_dotted_line{
  border-bottom: 1px dotted [color];
  width: [put your width here]px;
} 

13

公认的答案有很多不足之处,现代浏览器不再需要。我已经在IE8之前的所有浏览器上亲自测试了以下CSS,它运行完美。

 hr {
    border: none;
    border-top: 1px dotted black;
  }

border: none必须排在最前面,以删除浏览器应用于hr标记的所有默认边框样式。



7

这行应该为您工作:

<hr style="border-top: 2px dotted black"/>


3

我在这里尝试了所有解决方案,但是没有一个干净的1px线。为此,请执行以下操作:

border: none; border-top: 1px dotted #000;

或者:

 border-top: 1px dotted #000; border-right: none; border-bottom: none; border-left: none;

3

像这样使用:

<hr style="border-bottom:dotted" />

3

为此,您只需要将border-top或添加border-bottom<hr/>标签中,如下所示:

<hr style="border-top: 2px dotted navy" />

您想要的任何线型或颜色


3

将以下属性添加到要具有虚线的元素。

style="border-bottom: 1px dotted #ff0000;"

2

使用hr为我创建了两行,一条实线,一条虚线。

我发现这很好用:

div {
border-top: 1px dotted #cccccc;
color: #ffffff;
background-color: #ffffff;
height: 1px;
width: 95%;
}

另外,因为您可以将宽度设置为百分比,所以宽度总是在两侧都有(即使您调整窗口大小)。



1

元素后的虚线:

http://jsfiddle.net/korigan/ubtkc17e/

的HTML

<h2 class="dotted-line">Lorem ipsum</h2>

的CSS

.dotted-line {
  white-space: nowrap;
  position: relative;
  overflow: hidden;
}
.dotted-line:after {
  content: "..........................................................................................................";
  letter-spacing: 6px;
  font-size: 30px;
  color: #9cbfdb;
  display: inline-block;
  vertical-align: 3px;
  padding-left: 10px;
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.