Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

css - Making the first div element posititon:absolute(relative to body) , is making my follow up div to hide under my first div

When I position:absolute my first div relative to body, the follow up div-element content to hide under the first div, from what I know the follow up div(block element) has to come on the next line. and when posititon:absolute my 2nd element too , It covers my first element. Can someone explain me what exactly happens for second element if I position:absolute my first element.

<div class="first"></div>
<div class="second">
      <div class="d"></div>
      <div class="e"></div>
</div>
question from:https://stackoverflow.com/questions/65871218/making-the-first-div-element-posititonabsoluterelative-to-body-is-making-my

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since your first div is position absolute it is brought outside the natural hierarchy of elements. You can now freely choose a position for it. Your second div is now pushed up and is therefore at the same position as the first div was before setting it to absolute.

Divs are by default display: block; so they go underneath each other.

You can add z-index: 5; (or whatever number) to the second div in order to bring it to the front. And if that's not working add a lower z-index to the absolute positioned element. Keep in mind in order to use z-index you always need to specify the position:

What do you actually want to accomplish? I can improve my answer then.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...