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:48

You can use pure CSS 3: rgba(red, green, blue, alpha), where alpha is the level of transparency you want. There is no need for JavaScript or jQuery.

Here is an example:

#item-you-want-to-style{
    background:rgba(192.233, 33, 0.5)
}
查看更多
素衣白纱
3楼-- · 2018-12-30 23:49

Almost all these answers assume the designer wants a solid color background. If the designer actually wants a photo as the background the only real solution at the moment is JavaScript like the jQuery Transify plugin mentioned elsewhere.

What we need to do is join the CSS working group discussion and make them give us a background-opacity attribute! It should work hand in hand with the multiple-backgrounds feature.

查看更多
路过你的时光
4楼-- · 2018-12-30 23:50

This is the best solution I could come up with, NOT using CSS 3. And it works great on Firefox, Chrome and Internet Explorer as far as I can see.

Put a container DIV and two child DIVs in the same level, one for content, one for background. And using CSS, auto-size the background to fit the content and put the background actually in the back using z-index.

    .container {
      position: relative;
    }
    .content {
      position: relative;
      color: White;
      z-index: 5;
    }
    .background {
      position: absolute;
      top: 0px;
      left: 0px;
      width: 100%;
      height: 100%;
      background-color: Black;
      z-index: 1;
      /* These three lines are for transparency in all browsers. */
      -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";
      filter: alpha(opacity=50);
      opacity: .5;
    }
<div class="container">
  <div class="content">
    Here is the content.
    <br />Background should grow to fit.
  </div>
  <div class="background"></div>
</div>

查看更多
看风景的人
5楼-- · 2018-12-30 23:51

background-color:rgba(255,0,0,0.5); as mentioned above is the best answer simply put. To say use CSS3, even in 2013, is not simple because the level of support from various browsers changes with every iteration.

While background-color is supported by all major browsers (not new to CSS3) [1] the alpha transparence can be tricky, especially with Internet Explorer prior to version 9 and with border color on Safari prior to version 5.1. [2]

Using something like Compass or SASS can really help production and cross platform compatibility.


[1] W3Schools: CSS background-color Property

[2] Norman's Blog: Browser Support Checklist CSS3 (October 2012)

查看更多
回忆,回不去的记忆
6楼-- · 2018-12-30 23:53

The problem is, that the text actually HAS full opacity in your example. It has full opacity inside the p tag, but the p tag is just semi-transparent.

You could add an semi-transparent PNG background image instead of realizing it in CSS, or separate text and div into 2 elements and move the text over the box (for example, negative margin).

Otherwise it won't be possible.

EDIT:

Just like Chris mentioned: if you use a PNG file with transparency, you have to use a JavaScript workaround to make it work in the pesky Internet Explorer...

查看更多
栀子花@的思念
7楼-- · 2018-12-30 23:55

It's better to use a semi-transparent .png.

Just open Photoshop, create a 2x2 pixel image (picking 1x1 can cause an Internet Explorer bug!), fill it with a green color and set the opacity in "Layers tab" to 60%. Then save it and make it a background image:

<p style="background: url(green.png);">any text</p>

It works cool, of course, except in lovely Internet Explorer 6. There are better fixes available, but here's a quick hack:

p {
    _filter: expression((runtimeStyle.backgroundImage != 'none') ? runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src='+currentStyle.backgroundImage.split('\"')[1]+', sizingMethod=scale)' : runtimeStyle.filter,runtimeStyle.backgroundImage = 'none');
}
查看更多
登录 后发表回答