```你应该掌握的能提升文字设计的 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` 以获得更好的兼容性),并指出调整 `letter-spacing` 可能会导致连字功能失效。 * **改进建议:** 评论者建议使用 `text-wrap: balance` 来处理孤行问题,并纠正了将 `box-decoration-mode` 误写为 `box-decoration-break` 的引用错误。 * **最佳实践:** 讨论倾向于优先考虑向后兼容性,即支持最近两个主要浏览器版本,通常通过 *Can I Use* 进行验证。 * **推荐资源:** 在学习现代 CSS 方面,贡献者指出 MDN 是权威的技术参考,同时推荐了 Kevin Powell、Josh Comeau 和 Ahmad Shadeed 的教程。其他人则强调了 Fediverse 在获取 CSS 演进方面的实用社区见解。 * **Web 演进:** 参与者指出,现代 CSS 已经足够先进,可以取代许多 UI 组件(如菜单和弹窗)所需的 JavaScript,从而打造出更精简、高效的 Web。
相关文章

原文

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 spanimg, 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