2023-10-13 09:00:00

文本的轮廓效果

在CSS中有什么方法可以给不同颜色的文本提供轮廓吗?我想突出我的文本的一些部分,使其更直观-如名称,链接等。改变链接的颜色等是常见的现在,所以我想要一些新的东西。


当前回答

字母背后的简单轮廓。 适用于任何字体。任意轮廓宽度。

<style>
  text {
    stroke-linejoin: round;
    text-anchor: middle;
    fill: black;
    stroke: white;
    stroke-width: 12px;
    paint-order: stroke;
  }
</style>

<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">
  <text x="50%" y="50%" dominant-baseline="central" text-anchor="middle">Text</text>
</svg>

Codepen: https://codepen.io/RilDev/pen/vYeOjgj?editors=1000

其他回答

我用6种不同的阴影得到了更好的结果:

.strokeThis{
    text-shadow:
    -1px -1px 0 #ff0,
    0px -1px 0 #ff0,
    1px -1px 0 #ff0,
    -1px 1px 0 #ff0,
    0px 1px 0 #ff0,
    1px 1px 0 #ff0;
}

字母背后的简单轮廓。 适用于任何字体。任意轮廓宽度。

<style>
  text {
    stroke-linejoin: round;
    text-anchor: middle;
    fill: black;
    stroke: white;
    stroke-width: 12px;
    paint-order: stroke;
  }
</style>

<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">
  <text x="50%" y="50%" dominant-baseline="central" text-anchor="middle">Text</text>
</svg>

Codepen: https://codepen.io/RilDev/pen/vYeOjgj?editors=1000

我很抱歉,但是如果您已经了解到这里,您将意识到所有其他答案都是不够的,或者需要SVG。

Note: This beautiful outlining will cost you the small price of maintaining a copy of the text in a data-content attribute, which is used by a layered CSS3 ::before element that renders the fill text over the actual outline. .outline { color: black; -webkit-text-stroke: 0.15em black; } .outline::before { content: attr(data-content); -webkit-text-fill-color: white; -webkit-text-stroke: 0; position: absolute; } .outline-wrapper { font-family: "DaytonaW01-CondensedBold", Impact, Times, serif; font-size: 50px; } <div class="outline-wrapper"> <div class="outline" data-content="Take-Two v Anderson">Take-Two v Anderson </div> </div> <link href="https://db.onlinewebfonts.com/c/998b5de3cda797387727724405beb7d4?family=DaytonaW01-CondensedBold" rel="stylesheet" type="text/css"/>

外部样式链接只是为了包含字体,而不是必需的——尽管使用细字体可能需要调整描边参数。

h1 { 颜色:黑色; -webkit-text-fill-color:白色;/*将覆盖颜色(不管顺序)*/ -webkit-text-stroke-width: 1 px; -webkit-text-stroke-color:黑色; } <标题>正确抚摸!< / h1 >

只要加上这个答案。“描边”和“提纲”是不一样的。

提纲看起来很棒。抚摸看起来很可怕。

其他地方列出的SVG解决方案也存在同样的问题。如果你想要一个大纲,你需要把文本放两遍。一次抚摸,又一次不抚摸。

描边不是勾勒轮廓

body { font-family: sans-serif; margin: 20px; } .stroked { color: white; -webkit-text-stroke: 1px black; } .thickStroked { color: white; -webkit-text-stroke: 10px black; } .outlined { color: white; text-shadow: -1px -1px 0 #000, 0 -1px 0 #000, 1px -1px 0 #000, 1px 0 0 #000, 1px 1px 0 #000, 0 1px 0 #000, -1px 1px 0 #000, -1px 0 0 #000; } .thickOutlined { color: white; text-shadow: 0.0px 10.0px 0.02px #000, 9.8px 2.1px 0.02px #000, 4.2px -9.1px 0.02px #000, -8.0px -6.0px 0.02px #000, -7.6px 6.5px 0.02px #000, 4.8px 8.8px 0.02px #000, 9.6px -2.8px 0.02px #000, -0.7px -10.0px 0.02px #000, -9.9px -1.5px 0.02px #000, -3.5px 9.4px 0.02px #000, 8.4px 5.4px 0.02px #000, 7.1px -7.0px 0.02px #000, -5.4px -8.4px 0.02px #000, -9.4px 3.5px 0.02px #000, 1.4px 9.9px 0.02px #000, 10.0px 0.8px 0.02px #000, 2.9px -9.6px 0.02px #000, -8.7px -4.8px 0.02px #000, -6.6px 7.5px 0.02px #000, 5.9px 8.0px 0.02px #000, 9.1px -4.1px 0.02px #000, -2.1px -9.8px 0.02px #000, -10.0px -0.1px 0.02px #000, -2.2px 9.8px 0.02px #000, 9.1px 4.2px 0.02px #000, 6.1px -8.0px 0.02px #000, -6.5px -7.6px 0.02px #000, -8.8px 4.7px 0.02px #000, 2.7px 9.6px 0.02px #000, 10.0px -0.6px 0.02px #000, 1.5px -9.9px 0.02px #000, -9.3px -3.6px 0.02px #000, -5.5px 8.4px 0.02px #000, 7.0px 7.2px 0.02px #000, 8.5px -5.3px 0.02px #000, -3.4px -9.4px 0.02px #000, -9.9px 1.3px 0.02px #000, -0.8px 10.0px 0.02px #000, 9.6px 2.9px 0.02px #000, 4.9px -8.7px 0.02px #000, -7.5px -6.7px 0.02px #000, -8.1px 5.9px 0.02px #000, 4.0px 9.2px 0.02px #000, 9.8px -2.0px 0.02px #000, 0.2px -10.0px 0.02px #000, -9.7px -2.3px 0.02px #000, -4.3px 9.0px 0.02px #000, 7.9px 6.1px 0.02px #000 } svg { font-size: 40px; font-weight: bold; width: 450px; height: 70px; fill: white; } .svgStroke { fill: white; stroke: black; stroke-width: 20px; stroke-linejoin: round; } <h1 class="stroked">Properly stroked!</h1> <h1 class="outlined">Properly outlined!</h1> <h1 class="thickStroked">Thickly stroked!</h1> <h1 class="thickOutlined">Thickly outlined!</h1> <svg viewBox="0 0 450 70"> <text class="svgStroke" x="10" y="45">SVG Thickly Stroked!</text> </svg> <svg viewBox="0 0 450 70"> <text class="svgStroke" x="10" y="45">SVG Thickly Outlined!</text> <text class="svgText" x="10" y="45">SVG Thickly Outlined!</text> </svg>

PS:我想知道如何使SVG是任何任意文本的正确大小。我感觉这相当复杂,包括生成SVG,用Javascript查询它以获得范围,然后应用结果。如果有更简单的非js方式,我很想知道。


更新:正如@12me21指出的那样,在SVG中,你可以使用paint-order="stroke"在填充之前使它成为stroke,然后你就不必重复文本了

svg { 字体大小:40像素; 粗细:大胆的; 宽度:450 px; 身高:70 px; 填充:白色; } .svgText { 填充:白色; 中风:黑色; 笔划宽度:15 px; stroke-linejoin:圆形; paint-order:中风; } <svg viewBox="0 0 450 70"> <text class="svgText" x="10" y="45">< /文本> < / svg >