Is it possible to graduate the opacity of an HTML element?

前端 未结 6 1808
误落风尘
误落风尘 2020-12-21 05:54

I want a

element (the whole element, contents and everything) to fade out towards the bottom. It\'s to truncate a news post that has to fit into a v
6条回答
  •  醉酒成梦
    2020-12-21 06:24

    It is possible, without specifing an height for the container.

    Check this working demo, and try to add/remove contents from #contents

    HTML

    Some contents goes here

    CSS

    #container {
        position:relative;
    }
    #contents {
        background:red;
    }
    #gradient {
        position:absolute;
        z-index:2;
        right:0; bottom:0; left:0;
        height:200px; /* adjust it to your needs */
        background: url(data:image/svg+xml;base64,alotofcodehere);
        background: -moz-linear-gradient(top,  rgba(255,255,255,0) 0%, rgba(255,255,255,1) 70%);
        background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(255,255,255,0)), color-stop(70%,rgba(255,255,255,1)));
        background: -webkit-linear-gradient(top,  rgba(255,255,255,0) 0%,rgba(255,255,255,1) 70%);
        background: -o-linear-gradient(top,  rgba(255,255,255,0) 0%,rgba(255,255,255,1) 70%);
        background: -ms-linear-gradient(top,  rgba(255,255,255,0) 0%,rgba(255,255,255,1) 70%);
        background: linear-gradient(to bottom,  rgba(255,255,255,0) 0%,rgba(255,255,255,1) 70%);
    }​
    

    This will work almost in any browser which supports opacity (including IE9), and here's the IE8 "rgba" fallback (untested):

    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#00ffffff', endColorstr='#ffffff',GradientType=0 );
    

    To generate your own gradient, visit Colorzilla.

    The first stop (0%) must have opacity 0 ( rgba(255,255,255,0); ), then around 70% - do some tests to find what's good for you - add another stop with opacity 1 ( rgba(255,255,255,1); ).

    You've to know the background of the page/container to make it work obviously...

提交回复
热议问题