简体   繁体   中英

fixed header blocking content below

So, I have a header which I have set as fixed. I have a list of items underneath it which scroll, but the header stays intact. However, one of the issues I'm facing is that the list is covered by the fixed header at the very top, obstructing the first item of the list.

I've tried adding "margin-top:55px;" to the container holding the list of items, but doing so hides the scrollbar behind the header, which is not very nice to look at. How could I display the header and list without it being obstructed?

Any help would be most appreciated.

 body.campaign-body { height: 100%; overflow-y: scroll; } .container.campaign-header { position: fixed; z-index: 1000; background: #000; width: 100%; height: 55px; color:#fff;""} .container.scroll-campaign-list { /*margin-top: 55px;*/ } 
 <body class="campaign-body"> <div class="container campaign-header"> <div class="row admin-header"> <div class="header-title"> <h4 class="white-txt header-name">LISTING</h4> </div> </div> </div> <div class="container scroll-campaign-list" id="campaigns-list"> <div class="row campaigns"> <div class="twelve columns list-items"> <h5 class="white-txt campaign-name">Blah blah</h5> <p class="campaign-question grey-txt">Blah blah</p> </div> </div> <hr> <div class="row campaigns"> <div class="twelve columns list-items"> <h5 class="white-txt campaign-name">Blah blah</h5> <p class="campaign-question grey-txt">Blah blah</p> </div> </div> <hr><div class="row campaigns"> <div class="twelve columns list-items"> <h5 class="white-txt campaign-name">Blah blah</h5> <p class="campaign-question grey-txt">Blah blah</p> </div> </div> <hr><div class="row campaigns"> <div class="twelve columns list-items"> <h5 class="white-txt campaign-name">Blah blah</h5> <p class="campaign-question grey-txt">Blah blah</p> </div> </div> <hr><div class="row campaigns"> <div class="twelve columns list-items"> <h5 class="white-txt campaign-name">Blah blah</h5> <p class="campaign-question grey-txt">Blah blah</p> </div> </div> <hr><div class="row campaigns"> <div class="twelve columns list-items"> <h5 class="white-txt campaign-name">Blah blah</h5> <p class="campaign-question grey-txt">Blah blah</p> </div> </div> <hr><div class="row campaigns"> <div class="twelve columns list-items"> <h5 class="white-txt campaign-name">Blah blah</h5> <p class="campaign-question grey-txt">Blah blah</p> </div> </div> <hr><div class="row campaigns"> <div class="twelve columns list-items"> <h5 class="white-txt campaign-name">Blah blah</h5> <p class="campaign-question grey-txt">Blah blah</p> </div> </div> <hr><div class="row campaigns"> <div class="twelve columns list-items"> <h5 class="white-txt campaign-name">Blah blah</h5> <p class="campaign-question grey-txt">Blah blah</p> </div> </div> <hr> </div> </body> 

First, reset body margin:

body {
  margin: 0;
}

Next, set padding-top (not margin-top) for scroll-campaign-list :

.container.scroll-campaign-list {
  padding-top: 50px;
}

I have added a "blocker" do this before:

HTML:

<div class="blocker"> </div>

CSS:

/* Matches your header height */
.blocker { height: 55px; };

All you are doing it putting an invisible div underneath the header

Fiddle: https://jsfiddle.net/btv5bjfd/

I've posted your code in a fiddle to make is a bit easier to work with. Below is the fiddle link with the results, take a look and tell me if it's what you were looking for.

I've made your header attack to the top of the page which made me able to add a margin to the list.

.container.campaign-header { position: fixed;
    z-index: 1000; background: #000; width: 100%; height: 55px; color:#fff; top:0;}
.container.scroll-campaign-list {margin-top: 75px; }

https://jsfiddle.net/m4fw7mk1

If you want your scroll bar to not scroll "through" the header, you're going to have to make a fixed content area which will handle the scrolling, rather than the body which normally handles the scrolling.

Check This Fiddle for the basics.

Make sure you have zero margin and padding on your body

* {
    margin:0;
    padding:0;
}

Then, make sure your header is fixed to the top and left

.campaign-header {
    top:0;
    left:0;
}

Then your campaign scroll list is going to be a fixed content container that is 100% height minus the height of the header, positioned at the bottom of the header and allows scrolling

.scroll-campaign-list {
    position:fixed;
    top:55px;
    left:0;
    width:100%;
    height:calc(100% - 55px);
    overflow:scroll;
}

Here is a fiddle with your content in it

I think that it is better to have different classes. So I have deleted the .container class.

  body.campaign-body { height: 100%; overflow-y: scroll; } .campaign-header { position: fixed; top: 0; z-index: 1000; background: #000; width: 100%; height: 55px; color:#fff;""} .scroll-campaign-list { margin-top: 95px; } 
  <body class="campaign-body"> <div class="campaign-header"> <div class="row admin-header"> <div class="header-title"> <h4 class="white-txt header-name">LISTING</h4> </div> </div> </div> <div class="scroll-campaign-list" id="campaigns-list"> <div class="row campaigns"> <div class="twelve columns list-items"> <h5 class="white-txt campaign-name">Blah blah</h5> <p class="campaign-question grey-txt">Blah blah</p> </div> </div> <hr> <div class="row campaigns"> <div class="twelve columns list-items"> <h5 class="white-txt campaign-name">Blah blah</h5> <p class="campaign-question grey-txt">Blah blah</p> </div> </div> <hr><div class="row campaigns"> <div class="twelve columns list-items"> <h5 class="white-txt campaign-name">Blah blah</h5> <p class="campaign-question grey-txt">Blah blah</p> </div> </div> <hr><div class="row campaigns"> <div class="twelve columns list-items"> <h5 class="white-txt campaign-name">Blah blah</h5> <p class="campaign-question grey-txt">Blah blah</p> </div> </div> <hr><div class="row campaigns"> <div class="twelve columns list-items"> <h5 class="white-txt campaign-name">Blah blah</h5> <p class="campaign-question grey-txt">Blah blah</p> </div> </div> <hr><div class="row campaigns"> <div class="twelve columns list-items"> <h5 class="white-txt campaign-name">Blah blah</h5> <p class="campaign-question grey-txt">Blah blah</p> </div> </div> <hr><div class="row campaigns"> <div class="twelve columns list-items"> <h5 class="white-txt campaign-name">Blah blah</h5> <p class="campaign-question grey-txt">Blah blah</p> </div> </div> <hr><div class="row campaigns"> <div class="twelve columns list-items"> <h5 class="white-txt campaign-name">Blah blah</h5> <p class="campaign-question grey-txt">Blah blah</p> </div> </div> <hr><div class="row campaigns"> <div class="twelve columns list-items"> <h5 class="white-txt campaign-name">Blah blah</h5> <p class="campaign-question grey-txt">Blah blah</p> </div> </div> <hr> </div> </body> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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