【问题标题】:How does this CSS code apply a different horizontal color gradient to each line of text?这段 CSS 代码如何对每一行文本应用不同的水平颜色渐变?
【发布时间】:2018-06-11 03:51:33
【问题描述】:

我找到了一个 jsfiddle,它可以将颜色渐变应用于特定的文本行。它为每行文本应用不同的 2 色渐变。它有一个一致的模式。黑到红,红到黑,黑到蓝,蓝到黑。

我不知道它是如何工作的。谁能解释一下?

这里是 jsfiddle

https://jsfiddle.net/cers/o9s70yjq/2/

这里是代码

HTML

<p><em>Nobody is perfect.</em> At times we have difficulty managing our finances, we don’t always take our medications as planned, and sometimes we don’t perform up to par at work. However, research shows that people experience these problems to different degrees. Across financial strata, research reveals that the financially less well-off engage in these behaviors more often than those who are financially stable (1). These behaviors are particularly concerning, because, for those with limited financial resources, they can lead to poverty as well as perpetuate it.</p>

<p>In their article, “<a href="http://www.sciencemag.org/content/341/6149/976">Poverty Impedes Cognitive Function</a>,” which appears in the latest issue of Science, University of Warwick Professor Anandi Mani and several other social scientists (2) suggest poverty, and the ever-present concerns that come with it, places an undue burden on an individual’s limited mental resources. Compared with those who are free from poverty, this burden leaves those in poverty with fewer cognitive resources with which to make choices and take action. Mani et al. write, the poor “are less capable not because of inherent traits, but because the very context of poverty imposes load and impedes cognitive capacity.”</p>

<p>However, it is important to note that their explanation is not limited to the traditional populations of poverty, defined by a specific income level or ability to access basic human needs. The authors define poverty “broadly as the gap between one’s needs and the resources available to fulfill them.” That is, people in poverty are those who feel “poor,” who feel they have less than they need.</p>

这里是 CSS

p {
  margin: 0 0 1.5em 0;
  line-height: 1.5em;
  padding: 0;
  background: url(data:image/png,%89PNG%0D%0A%1A%0A%00%00%00%0DIHDR%00%00%00%02%00%00%00%3C%08%02%00%00%00%C3%A4%FC%AE%00%00%00%25IDAT%18%D3c```%F8%CF%C0%C0%C4%C0%C0%C0%40S%8A%E1%3F%ADm%80Y%04%B1%8D.%16%FD%A7%8B%8F%00e%B4%04m%18%DC%1D%8A%00%00%00%00IEND%AEB`%82) 0 0 / 100% 6em repeat-y;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

em {
  font-weight: bolder;
  font-style: normal;
}

a {
  text-decoration-color: black;
}

【问题讨论】:

  • 背景剪辑。他们为背景分配颜色,然后将其用作background-clip 的文本颜色
  • 我仍然想知道背景是如何与每一行文本自动对齐的。
  • 谢谢!还有一件事,“0 0 / 100% 6em repeat-y;”是什么意思?在代码中做什么?
  • @PatrickJFitzgerald 我已经在下面的回答中将其解释为评论

标签: css


【解决方案1】:

如果您删除-webkit-background-clip 规则,事情会变得更加清晰。此规则告诉浏览器仅将背景定义应用于由渲染文本填充的像素,而不是该区域中的所有像素。以下是没有这种样式的结果:

另一个技巧是定义渐变本身的内联背景图像。就是这样:

如果你调整它的大小 - 你会发现非常相似的结果:

其余样式定义背景大小,因此它将大致覆盖整个段落

【讨论】:

  • 你知道这张图片是如何产生的吗? url(数据:图像/png,%89PNG%0D%0A%1A%0A%00%00%00%0DIHDR%00%00%00%02%00%00%00%3C%08%02%00%00 %00%C3%A4%FC%AE%00%00%00%25IDAT%18%D3c```%F8%CF%C0%C0%C4%C0%C0%C0%40S%8A%E1%3F% ADm%80Y%04%B1%8D.%16%FD%A7%8B%8F%00e%B4%04m%18%DC%1D%8A%00%00%00%00IEND%AEB`%82) 0 0 / 100% 6em 重复-y; “0 0 / 100% 6em 重复-y”是什么意思?做什么?
  • 另外,他是如何让图像完全适合文本行的?
  • 这是同一张图片,只是编码为data: url。我只是在 Firefox 中打开了您的 jsfiddle,然后单击图像进入 dev.tools 以在新选项卡中打开它。您可以通过以下方式获取它的二进制版本(php 代码):urldecode(explode($image,',',2)[0])
  • 图像位置由background样式的剩余属性定义。这些0 0 / 100% 6em 实际上分别是background-position 和background-size 的简写。因此图像被放置在元素的左上角并调整大小以适应 100% 宽度和 6 行文本。我猜需要 6 行才能让单行图像大致匹配文本行大小。 repeat-y 告诉如果定义的大小不够,则垂直重复图像。
  • 谢谢,还有一件事。为什么需要指定 line-height: 1.5em; .当我删除这条线时,我注意到图片变得与线不对齐。
【解决方案2】:

这个 jsfiddle 中的技巧是:

首先他将background 设置为&lt;p&gt;,这意味着红色和蓝色来自background-image 属性。

其次,他将background-clip CSS property 设置为仅将background-image 应用于&lt;p&gt; 标记中的文本的文本。

最后他 -webkit-text-fill-color 和该属性将使文本填充颜色透明。

结果如下

p {
  margin: 0 0 1.5em 0;
  line-height: 1.5em;
  padding: 0;
  background: url(data:image/png,%89PNG%0D%0A%1A%0A%00%00%00%0DIHDR%00%00%00%02%00%00%00%3C%08%02%00%00%00%C3%A4%FC%AE%00%00%00%25IDAT%18%D3c```%F8%CF%C0%C0%C4%C0%C0%C0%40S%8A%E1%3F%ADm%80Y%04%B1%8D.%16%FD%A7%8B%8F%00e%B4%04m%18%DC%1D%8A%00%00%00%00IEND%AEB`%82) 0 0 / 100% 6em repeat-y;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

em {
  font-weight: bolder;
  font-style: normal;
}

a {
  text-decoration-color: black;
}
<p><em>Nobody is perfect.</em> At times we have difficulty managing our finances, we don’t always take our medications as planned, and sometimes we don’t perform up to par at work. However, research shows that people experience these problems to different degrees. Across financial strata, research reveals that the financially less well-off engage in these behaviors more often than those who are financially stable (1). These behaviors are particularly concerning, because, for those with limited financial resources, they can lead to poverty as well as perpetuate it.</p>

<p>In their article, “<a href="http://www.sciencemag.org/content/341/6149/976">Poverty Impedes Cognitive Function</a>,” which appears in the latest issue of Science, University of Warwick Professor Anandi Mani and several other social scientists (2) suggest poverty, and the ever-present concerns that come with it, places an undue burden on an individual’s limited mental resources. Compared with those who are free from poverty, this burden leaves those in poverty with fewer cognitive resources with which to make choices and take action. Mani et al. write, the poor “are less capable not because of inherent traits, but because the very context of poverty imposes load and impedes cognitive capacity.”</p>

<p>However, it is important to note that their explanation is not limited to the traditional populations of poverty, defined by a specific income level or ability to access basic human needs. The authors define poverty “broadly as the gap between one’s needs and the resources available to fulfill them.” That is, people in poverty are those who feel “poor,” who feel they have less than they need.</p>

【讨论】:

  • 谢谢!你知道他是如何让背景图片与每一行文本对齐的,或者他是如何对每一行应用不同的渐变的吗?
  • 他使用background-size: 100% 6em; 6em 定义了背景垂直大小,并且每行的不同渐变是随机的,并给予upvote或标记为answred
  • 谢谢!还有一件事,为什么输入“0 0 / 100% 6em?”,“0 0 /”是做什么的?还有,repeat-y 是什么意思?做?垂直重复图像?
  • 这些是背景位置 x & y background-image: url.....; background-position-x: 0px; background-position-y: 0px; background-size: 100% 6em; background-repeat-x: no-repeat; background-repeat-y: repeat; 所有在背景中使用的属性,所以将其标记为已回答,并标记为 5 个赞
【解决方案3】:

background:url(...) 正在设置具有渐变的图像。您也可以使用background:linear-gradient 实现类似的效果。

-webkit-background-clip: text 指定背景应在元素内延伸多远。

-webkit-text-fill-color:transparent 指定文本字符的填充颜色。

【讨论】:

  • 谢谢!你知道“0 0 / 100% 6em 重复-y”是什么吗?的代码呢?
  • 虽然已经回答了,background 是编写背景所有属性的捷径。所以0 0是background-postition: 0 0的缩写,100% 6em是background-size:100% 6em的缩写,repeat-y是background-repeat:repeat-y的缩写。您可以在here 找到所有这些属性的详细信息。
猜你喜欢
  • 2018-06-10
  • 1970-01-01
  • 1970-01-01
  • 2016-03-19
  • 2019-08-27
  • 1970-01-01
  • 1970-01-01
  • 2020-05-28
  • 2022-07-21
相关资源
最近更新 更多