transparent background image

ⅰ亾dé卋堺 提交于 2019-11-30 11:48:49

You can't adjust the translucency of a background image with CSS.

You can adjust the translucency of an entire element (opacity) or of a plain background rgba(), but not a background image.

Use an image format that supports translucency natively (such as PNG) and embed the translucency you want in the image itself.

The element that needs to have semi-transparent background:

#myPass

This is how without touching the HTML, we add controllable transparency background to the element:

#myPass::before {
    content: "";
    position: absolute;
    top: 0; right: 0;
    bottom: 0: left: 0;
    background: url(image.jpg) no-repeat top center;
    opacity: 0.5;
}

The only thing to note is that the #myPass element, must be positioned:

#myPass {
    position: relative; /* absolute is good too */
}

Why don't you create another div and use it as a pseudo-background or something?

This is the way to do it:

div {
  width: 200px;
  height: 200px;
  display: block;
  position: relative;
}

div::after {
  content: "";
  background: url(image.jpg);
  opacity: 0.5;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  position: absolute;
  z-index: -1;   
}

Just ran into this issue myself. If you combine linear-gradient with the background image url, you can control image opacity. By keeping the two "gradients" the same, you can get a filter/opacity effect without using opacity proper.

i.e.

 .bg-image {
        background: linear-gradient(rgba(255,255,255,0.5), rgba(255,255,255,0.5)), url(path-to-image.jpg) center no-repeat;
        background-size: cover;
      }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!