如果文本大于允许值,则在CSS溢出时淡出文本


77

我试图在文本量大于行可处理量时创建文本淡出效果。我用的混合物实现这一目标max-heightoverflowlinear-gradient。这样的事情。

max-height:200px;
overflow:hidden;
text-overflow: ellipsis;
background: -webkit-linear-gradient(#000, #fff);

全小提琴可用。我正在努力达到类似于这一效果在此处输入图片说明

而且我有点亲密。问题是在我的情况下,文本从一开始就开始淡出,我希望仅当文本确实接近最大大小时才开始淡出。可以说如果它已经是150px,则开始淡出。另外,我仅使用-webkit前缀,并且我假设可以为其他渲染引擎添加其他前缀。

有没有办法在纯CSS中做到这一点?


1
除非我们必须结合CSS使用一些布局技巧,否则检测CSS中的溢出何时发生是不可能的。如果您是说如果高度达到约150像素,则认为溢出发生,则应该淡化文本,这是适合您的解决方案,它在文本上方使用了一个渐变层,适用于所有支持线性-渐变,因此我认为它比-webkit-background-clip:text仅基于Webkit的浏览器支持的解决方案更好(我认为):jsfiddle.net/b9vtW/1
King King

Answers:


118

看起来您的要求只是淡出以某个高度(大约150像素)开始的文本,以该高度显示的文本(如果有)被视为溢出。因此,您可以尝试使用放置在文本区域顶部的某种透明的线性渐变图层,我们可以使用伪元素以一种简洁的方式实现此目的,:before如下所示:

.row:before {
  content:'';
  width:100%;
  height:100%;    
  position:absolute;
  left:0;
  top:0;
  background:linear-gradient(transparent 150px, white);
}

小提琴


27
非常好。我也不得不集position:relative.row。但是,一个问题是,重叠的伪元素使得无法单击原始元素内的链接。为了解决这个问题,我将其添加pointer-events: none到伪元素的样式中。
joeytwiddle

@joeytwiddle-感谢您的pointer-events提示。作为一个侧面说明,pointer-eventslinear-gradients在IE9 +支持,并:before在IE8 +是支持的,所以IE8用户将收到伪元素,但不会看到的梯度,将不能够点击以下任何事情。建议检测IE8并删除伪元素。
TheCarver '16

7
如果您想知道为什么这在iOS Safari上不起作用,那是因为transparent那里不允许这样做。使用rgba(255,255,255,0)替代,按照本SO后:stackoverflow.com/questions/16816648/...
马克宝

1
只是想指出一些事情:rgba值(最后一个值)的不透明度来自于0 - 1.0您不希望它完全消失的情况,并且如果您想要诸如文本下方的more链接之类的链接,则可以使用上面的样式,例如,p标记而不是.row
user3791372

1
此文本溢出:省略号;内容:“”;在您的课堂上没有用
Matohawk '17

14

我建议这样的事情:

将渐变应用于绝对定位的伪元素(:after),该元素从顶部开始定位在160px处,高度为40px –这样,就不会在较短的框中完全显示它(因为它们max-height与结合使用overflow:hidden)。渐变本身从完全透明(rgba(0,0,0,0))到纯黑色。

.row{
    position:relative;
    /* … */
}
.row:after {
    content:"";
    position:absolute;
    top:160px;
    left:0;
    height:40px;
    width:100%;
    background: linear-gradient(rgba(0,0,0,0), #000);
}

http://jsfiddle.net/b9vtW/2/


很好,但是如果您使用bottom:0;代替,top:160px;那么它将更加通用。渐变将仅占据父元素的底部height,而渐变的高度即为渐变。
Nux

如果元素具有max-height并且没有高度,则使用bottom而不是top的@nux将无法正常工作。如果您使用bottom:0,则即使文本仅占据一行,而仅在最后一行具有渐变(可能是第三,第四等)时
也会应用渐变-jbdeguzman

5

我认为您正在寻找类似的东西,对吧?

http://jsfiddle.net/QPFkH/

.text {
    position:relative;
    width:200px;
    max-height:10em;
    overflow:hidden;
}
.shadow {
    position:absolute;
    top:8em;
    width:100%;
    height:2em;
    background: -webkit-linear-gradient(transparent, white);
    background: -o-linear-gradient(transparent, white);
    background: -moz-linear-gradient(transparent, white);
    background: linear-gradient(transparent, white);
}

4

您的代码是正确的,只是必须设置线性梯度百分比

background: -webkit-linear-gradient(top,#000 70%, #fff);

尝试小提琴链接

http://jsfiddle.net/ShinyMetilda/kb4fL/1/

您可以像这样在像素中指定它

 background: -webkit-linear-gradient(top,#000 140px, #fff);

两者都一样



2

我建议您使用http://www.colorzilla.com/gradient-editor/

您正在寻找的可能是:

background: -moz-linear-gradient(top,  rgba(255,255,255,0) 0%, rgba(255,255,255,0) 80%, rgba(0,0,0,1) 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(255,255,255,0)), color-stop(80%,rgba(255,255,255,0)), color-stop(100%,rgba(0,0,0,1))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top,  rgba(255,255,255,0) 0%,rgba(255,255,255,0) 80%,rgba(0,0,0,1) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top,  rgba(255,255,255,0) 0%,rgba(255,255,255,0) 80%,rgba(0,0,0,1) 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top,  rgba(255,255,255,0) 0%,rgba(255,255,255,0) 80%,rgba(0,0,0,1) 100%); /* IE10+ */
background: linear-gradient(to bottom,  rgba(255,255,255,0) 0%,rgba(255,255,255,0) 80%,rgba(0,0,0,1) 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#00ffffff', endColorstr='#000000',GradientType=0 ); /* IE6-9 */

如果您无法正常工作,请将这些CSS复制并粘贴到url(css窗口)中,然后随意进行修改。


问题是我需要以像素而不是百分比指定起点。如果文本太小,百分比仍会淡出。显然,命令的语法建议不要这样做。
Salvador Dali

好。您可以使用insteed这个解决方案,该解决方案可以在所有浏览器上运行(除非后面是伪元素):jsfiddle.net/b9vtW/3 ..只需添加一个绝对位置的div,将您希望将其内部的高度与底部的gradient对齐即可backgroud。简单又安全
AlvaroMenéndez2014年

1

引导程序按钮有一个类似的问题,该按钮具有垂直渐变,并且在悬停时也会更改渐变。这对我有用。

<button class="btn btn-success" style="width:250px">
      <div class="faderflow">
         SOME TEXT THAT'S TOO LONG FOR THE BUTTON
      </div>
</button>



.faderflow {
    overflow: hidden;

   /* same color as text (white) */
    background: linear-gradient(to right, rgb(255, 255, 255) 80%,rgba(255, 255, 255, 0) 100%);
    background-clip: border-box;
    -webkit-background-clip: text;
    -webkit-text-fill-color: #fff0;

   /* overwrite the bootstrap text shadow  */
    text-shadow: 0 -1px 0 rgba(255, 255, 255, 0.2);
}

https://codepen.io/andyg2/pen/qGmKKN


感谢您提供此答案,当背景上有图像/渐变时,这确实是正确的方法。唯一的问题是当您使用多种颜色的文字时...
Adrian

1
有更好的方法-– mask-image: linear-gradient(to bottom, white, transparent);
Adrian

0

我使用这种方法使底部透明。

http://jsfiddle.net/IAMCHIEF/x7qLon18/4/

.row{
    position:relative;
    width: 300px;
    margin-bottom: 5px;
    padding-bottom: 5px;
    border-bottom: 3px solid #777;
    max-height:200px;
    overflow:hidden;
    color:#fff;
    background:#000;
}
.row:after {
  content: "";
position: absolute;
top: 137px;
left: 0;
height: 40px;
width: 100%;
background: -webkit-linear-gradient(rgba(255, 255,255, .4), rgba(255, 255, 255, 1));
}
<div class="row">
	Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
<div class="row">
	Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
<div class="row">
	Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed
</div>

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.