简体   繁体   English

CSS:如何从顶部和底部淡化文本?

[英]CSS: How to fade text from top and bottom?

I need to fade paragraph from both top and bottom.我需要从顶部和底部淡化段落。 But am able to fade from only either of the side.但我只能从任何一方消失。

HTML: HTML:

<p className="bottom-overflow-fade">
  {content}
</p>

CSS: CSS:

.bottom-overflow-fade {
  mask-image: linear-gradient(to bottom, black 80%, transparent 100%);
  -webkit-mask-image: linear-gradient(to bottom, black 80%, transparent 100%);
}

.top-overflow-fade {
  mask-image: linear-gradient(to top, black 80%, transparent 100%);
  -webkit-mask-image: linear-gradient(to top, black 80%, transparent 100%);
}

Current Result:当前结果:

在此处输入图像描述

Issue:问题:

If I add both these classnames to the paragraph, the fade won't work.如果我将这两个类名都添加到段落中,则淡入淡出将不起作用。 If I use either one of them, then the fade works perfectly either for top and bottom.如果我使用其中任何一个,那么淡入淡出对于顶部和底部都非常有效。 Is it possible to combine both these CSS properties in one, so that both the top and bottom fade works?是否可以将这两个 CSS 属性结合在一起,以便顶部和底部淡入淡出都起作用?

Note: I am not talking about any animation.注意:我不是在谈论任何 animation。

This is because of the nature of CSS.这是因为 CSS 的性质。 If you apply two declarations for the same property only one will preside: You can create a single class with a different linear gradient that starts transparent goes to black then ends transparent such as:如果您为同一属性应用两个声明,则只有一个将主持:您可以创建一个 class 具有不同的线性渐变,从透明开始变为黑色,然后以透明结束,例如:

.top-bottom-overflow-fade {
  mask-image: linear-gradient(transparent, black 20%, black 80%, transparent 100%);
  -webkit-mask-image: linear-gradient(transparent, black 20%, black 80%, transparent 100%);
}

EDIT编辑

The question posed in the comments asks what the values are that are passed to the linear-gradient function :评论中提出的问题询问传递给线性梯度 function的值是什么:

  1. The first argument is an optional value that controls the direction of the gradient.第一个参数是控制渐变方向的可选值。 This can be described using some keywords (such as to left top or to right ) or with an angle of rotation.这可以使用一些关键字(例如to left topto right )或旋转角度来描述。 In CSS the units used to express angles can be in the form of deg (degrees), turn , rad (radians), or grad (gradians).在 CSS 中,用于表示角度的单位可以是deg (度)、 turnrad (弧度)或grad (梯度)的形式。 The default direction is from top to bottom if none is supplied.如果未提供,则默认方向是从上到下。 Note that I didn't include this so the default direction is used.请注意,我没有包含此内容,因此使用了默认方向。
  2. The remaining arguments are a list of colors and optional values for where the color should start and stop called color stops .其余的 arguments 是 colors 的列表以及颜色应该在哪里开始和停止的可选值,称为颜色停止 You may use as many color stops as you wish.您可以根据需要使用尽可能多的色标。 The more color stops you have the narrower each 'band' of color will become.您拥有的色标越多,每个“色带”就会变得越窄。 A color stop list without any 'hints', or start and stop points, will have a smooth transition from one color to the next because it will apply the color start and stop points uniformly.没有任何“提示”或起点和终点的颜色停止列表将从一种颜色平滑过渡到另一种颜色,因为它将统一应用颜色起点和终点。 If you want an abrupt transition from one color to the next, you would set the stopping percentage of the first color to the same value as the starting percentage of the next color.如果您想从一种颜色突然过渡到另一种颜色,您可以将第一种颜色的停止百分比设置为与下一种颜色的起始百分比相同的值。 Some clever and artistic people produce amazing patterns with CSS gradients by taking advantage of the background-image property's ability to stack multiple images (gradients are treated as background-images not background-colors)!一些聪明和艺术的人通过利用background-image属性堆叠多个图像的能力(渐变被视为背景图像而不是背景颜色),使用 CSS 渐变产生惊人的图案

Here is a generic solution in case you want to have top, bottom and the combination of both.如果您想拥有顶部、底部和两者的组合,这是一个通用解决方案。

The trick is to create a mask using 3 gradient where by default everything is white color (no transparency) then with a CSS variable you change the white to transparent to create the fading effect.诀窍是使用 3 渐变创建蒙版,默认情况下所有内容均为白色(无透明度),然后使用 CSS 变量将白色更改为透明以创建褪色效果。

 .fade { -webkit-mask: linear-gradient(to top,#fff,var(--t,#fff)) top / 100% 30%, linear-gradient(#fff,#fff) center / 100% 40%, linear-gradient(to bottom,#fff,var(--b,#fff)) bottom / 100% 30%; -webkit-mask-repeat:no-repeat; mask: linear-gradient(to top,#fff,var(--t,#fff)) top / 100% 30%, linear-gradient(#fff,#fff) center / 100% 40%, linear-gradient(to bottom,#fff,var(--b,#fff)) bottom / 100% 30%; mask-repeat:no-repeat; }.top { --t:transparent; }.bottom { --b:transparent; } p { font-size:25px; max-width:180px; display:inline-block; }
 <p class="fade top"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras euismod est quis nibh porta, ut vehicula magna imperdiet. Etiam euismod urna id venenatis pulvinar </p> <p class="fade bottom"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras euismod est quis nibh porta, ut vehicula magna imperdiet. Etiam euismod urna id venenatis pulvinar </p> <p class="fade top bottom"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras euismod est quis nibh porta, ut vehicula magna imperdiet. Etiam euismod urna id venenatis pulvinar </p>

To understand the puzzle here is the same code used a backoground:为了理解这里的谜题,使用了相同的代码作为背景:

 .fade { background: linear-gradient(to top,red,var(--t,green)) top / 100% 30%, linear-gradient(pink,pink) center / 100% 40%, linear-gradient(to bottom,blue,var(--b,purple)) bottom / 100% 30%; background-repeat:no-repeat; }.top { --t:transparent; }.bottom { --b:transparent; } p { font-size:25px; max-width:180px; display:inline-block; }
 <p class="fade top"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras euismod est quis nibh porta, ut vehicula magna imperdiet. Etiam euismod urna id venenatis pulvinar </p> <p class="fade bottom"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras euismod est quis nibh porta, ut vehicula magna imperdiet. Etiam euismod urna id venenatis pulvinar </p> <p class="fade top bottom"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras euismod est quis nibh porta, ut vehicula magna imperdiet. Etiam euismod urna id venenatis pulvinar </p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM