search
HomeWeb Front-endHTML TutorialCSS秘密花园: 斑马条纹背景的文本行_html/css_WEB-ITnose

《 CSS Secrets 》是 @Lea Verou 最新著作,这本书讲解了有关于CSS中一些小秘密。是一本CSSer值得一读的一本书,经过一段时间的阅读,我、@南北和@彦子一起将在W3cplus发布一系列相关的读后感,与大家一起分享。

几年前,我们第一次接触伪元素 :nth-child() / :nth-of-type() 的时候,最常见的用例之一就是“斑马条纹”的表格。

这在以前需要服务器端的代码,客户端的脚本,或繁琐的手工代码,现在它已经只需要简单的这几行代码就可以实现了:

tr:nth-child(even) {    background: rgba(0,0,0,.2);}

斑马条纹行的表格在UI设计(如上面展示的Mac OS X Yosemite文件列表)和印刷设计中非常常见,因为斑马条纹可以帮助我们的视线更容易地跟随一长行移动。

但是,对于为文本行应用相同的效果,而不是表格中的一行的时候,我们就变得无能为力了。这对于提高代码片段的可读性非常有用。很多作者最后都会用JavaScript来包装

中的每一行,这样它们可以和 :nth-child() 技术一样,在语法高亮中使用这种方法。这不仅是它减少了理论简洁的原因(JS不应该用于样式),还因为很多DOM元素会拖慢页面的加载,所以这是一个非常脆弱的解决方案(当你增加文字大小,还有某个换行时会怎样呢?)。还有更好的方法吗?

很多作者甚至给css工作小组发了请求,申请一个 :nth-line() 伪类,但是因为性能原因被否决了。

解决方案

除了伪元素应用一个较暗的背景颜色来代表行,我们以另一个角度来思考问题。为什么不可以为整个元素应用一个带斑马条纹的背景图像呢?这刚开始听起来可能比较可怕,但是我们可以直接在CSS中生成背景,通过CSS渐变并让它们以 em 为单位,这样它们可以自动适应 font-size 的变化。

让我们延伸一下这个想法,写出下图中的斑马条纹的代码。

首先,我们需要创建横条纹,根据《 CSS秘密花园:条纹背景 》方案。 background-size 需要是 line-height 的两倍,因为每个条占两行。我们第一次尝试的代码如下:

padding: .5em;line-height: 1.5;background: beige;background-image: linear-gradient(                  rgba(0,0,0,.2) 50%, transparent 0);background-size: auto 3em;

效果如图所示

结果和我们想要的是非常相近的。我们可以尝试改变字体大小,还有条纹收缩或扩张成我们需要的大小!但是,有一个很严重的问题:线条没有对齐,和我们的目的不符。这是为什么?

如果你仔细看看上图,你会发现第一个条纹是从我们容器的顶部开始的,正如我们希望从背景图像中得到的一样。但是,我们的代码不会从那里开始,因为这样看起来会非常丑。所以正如你看到的,我们应用了一个 .5em 的内边距,这使得它和我们的条纹偏移了。

解决这个问题的一种方法是使用 background-position 来让条纹相对于底部移动 .5em 。但是,如果我们决定后边调整内边距,我们还需要调整背景位置,这并不是非常DRY。我们可以让背景自动适应 padding 的长度吗?

我们回忆一下 background-origin 。这正是我们需要的:一个告诉浏览器使用内容框边缘作为基准来解决 background-position 的方法,而不是默认的填充框边缘。让我们把它添加到前边的代码中:

padding: .5em;line-height: 1.5;background: beige;background-size: auto 3em;background-origin: content-box;background-image: linear-gradient(rgba(0,0,0,.2) 50%,                  transparent 0);

如图所示

这正是我们想要完成的斑马条纹效果的代码!因为我们在条纹中使用了半透明背景的颜色,我们可以调整背景颜色,斑马条纹仍然可以工作。基本上,这是非常灵活的,破坏它的唯一途径是改变 line-height 的值,而没有相应改变 background-size 的值。

这是假设我们正在处理代码片段。在一般情况下,如果有内联元素需要一个更大的行高的时候,它也可以被打破,比如有较大 fontsize 的图片或内联内容。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Are the HTML tags and elements the same thing?Are the HTML tags and elements the same thing?Apr 28, 2025 pm 05:44 PM

The article explains that HTML tags are syntax markers used to define elements, while elements are complete units including tags and content. They work together to structure webpages.Character count: 159

What is the significance of <head> and <body> tag in HTML?What is the significance of <head> and <body> tag in HTML?Apr 28, 2025 pm 05:43 PM

The article discusses the roles of <head> and <body> tags in HTML, their impact on user experience, and SEO implications. Proper structuring enhances website functionality and search engine optimization.

What is the difference between <strong>, <b> tags and <em>, <i> tags?What is the difference between <strong>, <b> tags and <em>, <i> tags?Apr 28, 2025 pm 05:42 PM

The article discusses the differences between HTML tags , , , and , focusing on their semantic vs. presentational uses and their impact on SEO and accessibility.

Please explain how to indicate the character set being used by a document in HTML?Please explain how to indicate the character set being used by a document in HTML?Apr 28, 2025 pm 05:41 PM

Article discusses specifying character encoding in HTML, focusing on UTF-8. Main issue: ensuring correct display of text, preventing garbled characters, and enhancing SEO and accessibility.

What are the various formatting tags in HTML?What are the various formatting tags in HTML?Apr 28, 2025 pm 05:39 PM

The article discusses various HTML formatting tags used for structuring and styling web content, emphasizing their effects on text appearance and the importance of semantic tags for accessibility and SEO.

What is the difference between the 'id' attribute and the 'class' attribute of HTML elements?What is the difference between the 'id' attribute and the 'class' attribute of HTML elements?Apr 28, 2025 pm 05:39 PM

The article discusses the differences between HTML's 'id' and 'class' attributes, focusing on their uniqueness, purpose, CSS syntax, and specificity. It explains how their use impacts webpage styling and functionality, and provides best practices for

What is the 'class' attribute in HTML?What is the 'class' attribute in HTML?Apr 28, 2025 pm 05:37 PM

The article explains the HTML 'class' attribute's role in grouping elements for styling and JavaScript manipulation, contrasting it with the unique 'id' attribute.

What are different types of lists in HTML?What are different types of lists in HTML?Apr 28, 2025 pm 05:36 PM

Article discusses HTML list types: ordered (<ol>), unordered (<ul>), and description (<dl>). Focuses on creating and styling lists to enhance website design.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

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.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!