103
votes

I want to make the list menu's background disappear by using opacity, without affecting the font. Is it possible with CSS3?

9
Yeah sure! You can do it by this trick: stackoverflow.com/a/15351192/366884 Good luckSaud Alfadhli

9 Answers

136
votes

now you can use rgba in CSS properties like this:

.class {
    background: rgba(0,0,0,0.5);
}

0.5 is the transparency, change the values according to your design.

Live demo http://jsfiddle.net/EeAaB/

more info http://css-tricks.com/rgba-browser-support/

48
votes

Keep these three options in mind (you want #3):

1) Whole element is transparent:

visibility: hidden;

2) Whole element is somewhat transparent:

opacity: 0.0 - 1.0;

3) Just the background of the element is transparent:

background-color: transparent;
17
votes

yes, thats possible. just use the rgba-syntax for your background-color.

    .menue {
      background-color: rgba(255, 0, 0, 0.5); //semi-transparent red
    }
10
votes

Here is an example class using CSS named colors:

.semi-transparent {
  background: yellow;
  opacity: 0.25;
}

This adds a background that is 25% opaque (colored) and 75% transparent.

CAVEAT Unfortunately, opacity will affect then entire element it's attached to.
So if you have text in that element, it will set the text to 25% opacity too. :-(

The way to get past this is to use the rgba or hsla methods to indicate transparency* as part of your desired background "color". This allows you to specify the background transparency*, independent from the transparency of the other items in your element.

  • Technically we're setting the opacity, though we often like to speak/think in terms of transparency. Obviously they are related, inverses of each other, so setting one decides the other. The number specified is the opacity %. 1 is 100% opaque, 0% transparent & vice versa).

Here are 3 ways to set a blue background at 75% opacity (25% transparent), without affecting other elements:

  • background: rgba(0%, 0%, 100%, 0.75)
  • background: rgba(0, 0, 255, 0.75)
  • background: hsla(240, 100%, 50%, 0.75)
10
votes

To achieve it, you have to modify the background-color of the element.

Ways to create a (semi-) transparent color:

  • The CSS color name transparent creates a completely transparent color.

    Usage:

    .transparent{
      background-color: transparent;
    }
    
  • Using rgba or hsla color functions, that allow you to add the alpha channel (opacity) to the rgb and hsl functions. Their alpha values range from 0 - 1.

    Usage:

    .semi-transparent-yellow{
      background-color: rgba(255, 255, 0, 0.5);
    }
    .transparent{
      background-color:  hsla(0, 0%, 0%, 0);
    }
    

    As of the CSS Color Module Level 4, rgb and hsl works the same way as rgba and hsla does, accepting an optional alpha value. So now you can do this:

    .semi-transparent-yellow{
      background-color: rgb(255, 255, 0, 0.5);
    }
    .transparent{
      background-color:  hsl(0, 0%, 0%, 0);
    }
    

    The same update to the standard (Color Module Level 4) also brought in support for space-separated values:

    .semi-transparent-yellow{
      background-color: rgba(255 255 0 / 0.5);
    }
    .transparent{
      background-color:  hsla(0 0% 0% / 0);
    }
    

    I'm not sure why would these two be any better than the old syntax, so consider using the a-suffixed, comma-separated variants for greater support.

  • Besides the already mentioned solutions, you can also use the HEX format with alpha value (#RRGGBBAA or #RGBA notation).

    That's contained by the same CSS Color Module Level 4, so it has worse support than the first two solutions, but it's already implemented in larger browsers (sorry, no IE).

    This differs from the other solutions, as this treats the alpha channel (opacity) as a hexadecimal value as well, making it range from 0 - 255 (FF).

    Usage:

    .semi-transparent-yellow{
      background-color: #FFFF0080;
    }
    .transparent{
      background-color: #0000;
    }
    

You can try them out as well:

  • transparent:

    div {
      position: absolute;
      top: 50px;
      left: 100px;
      height: 100px;
      width: 200px;
      text-align: center;
      line-height: 100px;
      border: 1px dashed grey;
      
      background-color: transparent;
    }
    <img src="https://via.placeholder.com/200x100">
    <div>
      Using `transparent`
    </div>
  • hsla():

    div {
      position: absolute;
      top: 50px;
      left: 100px;
      height: 100px;
      width: 200px;
      text-align: center;
      line-height: 100px;
      border: 1px dashed grey;
      
      background-color: hsla(250, 100%, 50%, 0.3);
    }
    <img src="https://via.placeholder.com/200x100">
    <div>
      Using `hsla()`
    </div>
  • rgb():

    div {
      position: absolute;
      top: 50px;
      left: 100px;
      height: 100px;
      width: 200px;
      text-align: center;
      line-height: 100px;
      border: 1px dashed grey;
      
      background-color: rgb(0, 255, 0, 0.3);
    }
    <img src="https://via.placeholder.com/200x100">
    <div>
      Using `rgb()`
    </div>
  • hsla() with space-separated values:

    div {
      position: absolute;
      top: 50px;
      left: 100px;
      height: 100px;
      width: 200px;
      text-align: center;
      line-height: 100px;
      border: 1px dashed grey;
      
      background-color: hsla(70 100% 50% / 0.3);
    }
    <img src="https://via.placeholder.com/200x100">
    <div>
      Using `hsla()` with spaces
    </div>
  • #RRGGBBAA:

    div {
      position: absolute;
      top: 50px;
      left: 100px;
      height: 100px;
      width: 200px;
      text-align: center;
      line-height: 100px;
      border: 1px dashed grey;
      
      background-color: #FF000060
    }
    <img src="https://via.placeholder.com/200x100">
    <div>
      Using `#RRGGBBAA`
    </div>
8
votes

In this case background-color:rgba(0,0,0,0.5); is the best way. For example: background-color:rgba(0,0,0,opacity option);

5
votes

Try this:

opacity:0;

For IE8 and earlier

filter:Alpha(opacity=0); 

Opacity Demo from W3Schools

2
votes

Yes you can just plain text as

.someDive{
    background:transparent 
}
0
votes

For your case, we can use rgba(): First, we manipulate the background-color, and use rgba.

    .selector {
        background-color: rgba(0, 0, 0, 0.3);
    }

Now what this does is, it basically adds an opacity to your element, along with the black background color. This is how it'd look when you run it.

    body {background-color: #0008f3;}

    .container {
        height: 100px;
        width: 100px;
        background-color: rgba(255,255,255,0.5);
    }
    <body>
        <div class="container"></div>
    </body>