Create non-transparent div on top of transparent parent element

前端 未结 7 1687
渐次进展
渐次进展 2020-12-29 04:35

EDIT: Changed title to actually be correct

I\'m trying to simulate a modal popup in all HTML and CSS and am having a bit of bad luck w

7条回答
  •  一生所求
    2020-12-29 05:25

    Updated Answer

    The best way to do this now is to use rGBA colors. It won't work for older browsers, but you can let the design gracefully degrade by just feeding them a solid color. Example:

    CSS

    .parent {
        background: gray; /* older browsers */
        background: rgba(128,128,128,0.7); /* newer browsers */
    }
    
    .child {
        background: blue;
    }
    

    Original Answer

    It is annoying, but CSS doesn't allow that. Setting opacity for one element means no child element can have any greater opacity. (100% of 25% opacity is? Right.)

    So, one solution is to use a tiny transparent PNG as a repeating background image to work around that. The only issue there is IE6, and there's an excellent workaround called supersleight.

    (Updated. Think supersleight will work for you.)

    Update Here's a very simple test case. Create the image (say, a PNG with 50% black fill) and then create this file--save them in the same folder. If you don't see a thin box with a transparent background behind 'stuff', then you're either not saving the file correctly or have saved the image somewhere else.

    
    
    
    
    
    
    
    stuff

提交回复
热议问题