144

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

FZs
  • 16,581
  • 13
  • 41
  • 50
Saranya
  • 1,451
  • 2
  • 9
  • 3

10 Answers10

180

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/

Roy
  • 7,811
  • 4
  • 24
  • 47
Rohit Azad Malik
  • 31,410
  • 17
  • 69
  • 97
65

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;
jordanb
  • 1,975
  • 17
  • 8
33

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>
FZs
  • 16,581
  • 13
  • 41
  • 50
20

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

    .menue {
      background-color: rgba(255, 0, 0, 0.5); //semi-transparent red
    }
kohane15
  • 809
  • 12
  • 16
oezi
  • 51,017
  • 10
  • 98
  • 115
  • 2
    Hi! Do you know something about 'background-color' CSS definition with aplha channel, but by using #HEX value for color? – Piotr Apr 25 '14 at 08:41
  • @PiotrStępniewski Probably late, but now there's a such syntax (see [my answer](https://stackoverflow.com/a/58358296/8376184)) – FZs Jan 02 '20 at 15:36
15

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)
SherylHohman
  • 16,580
  • 17
  • 88
  • 94
9

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);

dasdasdasdasd
  • 1,249
  • 2
  • 11
  • 13
5

Try this:

opacity:0;

For IE8 and earlier

filter:Alpha(opacity=0); 

Opacity Demo from W3Schools

ROOT
  • 11,363
  • 5
  • 30
  • 45
Vinod
  • 4,672
  • 4
  • 19
  • 26
  • yeah..I used this code to disappear the default arrow in list menu.but it will affect the font too.. – Saranya Jun 25 '12 at 06:13
  • 6
    The site you're linking to... it's terrible. – John Dvorak Apr 07 '14 at 15:04
  • @JohnDvorak why is w3schools terrible? – TheWandererr Dec 18 '17 at 08:30
  • 2
    @TheWandererr I'll start with the page being linked. Keep in mind it's supposed to demonstrate exemplary code. And yet... There's no indentation. Do note that they use the HTML5 doctype, which means they either updated the page after it was linked here, or they used HTML5 half a year before it was even in the candidate recommendation state. Next: Inline CSS is bad, for caching and for readability. They kinda have to use it here because they only have one panel for the entire page source, but if they used multiple panels, like jsfiddle (2010) does. And even for a page demonstrating opacity, – John Dvorak Dec 18 '17 at 14:41
  • 2
    @TheWandererr they could have done better. By showing the effect on nested elements or by allowing a hover action to disable it, for one. Then there's that whole "pretending to be the w3 consorcium, and when you're found out launch another reskin or two, also labeled as w3something" thing - kinda dishonest if you ask me. They also say that their examples may be "simplified for readability" and "constantly reviewed", but removing whitespace doesn't help readability and IE7 support isn't exactly the new trend either. Also, who wouldn't pay $100 for a piece of paper that hurts their credibility? – John Dvorak Dec 18 '17 at 14:47
  • 2
    @TheWandererr IIRC, they used to have a preview of the test. The questions were a mix of trivia and entry-level stuff. They may have had some of the answers mildly incorrect, too. As for the opacity part of their CSS tutorial: three headers dedicated to making images transparent - they only change the CSS selector. Then two more paragraphs - one to introduce RGBA (finally only the background is transparent) and one as an "integration example" (yay! Also, no indentation). Then... that's it. There's nothing more about opacity. And no, there's no "advanced opacity" topic later on either. – John Dvorak Dec 18 '17 at 14:54
  • 2
    @TheWandererr let's talk about Javascript: All of their examples include: no indentation (even in Javascript code); `innerHTML =` (hey, at least it's better than the alerts and `document.write` they used to do in 2012, but still no DOM manipulation, outside of jQuery tutorial except for the bit of tutorial where they list the relevant methods one per chapter, and even then it's just seletion - and yes, they do list `document.write`). Their XHR tutorial uses readyStateChange instead of onLoad (supported since IE7). Oh and their workaround for functions in JSON? Stringify them to be eval'd later – John Dvorak Dec 18 '17 at 15:07
  • Any alternative sites that you could recommend? – TheWandererr Dec 19 '17 at 11:44
  • 2
    @TheWandererr for Javascript, CSS and HTML, Mozilla Developer Network is the de-facto documentation site. For jQuery, head to its own home website, jQuery.com. For PHP, should you have to code in it, it's PHP.net. As a place to dump your HTML snippets, there is jsFiddle.net. Also, there is JSBin. StackOverflow also has its own snippet engine can be in questions and answers. – John Dvorak Dec 20 '17 at 01:51
2

Yes you can just plain text as

.someDive{
    background:transparent 
}
ROOT
  • 11,363
  • 5
  • 30
  • 45
1

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>
0

full transparent -> .youClass{background: rgba(0,0,0,0.001);}