How can one create an overlay in css?

前端 未结 9 2039
青春惊慌失措
青春惊慌失措 2020-11-29 17:58

I\'d like to create a div with an arbitrary size, then display something on top of that div. What is the best way to position and size the overlay exactly as the div below i

相关标签:
9条回答
  • 2020-11-29 18:28

    I'm late to the party, but if you want to do this to an arbitrary element using only CSS, without messing around with positioning, overlay divs etc., you can use an inset box shadow:

    box-shadow: inset 0px 0px 0 2000px rgba(0,0,0,0.5);
    

    This will work on any element smaller than 4000 pixels long or wide.

    example: http://jsfiddle.net/jTwPc/

    0 讨论(0)
  • 2020-11-29 18:31

    You can use position:absolute to position an overlay inside of your div and then stretch it in all directions like so:

    CSS updated *

    .overlay {
        position:absolute;
        top:0;
        left:0;
        right:0;
        bottom:0;
        background-color:rgba(0, 0, 0, 0.85);
        background: url(data:;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAABl0RVh0U29mdHdhcmUAUGFpbnQuTkVUIHYzLjUuNUmK/OAAAAATSURBVBhXY2RgYNgHxGAAYuwDAA78AjwwRoQYAAAAAElFTkSuQmCC) repeat scroll transparent\9; /* ie fallback png background image */
        z-index:9999;
        color:white;
    }
    

    You just need to make sure that your parent div has the position:relative property added to it and a lower z-index.


    Made a demo that should work in all browsers, including IE7+, for a commenter below.

    Demo

    Removed the opacity property from the css and instead used an rGBA color to give the background, and only the background, an opacity level. This way the content that the overlay carries will not be affected. Since IE does not support rGBA i used an IE hack instead to give it an base64 encoded PNG background image that fills the overlay div instead, this way we can evade IEs opacity issue where it applies the opacity to the children elements as well.

    0 讨论(0)
  • 2020-11-29 18:31

    If you don't mind messing with z-index, but you want to avoid adding extra div for overlay, you can use the following approach

    /* make sure ::before is positioned relative to .foo */
    .foo { position: relative; }
    
    /* overlay */
    .foo::before {
        content: '';
        display: block;
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0,0,0,0.5);
        z-index: 0;
    }
    /* make sure all elements inside .foo placed above overlay element */
    .foo > * { z-index: 1; }
    
    0 讨论(0)
  • 2020-11-29 18:35

    I would suggest using css attributes to do this. You can use position:absolute to position an element on top of another.

    For example:

    <div id="container">
       <div id="on-top">Top!</div>
       <div id="on-bottom">Bottom!</div>
    </div>
    

    and css

    #container {position:relative;}
    #on-top {position:absolute; z-index:5;}
    #on-bottom {position:absolute; z-index:4;}
    

    I would take a look at this for advice: http://www.w3schools.com/cssref/pr_class_position.asp

    And finally here is a jsfiddle to show you my example

    http://jsfiddle.net/Wgfw6/

    0 讨论(0)
  • 2020-11-29 18:40

    Here is an overlay using a pseudo-element (eg: no need to add more markup to do it)

    .box {
      background: 0 0 url(http://ianfarb.com/wp-content/uploads/2013/10/nicholas-hodag.jpg);
      width: 300px;
      height: 200px;
    }
    
    .box:after {
      content: "";
      display: block;
      width: 100%;
      height: 100%;
      background: rgba(0, 0, 0, 0.4);
    }
      <div class="box"></div>

    0 讨论(0)
  • 2020-11-29 18:43
             div{
             background-image:url('');
             background-size:cover;
             background-position:top center;
             position:relative;
             }
    
             div:before{
             content:'';
             position:absolute;
             left:0;
             top:0;
             height:100%;
             width:100%;
             background-color:rgba(0,0,0,0.7);
             }
    
    0 讨论(0)
提交回复
热议问题