0

html file

* {
  margin: 0px;
}

.header {
  background-color: tomato;
  height: 60px;
  position: sticky;
  top: 0;
}

.contents {
  height: 2500px;
}

.footer {
  position: sticky;
  bottom: 0;
  height: 65px;
  background-color: green;
}
<body>
  <div>
    <div class="contents">
      <div class="header">
      </div>
      1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>

      <div class="footer"></div>
    </div>
    <!--if footer is here, footer works-->
  </div>
</body>

I have two sticky elements the header and footer. footer doesn't work, but header works, even footer and header are in same element.(they are sibling). Only top: 0 works correctly, but bottom: 0 works incorrectly.

Sfili_81
  • 2,377
  • 8
  • 27
  • 36
JinYeong Kim
  • 139
  • 1
  • 7

2 Answers2

0

its simple, make it like this

<body>
    <div>
        <div class="contents">
            <div class="header">
            </div>
            1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>
        </div>
        <div class="footer"></div>

    </div>
</body>
Alvino
  • 21
  • 10
0

You can use flexbox and wrap content between the header and footer and add flex: 1 to contain all remaining part

something like this:

* {
  margin: 0px;
}

.header {
  background-color: tomato;
  height: 60px;
  position: sticky;
  top: 0;
}

.contents {
  height: 2500px;
  display:flex;
  flex-direction:column;
  
}

.mainContent{
  flex:1;
}

.footer {
  position: sticky;
  bottom: 0;
  height: 65px;
  background-color: green;
}
   <div>
    <div class="contents">
      <div class="header">
      </div>
      <div class="mainContent">
      1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>1234<br/>

        </div>
      <div class="footer"></div>
    </div>
  </div>
Ali Sattarzadeh
  • 3,220
  • 1
  • 6
  • 20