Overlapping elements in CSS

前端 未结 5 1656
[愿得一人]
[愿得一人] 2020-12-08 04:00

How can I make two elements overlap in CSS, e.g.

Content 1
Content 2

I would like the two con

5条回答
  •  臣服心动
    2020-12-08 04:41

    You can use relative positioning to overlap your elements. However, the space they would normally occupy will still be reserved for the element:

    DEFAULT POSITIONED
    RELATIVE POSITIONED
    DEFAULT POSITIONED

    In the example above, there will be a block of white space between the two 'DEFAULT POSITIONED' elements. This is caused, because the 'RELATIVE POSITIONED' element still has it's space reserved.

    If you use absolute positioning, your elements will not have any space reserved, so your element will actually overlap, without breaking your document:

    DEFAULT POSITIONED
    ABSOLUTE POSITIONED
    DEFAULT POSITIONED

    Finally, you can control which elements are on top of the others by using z-index:

    DEFAULT POSITIONED
    ABSOLUTE POSITIONED
    DEFAULT POSITIONED

提交回复
热议问题