《 CSS Secrets 》是 @Lea Verou 最新著作,这本书讲解了有关于CSS中一些小秘密。是一本CSSer值得一读的一本书,经过一段时间的阅读,我、@南北和@彦子一起将在W3cplus发布一系列相关的读后感,与大家一起分享。
需要通过CSS插入换行符的情况,通常出现在list列表定义中,如下图所,
在其它的情况中也有。很多时候我们使用定义列表,是因为我们想要使用恰当的、语义标记,甚至当我们视觉上想要的只是几行 name/value 对,例如,看看下边的HTML:
<dl> <dt>Name:</dt> <dd>Lea Verou</dd> <dt>Email:</dt> <dd>lea@verou.me</dd> <dt>Location:</dt> <dd>Earth</dd></dl>
我们想要的视觉效果是类似上图这样的简单的样式。第一步通常是应用一些基础的CSS,如下:
dd { margin: 0; font-weight: bold;}
但是,因为
name 和 value 各占一行。下一步尝试通常是将
在我们开始发狂、埋怨CSS、或者是放弃转移注意力或者去修改我们的HTML,有什么办法能让我们保持理智并维持我们的代码整洁吗?
解决方案
基本上,我们要做的就是在每一个
元素来完成,如下:
<!-- If you do this, kittens die --><dt>Name:</dt><dd>Lea Verou<br /></dd>...
然后,我们分别为
元素这样的效果的,那么我们的问题就可以解决了!但是我们不能这样做,对吗?或许可以试试~?
事实上在Unicode中有一个转义字符是等同于换行的: 0x000A 。在CSS中,可以写成 "\000A" ,或更简单的 "\A" 。我们可以使用它作为我们的 ::after 伪元素的内容,这样在每个
dd::after { content: "\A";}
从技术上说, 0x000A 对应于“Line Feed”字符,也就是我们在JavaScript中使用的 \n 。还有一个“Carriage Return”字符(在js中是" \r ",在CSS中是" \D "),但是这在现代浏览器中并不需要。
这看起来似乎是可行的,但是如果我们尝试了,结果却是令人失望的:和上图显示的结果并没有什么区别。但是这并不意味着我们的方向错了;我们只是忘了一些东西。我们可以在CSS中完成的,和在HTML标签中添加换行符是一样的,就是在结束标签
我们现在只有一个换行符,所以我们不需要关心空格是否会被保留(因为没有空格),所以任何的 pre 值都会生效( pre , pre-line , pre-wrap )。我推荐 pre ,因为它有广泛的浏览器支持。我们来把它们放到一起:
dt, dd { display: inline; }dd { margin: 0; font-weight: bold;}dd::after { content: "\A"; white-space: pre;}
如果你测试过了,你会看到它是可行的,而且和下图的结果完全一样!
但是,这个东西灵活吗?假设我们想要为我们定义列表中的用户添加第二个电子邮件,如下所示:
...<dt>Email:</dt><dd>lea@verou.me</dd><dd>leaverou@mit.edu</dd>...
现在结果如下所示,并不太美观。
因为我们在每一个
理想情况下,我们希望针对最后一个
dt::before { content: '\A'; white-space: pre;}
但是,这会导致第一行是空白的,因为选择器也会应用在第一个
- dt:not(:first-child)
- dt ~ dt
- dd + dt
我们使用最后一个选择器,因为它在多个
dd + dt::before { content: '\A'; white-space: pre;}dd + dd::before { content: ', '; font-weight: normal;}
效果如下:
记住,如果你的HTML标签中,多个连续的
dd + dd::before { content: ', '; margin-left: -.25em; font-weight: normal;}
这是可行的,但是相当站不住脚。如果你的内容显示的是不同的字体,带有不同的度量,空格可能会比 0.25em 大一些或小一点,这样结果看起来可能就有点过。但是,对于大多数的字体,它们的差异是可以忽略不计的。

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!
