How to do equivalent of a linear-gradient with opacity on a seamless background image in CSS

谁说我不能喝 提交于 2019-12-01 21:15:55

You can consider mask to do this. You can specify the same properties as background thus you can easily define your gradient.

.box {
  width: 1000px;
  height: 1000px;
  position:relative;
  z-index:0;
}
.box:before {
  content:"";
  position:absolute;
  z-index:-1;
  top:0;
  left:0;
  right:0;
  bottom:0;
  background:url(https://picsum.photos/id/42/10/10);
  -webkit-mask-image:linear-gradient(to top, transparent, #fff, transparent);
  mask-image:linear-gradient(to top, transparent, #fff, transparent);
}
<div class="box">hello</div>

Or you can simulate this using multiple background. You will not have transparency but you will have better support:

.box {
  width: 1000px;
  height: 1000px;
  background:
    linear-gradient(to top,#fff, transparent, #fff),
    url(https://picsum.photos/id/42/10/10);
}
<div class="box">hello</div>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!