首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >可以使用html/javascript/css在img元素上打洞吗?

可以使用html/javascript/css在img元素上打洞吗?
EN

Stack Overflow用户
提问于 2020-03-06 20:34:03
回答 1查看 144关注 0票数 0

我正在开发一个网站,其中的背景部分需要是透明的,因为它背后的视频需要通过。背景是一幅图像。此外,我需要确保网站在Opera版本11上运行。

考虑到网站需要在Opera11浏览器中运行,有没有办法在图片元素上留下一个小洞?<img src='http://ip/cutaholeinme.png'>

这样,最后就会显示以下image with a hole;(白色矩形应该是透明的)

EN

回答 1

Stack Overflow用户

发布于 2020-03-06 20:52:15

mask可以很容易地做到这一点

代码语言:javascript
运行
复制
img {
  -webkit-mask:
    linear-gradient(#fff,#fff) top   / 100% 50px, /* show 50px from the top */
    linear-gradient(#fff,#fff) bottom/ 100% 70px, /* show 70px from the bottom */
    linear-gradient(#fff,#fff) left  / 20% 100%,  /* show 20% from the left */
    linear-gradient(#fff,#fff) right / 40% 100%;  /* show 40% from the right */
  -webkit-mask-repeat:no-repeat;
  
  mask:
    linear-gradient(#fff,#fff) top   / 100% 50px,
    linear-gradient(#fff,#fff) bottom/ 100% 70px,
    linear-gradient(#fff,#fff) left  / 20px 100%,
    linear-gradient(#fff,#fff) right / 40px 100%;
  mask-repeat:no-repeat;
}

body {
 background:linear-gradient(to right,blue,red);
}
代码语言:javascript
运行
复制
<img src="https://picsum.photos/id/1006/500/400" >

为了获得更好的支持,可以考虑使用SVG掩码:How to apply background to specific elements only?

您还可以考虑使用多个元素的技巧,如下所示:

代码语言:javascript
运行
复制
.box {
  width: 500px;
  height: 400px;
  position: relative;
  background-size: 0 0;
}

.box div {
  background: inherit;
  height: 100%;
}

.box:before,
.box:after,
.box div:before,
.box div:after {
  content: "";
  position: absolute;
  background-image: inherit;
  background-size: 500px 400px;
}

.box:before,
.box:after {
  left: 0;
  right: 0;
}

.box:before {
  top: 0;
  background-position:top;
  height: 20%;
}

.box:after {
  bottom: 0;
  background-position:bottom;
  height: 30%;
}

.box div:before,
.box div:after {
  top: 0;
  bottom: 0;
}

.box div:before {
  left: 0;
  background-position:left;
  width: 40px;
}

.box div:after {
  right: 0;
  background-position:right;
  width: 80px;
}

body {
  background: linear-gradient(to right, blue, red);
}
代码语言:javascript
运行
复制
<div class="box" style="background-image:url(https://picsum.photos/id/1006/500/400)">
  <div></div>
</div>

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60564183

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档