How can one create an overlay in css?

前端 未结 9 2040
青春惊慌失措
青春惊慌失措 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:45

    Quick answer without seeing examples of your current HTML and CSS is to use z-index

    css:

    #div1 {
    position: relative;
    z-index: 1;
    }
    
    #div2 {
    position: relative;
    z-index: 2;
    }
    

    Where div2 is the overlay

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

    I was just playing around with a similar problem on codepen, this is what I did to create an overlay using a simple css markup. I created a div element with class .box applied to it. Inside this div I created two divs, one with .inner class applied to it and the other with .notext class applied to it. Both of these classes inside the .box div are initially set to display:none but when the .box is hovered over, these are made visible.

    .box{
      height:450px;
      width:450px;
      border:1px solid black;
      margin-top:50px;
      display:inline-block;
      margin-left:50px;
      transition: width 2s, height 2s;
      position:relative;
      text-align: center;
        background:url('https://upload.wikimedia.org/wikipedia/commons/c/cd/Panda_Cub_from_Wolong,_Sichuan,_China.JPG');
      background-size:cover;
      background-position:center;
      
    }
    .box:hover{
      width:490px;
      height:490px;
    }
    .inner{
      border:1px solid red;
      position:relative;
      width:100%;
      height:100%;
      top:0px;
      left:0px;
      display:none; 
      color:white;
      font-size:xx-large;
      z-index:10;
    }
    .box:hover > .inner{
      display:inline-block;
    }
    .notext{
      height:30px;
      width:30px;
      border:1px solid blue;
      position:absolute;
      top:0px;
      left:0px;
      width:100%;
      height:100%;
      display:none;
    }
    .box:hover > .notext{
      background-color:black;
      opacity:0.5;
      display:inline-block;
    }
    <div class="box">
      <div class="inner">
        <p>Panda!</p>
      </div>
      <div class="notext"></div>
    </div>

    Hope this helps! :) Any suggestions are welcome.

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

    http://jsfiddle.net/55LNG/1/

    CSS:

    #box{
        border:1px solid black;
        position:relative;
    }
    #overlay{
        position:absolute;
        top:0px;
        left:0px;
        bottom:0px;
        right:0px;
        background-color:rgba(255,255,0,0.5);
    }
    
    0 讨论(0)
提交回复
热议问题