问题
I want to make my div have gradient opacity. What I could find is how I can set it up with a color. But what if I don't want to have any color, so that the elements within would become transparent towards the bottom:
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(255,255,255,0)), color-stop(50%,rgba(41,137,216,1)), color-stop(100%,rgba(125,185,232,0))); /* Chrome,Safari4+ */
回答1:
This is the purpose of mask
.box {
font-size:35px;
display:inline-block;
-webkit-mask:linear-gradient(#fff,transparent);
mask:linear-gradient(#fff,transparent);
}
<div class="box">
text<br>text<br>text<br>text<br>text<br>text<br>
</div>
<div class="box" style="color:red;">
text<br>text<br>text<br>text<br>text<br>text<br>
</div>
<div class="box" style="color:green;">
text<br>text<br>text<br>text<br>text<br>text<br>
</div>
回答2:
You can create a gradient on the ::after
pseudo-element that fades from transparent to white (or whatever background-color you want to use) and overlay it on the content in the div:
.g{
width: 50vw;
height: 50vh;
position: relative;
display:flex;
align-items: flex-end;
}
.g::after{
content: "";
width: 100%;
height: 100%;
position:absolute;
background: linear-gradient(0deg, rgba(255,255,255,1) 0%, rgba(255,255,255,0) 100%);
}
<div class="g">
Lorem Ipsum<br>
Lorem Ipsum<br>
Lorem Ipsum<br>
Lorem Ipsum<br>
Lorem Ipsum<br>
Lorem Ipsum<br>
Lorem Ipsum<br>
Lorem Ipsum<br>
Lorem Ipsum<br>
Lorem Ipsum
</div>
来源:https://stackoverflow.com/questions/60996982/css-opacity-gradient-top-to-bottom-without-color