如何用CSS淡化div的边缘?

ant*_*pug 49 css css3

在此输入图像描述

是否有可能只用一个div(没有背景图像/前景图像/图层)来实现这一点?

fca*_*ran 59

codepen上的示例:http://codepen.io/anon/pen/sbHAc/

相关的CSS

ol {
  border   : 1px #d8d8d8 dashed;
  position : relative;
}

ol:after {
  content  : "";
  position : absolute;
  z-index  : 1;
  bottom   : 0;
  left     : 0;
  pointer-events   : none;
  background-image : linear-gradient(to bottom, 
                    rgba(255,255,255, 0), 
                    rgba(255,255,255, 1) 90%);
  width    : 100%;
  height   : 4em;
}
Run Code Online (Sandbox Code Playgroud)

产生的效果

在此输入图像描述

如果浏览器支持该pointer-events 属性(除了所有主要浏览器IE<=10),则渐变下的文本也将是可选择/可点击的.

  • 我来到这里寻找滚动列表边缘的淡入淡出效果 - 我假设OP正在尝试做类似的事情.我把这把笔分开,将`ol`包裹在`div`中,结束了我正在寻找的效果.http://codepen.io/1deadpixel/pen/XNXaMY (3认同)

Ale*_*izi 13

I (personally) find that using a secondary element as an "overlap" works pretty well. I do this by defining a new tag. This makes it really easy to add the desired fade out effect to any element you want using <fade/> at the end.

div {
    position: relative;
}

fade {
    position: absolute;
    bottom: 0px;

    display: block;
  
    width: 100%;
    height: 50px;
  
    background-image: linear-gradient(to bottom, 
        rgba(255, 255, 255, 0), 
        rgba(255, 255, 255, 0.9)
    100%);
}
Run Code Online (Sandbox Code Playgroud)
<div>
    text
    <br>
    text
    <br>
    text
    <fade/>
</div>
Run Code Online (Sandbox Code Playgroud)

Giving the fade element an absolute position with a gradient background works just as expected. As long as you remember to set the parent's position to relative.

  • 仅当内容位于白色容器上时,此功能才有效。如果下面的颜色是其他颜色,看起来会很奇怪。当然,可以更改颜色以匹配背景,但如果背景不是纯色(例如图片的渐变),这就会成为问题。 (2认同)

小智 5

<style>
.fade {
    position: relative; 
    bottom: 4em;
    height: 4em;
    background: -webkit-linear-gradient(
        rgba(255, 255, 255, 0) 0%,
        rgba(255, 255, 255, 1) 100%
    ); 
    background-image: -moz-linear-gradient(
        rgba(255, 255, 255, 0) 0%,
        rgba(255, 255, 255, 1) 100%
    );
    background-image: -o-linear-gradient(
        rgba(255, 255, 255, 0) 0%,
        rgba(255, 255, 255, 1) 100%
    );
    background-image: linear-gradient(
        rgba(255, 255, 255, 0) 0%,
        rgba(255, 255, 255, 1) 100%
    );
    background-image: -ms-linear-gradient(
        rgba(255, 255, 255, 0) 0%,
        rgba(255, 255, 255, 1) 100%
    );
}
</style>
Run Code Online (Sandbox Code Playgroud)

这是您的示例http://jsfiddle.net/nrgx7/

  • 谢谢。但这并不能回答我的问题。您的解决方案需要添加第二个 .fade 元素以适应淡入淡出。 (2认同)