how to position two divs above each over

后端 未结 5 645
耶瑟儿~
耶瑟儿~ 2020-12-14 00:32

Is there any way to start drawing divs from the same point? That means if I add new div and then I add another div, they will appear above each other. Because I want to move

5条回答
  •  北海茫月
    2020-12-14 01:11

    All statements regarding absolute positioning are correct. People failed to mention, however, that you need position: relative on the parent container.

    #container {
      position: relative;
    }
    #num1,
    #num2 {
      position: absolute;
      left: 50px;
    }
    1
    2

    Depending on which element you want on top, you can apply z-indexes to your absolutely positioned divs. A higher z-index gives the element more importance, placing it on the top of the other elements:

    #container {
      position: relative;
    }
    #num1,
    #num2 {
      position: absolute;
      left: 50px;
    }
    /* num2 will be on top of num1 */
    #num1 {
      z-index: 1;
    }
    #num2 {
      z-index: 2;
    }
    1
    2

提交回复
热议问题