Create a floating div on top of divs

前端 未结 3 1494
慢半拍i
慢半拍i 2021-01-26 16:35

I am trying to create something like the following

 -------------------------
|            div 1        |
|_________________________|
| div2 on top of div1/div3|         


        
相关标签:
3条回答
  • 2021-01-26 16:49

    You need to use absolute CSS positioning. Example:

    <div id="container">
     <div id="div1"></div>
     <div id="div2"></div>
     <div id="div3"></div>
    </div>
    
    #container {
      position: relative;
    }
    #div2 {
      position: absolute;
    }
    

    Here's a jsfiddle example: http://jsfiddle.net/M7J3G/1

    0 讨论(0)
  • 2021-01-26 17:06
    <div id="1"></div>
    <div id="2"></div>
    <div id="3"></div>
    

    Just change to

    <div id="2"></div>
    <div id="1"></div>
    <div id="3"></div>
    

    or create a Wrap element to divs 1 and 3

    <div id="wrapper">
        <div id="1></div>
        <div id="2></div>
        <div id="3></div>
    </div>
    

    and the css:

    #wrapper {
    position: relative;
    }
    #2 {
    position: absolut;
    bottom: 100%;
    }
    
    0 讨论(0)
  • 2021-01-26 17:06

    Here's another possible solution you could nest div2 in div3 then add position relative to it and add a negative top to it. Like this:

    HTML

    <div class="div1"></div>
    <div class="div3">
        <div class="div2"></div>
    </div>
    

    CSS

    .div2 {
        position: relative;
        top: -50px;
    }
    

    Here's a fiddle I added some transparency (through the opacity property) to div2 so you could tell it was above divs 1 and 3. http://jsfiddle.net/hN5gq/

    Alot of these answers seemed to use css positioning. Which is the right way to go. Here's a good article on positioning it's a useful thing to understand along with floating so that you can execute almost any layout. http://alistapart.com/article/css-positioning-101/

    0 讨论(0)
提交回复
热议问题