CSS: opacity only background, not the text inside

This question already has an answer here:

  • How do I give text or an image a transparent background using CSS? 26 answers

  • The easy way would be to move the text into a separate div, like so. Basically you apply the opacity to a separate div and position the text on top...

    <div id="parent">
       <div id="opacity"></div>
       <div id="child">text</div>
    </div>
    
    div#parent { position:relative; width:200px; height:200px; }
    div#child { position:absolute; width:200px; height:200px; z-index:2; }
    div#opacity { position:absolute; width:200px; height:200px; z-index:1; }
    

    The other route would be rgba. Don't forget there's a separate css property to feed IE since it doesn't support the rgba property. You can also feed a transparent png.

    #regForm {
       background: rgb(200, 54, 54); /* fallback color */
       background: rgba(200, 54, 54, 0.5);
    }
    

    And for IE...

    <!--[if IE]>
    
       <style type="text/css">
    
       .color-block {
           background:transparent;
           filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000050,endColorstr=#99000050);
           zoom: 1;
        } 
    
        </style>
    
    <![endif]-->
    

    Personally I'd go with the first option because it's less of a hassle.


    RGBA is the way to go if you're only looking for a css solution. It is possible to use a solid colour as fallback for the old browsers which can't use RGBA.

    .stuff {
      background-color: rgb(55, 55, 55);
      background-color: rgba(55, 55, 55, 0.5);
    }
    

    You can also fallback on an image:

    .stuff2 {
      background: transparent url(background.png);
      background: rgba(0, 0, 0, 0.5) none;
    }
    

    Here is all you need for getting this to work in all evil versions of IE: http://kimili.com/journal/rgba-hsla-css-generator-for-internet-explorer


    Your best bet will probably be to use semi-transparent PNGs for your background, or to set the colors for the background and border using RGBa. PNGs will work well if you don't mind the extra markup you'll need to make a flexible-width container, but they also aren't supported in IE6 (if that's a concern).

    RGBa is less widely-implemented across browsers, but if the transparency is only used for visual flair, then it's a good place to use some progressive enhancement.

    For RGBa, you'll need to add an extra line as a fallback:

    #regForm {
        background: rgb(0, 0, 0);
        background: rgba(0, 0, 0, 0.5);
        border-color: rgb(24, 17, 12);
        border-color: rgba(24, 17, 12);
    }
    

    Any browser that doesn't recognize the RGBa declaration will simply use the plain RGB.

    CSS-Tricks article on RGBa across browsers

    链接地址: http://www.djcxy.com/p/28080.html

    上一篇: 只有背景

    下一篇: CSS:不透明只有背景,不是里面的文字