简体   繁体   English

div无法使用固定标头正确定位自身

[英]div won't position itself correctly with a fixed header

I have a header with position:fixed . 我有一个带有position:fixed的标题。 Within this header there is a div which is of a variable height, with min-height: 60px 此标头中有一个高度可变的div, min-height: 60px

Now, there is a div called container below the header div which has the following css: 现在,头div下方有一个称为container的div,它具有以下CSS:

width: 1007px;
padding: 60px 0 0 0;
display: block;

Now this works fine as long as the header is 60px, but when the header height increases, the top of the container gets hidden by the header. 现在,只要标头为60px,它就可以正常工作,但是当标头高度增加时,标头会隐藏容器的顶部。 How do I go about fixing this so that if the height of the header changes dynamically to say 90px, the container div will just move below, according to this new height? 我该如何解决这个问题,以便如果标题的高度动态更改为90px,则容器div将根据此新高度移动到下方?

Html is as follows: HTML如下:

<div id="wrapper">
 <header style="width: 100%; display: block; position: fixed; z-index: 1;">
    <div class="b-block" style="min-height:60px;display: block;">blah blah</div>
 </header>
 <div id="container" style="width: 1007px; padding: 60px 0 0 0; display: block;">blah blah</div>
</div>

What you are experiencing is normal behavior since the header has fixed positioning. 由于标题具有固定的位置,因此您遇到的是正常现象。 The only way to achieve what you want, if the header content changes, is to change the padding of the #container dynamically, with javascript. 如果标题内容发生更改,唯一实现所需内容的方法是使用javascript动态更改#container的填充。

Here's an example: http://codepen.io/seraphzz/pen/milpv 这是一个示例: http : //codepen.io/seraphzz/pen/milpv

You get the height of the header and set it as top padding for the #container div. 您获得标头的高度并将其设置为#container div的顶部填充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM