阿里云主机折上折
  • 微信号
Current Site:Index > Text outline

Text outline

Author:Chuan Chen 阅读数:35557人阅读 分类: CSS

Text outlining is a common visual effect that can make text stand out against complex backgrounds or serve as a decorative element to enhance design appeal. CSS3 offers multiple methods to achieve text outlining, including text-stroke, text-shadow, and SVG-based solutions, each with its own characteristics.

Using text-stroke for Outlining

text-stroke is a CSS3 property specifically designed for text outlining. It includes two sub-properties:

  • -webkit-text-stroke-width: Controls the outline width (e.g., 2px).
  • -webkit-text-stroke-color: Defines the outline color (e.g., #ff0000).
.stroke-example {
  font-size: 48px;
  -webkit-text-stroke: 2px #000;
  color: transparent; /* Hide the fill color to show only the outline */
}

Effect Comparison:

  • By default, the outline is overlaid on the text's original color. To retain only the outline, set color to transparent.
  • In terms of compatibility, text-stroke is only supported in WebKit-based browsers (Chrome, Safari) and requires the -webkit- prefix.

Simulating Outlines with text-shadow

text-shadow can simulate outlining by stacking multiple shadows, offering better compatibility but slightly more complex code.

Single-Direction Outline:

.shadow-simple {
  text-shadow: 2px 0 0 #000, -2px 0 0 #000, 0 2px 0 #000, 0 -2px 0 #000;
}

More Detailed 8-Direction Outline:

.shadow-detail {
  text-shadow: 
    -1px -1px 0 #000,  
    1px -1px 0 #000,
    -1px 1px 0 #000,
    1px 1px 0 #000,
    0 -2px 0 #000,
    0 2px 0 #000,
    -2px 0 0 #000,
    2px 0 0 #000;
}

Pros and Cons:

  • Pros: Supported by all modern browsers; can achieve gradient outlines by adjusting shadow blur values.
  • Cons: Multiple shadows may impact performance, and perfect rounded outlines are difficult to achieve.

SVG for Dynamic Outlines

SVG's <text> element combined with the stroke property allows for more flexible outlining, even supporting animations:

<svg width="300" height="100">
  <text 
    x="50%" y="50%" 
    font-size="40" 
    text-anchor="middle" 
    dominant-baseline="middle"
    fill="none" 
    stroke="#000" 
    stroke-width="2">
    Outlined Text
  </text>
</svg>

Advanced Techniques:

  • Use stroke-dasharray and stroke-dashoffset to create outline animations:
text {
  stroke-dasharray: 1000;
  stroke-dashoffset: 1000;
  animation: draw 3s forwards;
}
@keyframes draw {
  to { stroke-dashoffset: 0; }
}

Hybrid Solutions and Creative Effects

Combining multiple techniques can achieve unique effects, such as:

Gradient Outlined Text:

.gradient-stroke {
  color: white;
  -webkit-text-stroke: 2px transparent;
  background: linear-gradient(90deg, red, blue);
  -webkit-background-clip: text;
  text-shadow: none;
}

Double Outlines (Inner and Outer):

.double-stroke {
  color: yellow;
  text-shadow: 
    -1px -1px 0 #000,  
    1px -1px 0 #000,
    -1px 1px 0 #000,
    1px 1px 0 #000,
    -3px -3px 0 red,
    3px -3px 0 red,
    -3px 3px 0 red,
    3px 3px 0 red;
}

Practical Considerations

  1. Performance Optimization: Avoid excessive use of multi-layer text-shadow on mobile devices.
  2. Accessibility: Ensure outlined text meets WCAG contrast standards against the background.
  3. Responsive Design: Adjust outline width for smaller screens using media queries to prevent blurry text:
@media (max-width: 600px) {
  .responsive-stroke {
    -webkit-text-stroke-width: 1px;
  }
}

本站部分内容来自互联网,一切版权均归源网站或源作者所有。

如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn

上一篇:文字渐变效果

下一篇:文字换行规则

Front End Chuan

Front End Chuan, Chen Chuan's Code Teahouse 🍵, specializing in exorcising all kinds of stubborn bugs 💻. Daily serving baldness-warning-level development insights 🛠️, with a bonus of one-liners that'll make you laugh for ten years 🐟. Occasionally drops pixel-perfect romance brewed in a coffee cup ☕.