How do I give text or an image a transparent backg

2018-12-30 23:00发布

Is it possible, using CSS only, to make the background of an element semi-transparent but have the content (text & images) of the element opaque?

I'd like to accomplish this without having the text and the background as two separate elements.

When trying:

p {
  position: absolute;
  background-color: green;
  filter: alpha(opacity=60);
  opacity: 0.6;
}

span {
  color: white;
  filter: alpha(opacity=100);
  opacity: 1;
}
<p>
  <span>Hello world</span>
</p>

It looks like child elements are subjected to the opacity of their parents, so opacity:1 is relative to the opacity:0.6 of the parent.

标签: html css opacity
28条回答
荒废的爱情
2楼-- · 2018-12-30 23:55

As per my point of view best way to use background color with opacity if we use this then we will not lose opacity for the other elements like test color and border etc..

background-color: rgba(71,158,0,0.8);

Use background color with opacity

background-color:rgba(R,G,B,Opacity);

enter image description here

查看更多
春风洒进眼中
3楼-- · 2018-12-30 23:56

CSS3 has an easy solution of your problem. Use:

background-color:rgba(0,255,0,0.5);

Here, rgba stands for red, green, blue and alpha value. Green value is obtained because of 255 and half transparency is obtained by 0.5 alpha value.

查看更多
余生请多指教
4楼-- · 2018-12-30 23:57

If you are a Photoshop guy, you can also use:

 #some-element {
  background-color: hsla(170, 50%, 45%, 0.9); // **0.9 is the opacity range from 0 - 1**
 }

Or:

#some-element {
  background-color: rgba(170, 190, 45, 0.9); // **0.9 is the opacity range from 0 - 1**
}
查看更多
余生请多指教
5楼-- · 2018-12-30 23:57

There's an easier solution to put an overlay over an image on the same div. It's not the right use of this tool. But works like a charm to make that overlay using CSS.

Use an inset shadow like this:

box-shadow: inset 0 0 0 1000px rgba(255, 255, 255, 0.9);

That's all :)

查看更多
唯独是你
6楼-- · 2018-12-30 23:59

A while back, I wrote about this in Cross Browser Background Transparency With CSS.

Bizarrely Internet Explorer 6 will allow you to make the background transparent and keep the text on top fully opaque. For the other browsers I then suggest using a transparent PNG file.

查看更多
时光乱了年华
7楼-- · 2018-12-31 00:00
<div align="center" style="width:100%;height:100%;background:white;opacity:0.5;position:absolute;z-index:1001">
    <img id="search_img" style="margin-top:20%;" src="../resources/images/loading_small.gif">
</div>

http://jsfiddle.net/x2ukko7u/?

查看更多
登录 后发表回答