```你应该掌握的能提升文字设计的 CSS 属性```
CSS properties you should know for better text designs

原始链接: https://master.dev/blog/typographic-css-tricks/

为了将网页排版提升到基础字体之外,CSS 提供了几种功能强大的属性,可以增加视觉趣味性和精确度。以下是五个必备工具: 1. **`background-clip: text`**:允许在文本字符内遮罩图像或渐变,从而创建引人注目的填充字形。 2. **`vertical-align` 和 `align-content`**:`vertical-align` 用于管理行内元素(如图标)相对于文本的对齐方式,而现代的 `align-content` 属性则能够实现块级盒内文本的垂直居中。 3. **`box-decoration-break: clone`**:确保在文本换行或断行时,样式(如边框、内边距和阴影)能均匀地应用于每一行,避免边缘不连贯。 4. **`letter-spacing`**:除了调整可读性外,该属性还是动画的强大工具,通过操纵字符之间的间距,可以实现动态的文本揭示效果。 5. **`text-combine-upright`**:专为垂直书写模式设计,允许短字符串(如日期或度量单位)在垂直布局中水平显示,从而保持整洁、专业的排版效果。 掌握这些属性,你将拥有一个多功能的工具箱,为网页排版增添精致的细节和引人入胜的动态效果。

这篇 Hacker News 讨论帖围绕一篇关于文本设计 CSS 属性的文章展开,用户在其中提出了技术性更正和最佳实践建议。 主要内容包括: * **技术注意事项:** 贡献者指出 `background-clip: text` 存在缺陷,建议使用 `-webkit-text-fill-color: transparent` 以获得更好的兼容性。另有人建议使用 `@supports` 查询来实现更稳健的代码。 * **排版建议:** 评论者认为 `letter-spacing`(字间距)常被滥用,且往往会损害正文的可读性。通常认为该属性仅适用于艺术标题或特定的全大写场景。 * **现代 CSS 资源:** 用户强调应摒弃过时的“黑客”思维。他们建议查阅 **MDN** 文档,并使用 **Baseline**(已集成在 *Can I Use* 中)来判断某项 CSS 特性是否因具备广泛的浏览器支持而可安全用于生产环境。 * **行业转变:** 目前有一种趋势,即利用现代 CSS 取代 JavaScript 来实现弹出窗口和移动端菜单等 UI 元素,从而提升网页性能。普遍共识是:应优先考虑浏览器兼容性和规范标准,而非盲目追逐每一个新特性。
相关文章

原文

Like the graphic design of a physical poster, sometimes we need some eye-catching text on the web too. Picking the right font is just the beginning. Text weight, style, spacing, and decoration can go a long way, but sometimes we need more to make the text stand out.

Here are five properties that can help make words look better, or at least more interesting, on the web. They are all fairly simple implementations you can layer into other ideas.

1. background-clip

The fill of the letterforms is usually a solid color in typography (with some wild exceptions). But it’s rather easy to use an image to fill that space instead. All we’ve got to do is pick a fancy background and snip it to the shape of our text. The result is a set of striking knockouts that can be read.

In the late 2000s, the text value was introduced for the background-clip property, making it possible to mask background images (or gradients) inside live text. Since then, it has remained one of the most sought-after rules for really cool typography visuals.

<p>Belize Reef</p>Code language: HTML, XML (xml)
p {
  
  background: text url("image.jpg") center/auto 1lh;
  
  color: transparent; 
}Code language: CSS (css)

2. vertical-align / align-content

On occasion, the basics build the brilliance.

We all could align text horizontally since we could write CSS (text-align: center;). But when it comes to the vertical axis, the text alignment doesn’t always go smoothly. However, understanding these two properties should resolve most of that.

The vertical-align property hails from the era of the Early Web, when HTML tables ruled the layout landscape. It was widely used to align content inside a table cell. However, outside of a table cell, it’s not really about aligning text vertically; rather, it’s about aligning inline elements to the text.

If something goes into a line of text, like span, img, or input, and has to line up relative to the text, vertical-align is how it’s done.

31 B<span class="emoji">&#x1F4BA;</span>Code language: HTML, XML (xml)
.emoji {
  vertical-align: top;
}Code language: CSS (css)

What we all might have initially thought vertical-align did — aligning text vertically in a box — is actually done by the relatively newer property, align-content.

Evolved in the Modern Layout era, other than flex box and grid, align-content also affects the block box and arranges the box’s content vertically.

<p>Bonjour</p>Code language: HTML, XML (xml)
p {
  width: 360px;
  aspect-ratio: 1;
  text-align: center;

  
  align-content: center;
}Code language: CSS (css)

3. box-decoration-mode

We don’t usually think line by line for typography design. However, if we ever do go line by line, this property is all we need, and frankly, it’s all we have.

There’s a CSS standard module to handle when content breaks along a flow, column, or page, termed fragmentation. But that was all only about deciding on the spacing, wrapping, and location of the break until box-decoration-mode came along to tackle the styling.

When a text’s line box breaks, the broken edge is typically unstyled, causing an awry appearance at the edges of the fragments. But with box-decoration-mode we can ensure all the fragments’ edges are uniformly styled. Which means they all get the borders, shadows, etc. that were applied to the element at the edges.

<span>
water cooler chat <br>
everyone agrees <br>
it is hot
</span>Code language: HTML, XML (xml)
span {
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
  
  border: solid blue;
  border-width: 0 1px 1px 0;
  box-shadow: 2px 2px 3px rgb(171, 171, 245);
  padding-inline: 6px;
  border-radius: 3px;
}Code language: CSS (css)

4. letter-spacing

The letter-spacing property is underappreciated not only for the typographical aesthetic, but also for animation.

There is no way in CSS to target the individual glyphs in a text (except the first one). But letter-spacing somewhat does that, since it determines the trailing space of all the glyphs in a text. The spacing accepts both positive (putting distance) and negative (shrinking gap) values.

An animation of the same yields a text reveal effect.

<section id="text">
  <span>Ingvar</span> 
  <span>Kamprad</span> 
  <span>Elmtaryd</span> 
  <span>Agunnaryd</span>
</section>Code language: HTML, XML (xml)
span {
  
  letter-spacing: -1ch;
  color: transparent;
  
  &::first-letter {
    color: #FBDA0C; 
  }
  
  body:has(:checked) & {
    
    letter-spacing: 0ch;
    color: #0057AD; 
    transition: letter-spacing 0.4s cubic-bezier(.8, -.5, .2, 1.4), color 0.8s linear;
  }
}Code language: CSS (css)

5. text-combine-upright

text-combine-upright is made for East Asian typography, where short texts are sometimes written in small size as a vertical sentence.

This means this works only when the writing mode is set to vertical.

In LTR (left-to-right) languages, like English, occasionally we opt for vertical writing either to save space or for its appearance. It’d be great if we could still include some horizontal text.

<p>2 Kilo <span>4 lb</span>Sugar</p>
<p><span>&#x1F3C2;</span><span>Feb</span>02/26</p>Code language: HTML, XML (xml)
p {
  writing-mode: vertical-lr;
  span {
    text-combine-upright: all;
  }
}Code language: CSS (css)

There are plenty of properties that can be useful for typography design, but these five should give you a head start and an understanding of how typography is approached with CSS and what aspects of typography we can work with for some good detailing.

联系我们 contact @ memedata.com