简体   繁体   English

固定标题与下面的内容重叠

[英]Fixed header overlaps the content below

I have the following HTML, 我有以下HTML,

 header { background: #f6f6f6; width: 100%; min-height: 120px; position: fixed; z-index: 100; top: 0; padding: 5px 0; } 
 <header> <div class="container"> <div class="row"> <a href="index.html"> <img src="images/logo.png" class="img-responsive" alt=""> </a> </div> </div> </header> 

As you can see it is a fixed header and I've given it a min-height of 120px. 如您所见,它是一个固定的标头,我给它的最小高度为120px。 This makes the header overlap the content below and I've prevented that by giving the div that wraps the content a margin-top of 90px. 这使得标题与下面的内容重叠,而我通过给div内容加上90px的margin-top来防止这种情况。

This works fine on larger layouts but when the layout gets smaller and the image starts re-sizing(due to the .img-responsive class) its height reduces and it results in an empty space below it. 这在较大的布局上效果很好,但是当布局变小并且图像开始重新调整大小时(由于.img-response类),它的高度减小了,并在其下方形成了空白区域。 I could write a media query and reduce the margin-top but I was wondering if there is some other way to prevent this from happening. 我可以编写媒体查询并减少页边距,但是我想知道是否还有其他方法可以防止这种情况发生。

You can use JS to update the margin based on the header size on page load. 您可以使用JS根据页面加载时的标题大小来更新页边距。

$(function() {
    var headHeight = $('header').outerHeight();

    $('.welcome-home').css({'margin-top': headHeight });
});

This will get the total height of the header element and then apply that size as margin-top to .welcome-home . 这将获取header元素的总高度,然后将该尺寸作为.welcome-home margin-top

Here's a fiddle: https://jsfiddle.net/13n7mpbk/ If you try adding to the header, it will automatically increase the margin as needed when the page is loaded. 这是一个小提琴: https : //jsfiddle.net/13n7mpbk/如果您尝试添加至页眉,则在加载页面时,它会根据需要自动增加页边距。

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

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