Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set position fix horizontal and sticky vertical

I'm having actually a problem. I'm developing a website for my university.

There you have on top a typical header. Below this header is a div with some filter possibility to filter a table, which is positioned below.

The table is typically very large. So you have to scroll sometimes in both directions (horizontal and vertical). If you want to scroll, then should the header stay on its position. Between Header and filter div is a headline located. This headline should hide.

The filters should move a little bit to the top and then stay below the Header. For this, I used position: sticky. This works very well.

But if I scroll to the right, the filters move out of the viewport.

So is there a possibility, that the filter div is vertically sticky and stays horizontally in his fixed position?

Here is an example of my problem:

body {
  height: 100%;
  display: block;
}
.header {
  background-color: red;
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  z-index: 1;
}

.body {
  padding-top: 20px;
  width: 100%;
  height: 100%;
}

.filter {
  display: flex;
  flex-direction: row;
  align-items: center;
  background-color: blue;
  width: 100%;
  position: sticky;
  position: -webkit-sticky;
  top: 20px;
  z-index: 1;
}



th {
  width: 200px;
  background-color: purple;
  position: sticky;
  top: 40px;
}

td {
  width: 200px;
  background-color: lightblue;
}

table {
  position: relative;
  border-collapse: collapse;
  width: 1500px;
}
<html>
<body>
<div class="header">
Header
</div>
<div class="body">
  <h2>
    Headline
  </h2>
  <div class="filter">
    Filters
  </div>
  <span>some text</span>
  <div class="table">
    <table>
      <thead>
        <tr>
          <th>a</th>
          <th>a</th>
          <th>a</th>
          <th>a</th>
          <th>a</th>
          <th>a</th>
          <th>a</th>
          <th>a</th>
          <th>a</th>
          <th>a</th>
          <th>a</th>
          <th>a</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
        </tr>
        <tr>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
        </tr>
        <tr>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
        </tr>
        <tr>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
        </tr>
        <tr>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
        </tr>
        <tr>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
        </tr>
        <tr>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
        </tr>
        <tr>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
        </tr>
        <tr>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
        </tr>
        <tr>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
        </tr>
        <tr>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
        </tr>
        <tr>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
        </tr>
        <tr>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
        </tr>
        <tr>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
        </tr>
        <tr>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
        </tr>
        <tr>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
        </tr>
        <tr>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
        </tr>
        <tr>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
        </tr>
        <tr>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
          <td>b</td>
        </tr>
      </tbody>
    </table>
  </div>
</div>
</body>
</html>

So I don't want to lose the vertical sticky behavior when I scroll horizontal. I want to use sticky vertical and fixed horizontal at the same time.

like image 472
gumellupf Avatar asked Dec 21 '25 18:12

gumellupf


1 Answers

For those who are interested:

I've got it done! The solution was to set a specific width. Not a relative width like 100%.

Thank you all for your help.

like image 129
gumellupf Avatar answered Dec 23 '25 08:12

gumellupf