div之后的div,其位置固定在CSS中 [英] div after a div which position is fixed in css

查看:56
本文介绍了div之后的div,其位置固定在CSS中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个div,并指定了位置:使用此样式表进行了修复:

I have made a div and I have given it position: fixed using this style sheet:

CSS

position: fixed;
top: 35px;
z-index: 9999;
height:60px;
background:#000;
width:100%;

现在我想要的是:如果此div的高度为零,则紧随其后的div应该代替上面的div.如果div的高度不为零,则该div之后的下一个div应与上一个div留有边距,以使两个div不会相互重叠.

Now what I want is: if this div has height zero, then the div which is just after should come in place of the above div. if the height of div is not zero, the next div after this div should have a margin from the above div so both div will not overlap each other.

推荐答案

当您拥有固定(或绝对)的元素时,其后将没有任何元素.固定元素将从文档流中删除.

When you have an element that is fixed (or absolute), there is no element after it. The fixed element is taken out of the document flow.

如果要在固定位置上依次放置两个元素,请制作一个固定的容器,然后将两个元素放入其中.

If you want two elements after each other at a fixed position, make a container that is fixed, and put the two elements inside it.

您可以放置​​另一个容器,在其周围有一个上边距,并在第二个元素上设置一个上边距.如果第一个元素为空,则没有余量,因此该余量将在容器外部折叠,而第二个元素将在容器顶部.

You can put another container with a top-margin around them, and set a top-margin on the second element. If the first element is empty, there is nothing to have a margin to, so the margin will collapse outside the container and the second element will be at the top of the container.

(需要第二个容器,因为边距不会在固定元素外塌陷.)

(The second container is needed because the margin will not collapse outside the fixed element.)

演示: http://jsfiddle.net/Guffa/r5crS/

这篇关于div之后的div,其位置固定在CSS中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆