35

是否可以用css制作虚线文本?

我知道显而易见的事情是使用点字体,但如果我只需要谨慎地使用点文本,那么让用户下载整个字体可能会过度。

我的想法是用带有白色背景的小透明圆圈的背景图案的伪元素覆盖文本。

像这样的一些事情:

<div class="dottedText">Some dotted text</div>

在此处输入图像描述

小提琴

CSS

.dottedText:after
{
    content: '';
    position:absolute;
    left:0;
    top:0;
    width: 100%;
    height: 100%;
    background: radial-gradient(circle, transparent 50%, transparent 50%),
    radial-gradient(circle, transparent 20%, white 50%) 30px 30px;
    background-size:4px 4px;
}

我想我可能很接近,但是如果您更改字体大小,上述解决方案将无法正常工作。

我正在寻找一个解决方案

1)随着字体大小的增加,点的大小会增加,并且

2)最好每个字母只显示一行点 - 而不是现在的双线。

编辑:当我说一行点时 - 我的意思是每个笔划应该只由一个点组成。例如:在上图中,请注意“m”字符有 2 列点....好吧,我只喜欢一个。

理想情况下是这样的(取自这里):

在此处输入图像描述

(我不确定,但可能需要调整径向渐变才能做到这一点)

编辑:

1)我不介意使用哪种字体 - 只要它是内置字体。(即使是等宽字体也可以)

2)该解决方案不需要在每个浏览器中都有效。(所以只有 webkit 的解决方案就可以了)

4

7 回答 7

19

老实说,这个答案可能听起来很有趣或很奇怪,但我不确定它是否可能仅使用 CSS (因为您没有标记任何其他语言),即使它这样做,这样做也是一种矫枉过正,因此它使用虚线字体而不是编写太多的 CSS 行是有意义的。

即使你排除了IE,你也只有一个.woff文件,我认为这是非常正常的,因为它会使你的http请求增加一个,而且肯定不会像你想象的那么臃肿。

可以在这里找到很酷的虚线字体列表。转换ttf,使用字体松鼠服务

确保您有权这样做。


使用的演示字体: Dotline

(文件托管在我自己的服务器上,启用了CORS,因为演示在 Firefox 上失败)

如果您不希望支持糟糕的 IE,那么您需要的唯一文件就是woff23kb

于 2014-06-10T12:30:21.117 回答
8

即使它依赖于 SVG 内联样式,这也是我带来的:

<svg xmlns="http://www.w3.org/2000/svg"
 width="1450px" height="300px" viewBox="0 0 800 300">

   <text x="2" y="155" 
    font-family="'Lucida Grande', sans-serif" 
    font-size="222"
    stroke="red"
    stroke-width="3"
    stroke-linecap="round"
    stroke-dasharray="5,5"
    fill="none">
             Some dotted text
</text>

尽管由于某些原因,stroke-linecap 不起作用..

如果你想玩一个工作小提琴检查这个

EDIT-1(将 svg 样式移动到 CSS)

    svg{ 
        width:1450px;
        height:300;
        viewBox:0 0 1500 300;
      
    }
    text{
      font-family:'Lucida Grande', sans-serif;
      font-size:152px;
      stroke:#000ece;
      stroke-width:3px;
      stroke-linecap:round;
      stroke-dasharray:1,1;
      fill:none;
    
    }
<div class="dott">
<svg xmlns="http://www.w3.org/2000/svg">

  <text  x="2" y="155" >
        

    Some dotted text

  </text></div>

于 2014-12-24T23:46:55.390 回答
3

通过一些小的调整,我们可以非常接近:

1)font-family换新快递

2)text-shadow在div上添加一个水平和垂直偏移量

3)将单位更改为ems- (如@BDawg建议)

小提琴

div {
  font-size: 40px;
  font-family: courier new;
  position: relative;
  text-shadow: -.03em -.03em 0 black;
}
.dottedText:after {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: radial-gradient(circle, transparent 50%, transparent 50%), radial-gradient(circle, transparent 20%, white 50%) 30px 30px;
  background-size: .1em .1em;
}
div + div {
  font-size: 60px;
}
div + div + div {
  font-size: 80px;
}
div + div + div + div {
  font-size: 100px;
}
<div class="dottedText">The quick brown fox</div>
<div class="dottedText">The quick brown fox</div>
<div class="dottedText">The quick brown fox</div>
<div class="dottedText">The quick brown fox</div>

于 2014-12-31T11:43:40.533 回答
2

Short Answer:

No. Not possible.

tl;dr;

I'm looking for a solution where

1) The dots will increase in size as font-size increase, and

2) preferably each letter should only be shown with only one single line of dots - not the double line as it is now.

Edit: When I say one single line of dots - I mean that each stroke should be made up of only one dot. For example: In the above picture notice that the 'm' char has 2 columns of dots....well I would prefer only one.

This cannot be done without a custom font.

There are two inherent problems with other workarounds:

  1. There is no text-fill-pattern in CSS. Not even in SVG. There is text-fill-color in both CSS and SVG. However, it is limited to browser-specific implementation and non-standard vendor-prefixes in CSS. Then there is stroke style. It has the same limitations in CSS (as that of fill) of being non-standard, and also is limited only to width and color. Although, SVG adds stroke-linecap and stroke-dasharray, but that is all there is.

  2. text-outline could have helped. If it worked like a border, then we could have done a text-outline: Npx dotted red;. And increase the Npx to virtually eliminate the text-fill. But, there are other problems with that: (1) The specs says, it will work as shadow i.e. with no style. As in text-outline: 2px 2px #f00;. There is no solid / dotted / dashed style option. (2) W3C says that the feature is at risk and may be cut from the spec. (3) As of now, it is still not implemented by any browser as yet. Ref: http://www.w3.org/TR/2007/WD-css3-text-20070306/#text-outline

  3. The only way left out, is to use a background pattern and then make it clip to the text. This is very much what you have already tried in your question.

The problem with the last approach (background) is that the fonts are not same. Not even similar. The glyphs are different. The ascenders and descenders are different. Even strokes on the same character are different.

This can be understood by this illustration:

enter image description here

If you notice the characters in the above sample (Times New Roman font), while the vertical lines have nearly same width, the horizontal lines (the horizontal bar in "e") are narrower. Further, the serifs are also of differing widths and taper towards the end. When a background with a pattern is applied (any mechanism, image or SVG or radials), it will not line-up neatly with the font lines. Because of whitespaces and proportional fonts have varying distances.

Notice the two ts in the above illustration marked in red. Even though the glyphs are same, but depending on the distance from the origin, the background pattern cannot line-up neatly. Thus while the second t has the dots lined up, the first t does not. The pattern visible is shifted partly and hence white space is prominent. The same pattern-shift occurs randomly across the characters.

Notice the taper of the serifs and that of e, as marked in red circle in the above illustration. In the middle, the font is fatter and accommodates more dots from the pattern (some full, some partial). At the serifs and tapers, it gets narrower and the pattern cannot fit. With curves, the dots in the pattern cannot bend, it is after all a grid pattern.

We cannot reduce or increase the individual dots in the pattern to fit with the fonts. And we cannot shift background to line-up across all characters. When you use mono-space fonts, then the proportional distance problem is mitigated to some extent, but the curves still remain and the pattern cannot be lined-up with that.

So, the background technique for this is inherently flawed. The only solution is to use a custom font.

However, if approximations are good enough for you, then your own technique of radial background works well. At least apart from Firefox, your own technique works across other browsers.

I will also attempt to provide one more similar solution. Combining SVG pattern with the background-image and keeping the background-size in percent may work to some extent on monospace fonts at larger sizes.

Disclaimer: This snippet will work only with webkit based browsers (Chrome / Safari), because other browsers don't seem to support SVG as background-image and also -webkit-background-clip: text; is, well webkit dependent.

Snippet:

.dotted {
    padding: 0px;
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg'  width='4' height='4'><circle cx='2' cy='2' r='2' fill='#f00' stroke='#fff' stroke-width='1'/></svg>");
    font-family: sans-serif;
    font-weight: 300;
    font-size: 32px; background-size: 0.9%;
    -webkit-font-smoothing: antialiased;
    -webkit-background-clip: text;
    -moz-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
}
div:nth-of-type(2) { font-size: 64px;  }
div:nth-of-type(3) { font-size: 80px;  }
<div class="dotted">Some dotted text</div>
<div class="dotted">Dotted text</div>
<div class="dotted">More dotted text</div>

于 2014-12-31T16:15:03.307 回答
2

你不能只使用这个字体的 webfont 工具包吗?

http://www.fontsquirrel.com/fonts/BPdots?q%5Bterm%5D=dot&q%5Bsearch_check%5D=Y

您只需像这样为您想要的字体类型链接您的 CSS:

@font-face {
    font-family: 'bpdotsbold';
    src: url('BPdotsBold-webfont.eot');
    src: url('BPdotsBold-webfont.eot?#iefix') format('embedded-opentype'),
         url('BPdotsBold-webfont.woff') format('woff'),
         url('BPdotsBold-webfont.ttf') format('truetype'),
         url('BPdotsBold-webfont.svg#bpdotsbold') format('svg');
    font-weight: normal;
    font-style: normal;

}

然后只需链接您要使用此字体的任何元素:

h1{font-family: 'bpdotsbold', arial, helvetica;font-size:80px}

只需确保将 webfonts 的路径上传到您的服务器并更新url('LINKTOFONT')您的 CSS 中的每一个。

font-squirrel 还提供了其他几种点状字体:

http://www.fontsquirrel.com/fonts/list/find_fonts?q%5Bterm%5D=dot&q%5Bsearch_check%5D=Y

于 2014-12-24T20:04:11.100 回答
2

更改background-size为使用 em。

例如:

background-size: 0.1em 0.1em;

注意:以上使用字体更改了您的第一个示例的大小,但不会生成您的第二个示例。如果确切的效果是绝对必须的,我会使用内联 SVG 而不是纯 CSS 方法。(或者更明显的方法:改为点字体)

于 2014-12-30T21:30:56.130 回答
1

此字体看起来与您期望的相似

http://www.fontsquirrel.com/fonts/Synthetique?q[term]=dot&q[search_check]=Y

它有 4 种扩展 [TTF,EOT,WOFF,SVG) 字体,所有浏览器都支持

希望对你有帮助

于 2014-12-27T12:10:29.660 回答