0

I have a table where i try to make the tbody scroll if it's greater than 1000px.

I've tried the following:

.fixed_header tbody{
    display:block;
    overflow:auto;
    height:1000px;
    width:100%;
}

.fixed_header thead tr{
    display:block;
}

The scroll works, but it ruines my style.

Before

After

Live Result: https://www.bunlongheng.com/baby/1?code=rithys4k&date=2021-03-01

How do I keep the scrolling but let the rest of the page remain fixed?


Code

<table class="table fixed_header">

    <thead class="thin-border-bottom">
        <tr style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
            <th>Type</th>
            <th>Time</th>
            <th>Note</th>
        </tr>
    </thead>

    <tbody class="tbody-log">

        <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
            <td>
                <a class="btn btn-feed">
                    <img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924">
                </a>
            </td>
            <td>
                <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">
                01:59 am
                </a>
                <div class="modal fade" id="edit_446">
                    <div class="model-content" style="margin-top: 200px;">
                        <div class="col-sm-offset-4 col-sm-2 col-md-offset-5 col-md-2 text-center">
                            <img width="80" src="/assets/be/img/baby/feed.png">
                            <br><br><br>
                            <input type="text" value="01:59:50" name="updatedAt" width="100%" height="80">
                            <br><br>
                            <button onclick="updateLog(446)" class="btn btn-success btn-block">Done</button>
                            <br>
                            <button onclick="deleteLog(446)" class="btn btn-danger btn-block">Delete</button>
                        </div>
                    </div>
                </div>
            </td>
            <td id="td-446">
                <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
            </td>
        </tr>
        
    </tbody>
    
</table>


Edit

The goal on mobile is to let the content have the full width and not to have space below the table.

enter image description here

biberman
  • 5,606
  • 4
  • 11
  • 35
code-8
  • 54,650
  • 106
  • 352
  • 604
  • I was not planning to clutter the post with the code. – code-8 Mar 27 '21 at 13:35
  • Yes. I use it Bootstrap. – code-8 Mar 30 '21 at 18:23
  • 1
    This is a duplicate of many, many, posts, including [How to set tbody height with overflow scroll](https://stackoverflow.com/q/23989463/215552) – Heretic Monkey Mar 30 '21 at 19:16
  • And here's one more thats excatly like your case: [HTML table with 100% width, with vertical scroll inside tbody](https://stackoverflow.com/questions/17067294/html-table-with-100-width-with-vertical-scroll-inside-tbody) (btw: it explains the `display-block` used in the answer below – 0stone0 Mar 30 '21 at 19:23
  • You tagged your question with 'jquery'. Could you please add the relevant jquery code for us? – biberman Apr 04 '21 at 23:44
  • I had to use a line of jQuery to set the css property base on window.height – code-8 Apr 04 '21 at 23:56
  • Please add this line of jQuery to your question... – biberman Apr 05 '21 at 06:40

1 Answers1

3

I think i have a solution:

  • You have to make the 'tbody' to a 100vh block element with overflow.
  • For hiding the scrollbar use only overflow-y:scroll with box-sizing:content-box and padding-right.
  • For aligning the body- and head-cells set a width and a text-align for them.
  • The head-cells need to be inline-blocks, but not the head-row 'tr' – don't set them to display:block.
  • Since your table is already at 1000px width wrapping down and the whole page gets scrollable, you don't need a scrollable 'tbody' and can wrap the releated css code in a media query so that it is affecting the html only over 1000px width.
@media screen and (min-width: 1000px) {

    .table.fixed_header>tbody {
        display: block;
        box-sizing: content-box;
        height: 100vh;
        width: 100%;
        padding-right: 32px;
        overflow-y: scroll;
    }

    .table.fixed_header>thead>tr>th {
        display: inline-block;
        width: 30%;
        text-align: center;
    }

    .table.fixed_header>tbody>tr>td {
        width: 30%;
        text-align: center;
    }

}

Working example (with smaller width because the small snippet window):

#table-wrapper {
  height: 100vh;
  width: 50%;
  overflow: hidden;
}

.table.fixed_header {
  width: 100%;
}

@media screen and (min-width: 400px) {

    .table.fixed_header>tbody {
      display: block;
      box-sizing: content-box;
      height: 100vh;
      width: 100%;
      padding-right: 32px;
      overflow-y: scroll;
    }

    .table.fixed_header>thead>tr>th {
      display: inline-block;
      width: 30%;
      text-align: center;
    }

    .table.fixed_header>tbody>tr>td {
      width: 30%;
      text-align: center;
    }

}
<div id="table-wrapper">
  <div id="linkBox">
    <a>some link,</a>
    <a>another link</a>
  </div>
  <table class="table fixed_header">

    <thead class="thin-border-bottom">
      <tr style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <th>Type</th>
        <th>Time</th>
        <th>Note</th>
      </tr>
    </thead>

    <tbody class="tbody-log">

      <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <td>
          <a class="btn btn-feed"><img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924"></a>
        </td>
        <td>
          <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">01:59 am</a>
        </td>
        <td id="td-446">
          <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
        </td>
      </tr>
      <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <td>
          <a class="btn btn-feed"><img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924"></a>
        </td>
        <td>
          <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">01:59 am</a>
        </td>
        <td id="td-446">
          <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
        </td>
      </tr>
      <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <td>
          <a class="btn btn-feed"><img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924"></a>
        </td>
        <td>
          <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">01:59 am</a>
        </td>
        <td id="td-446">
          <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
        </td>
      </tr>
      <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <td>
          <a class="btn btn-feed"><img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924"></a>
        </td>
        <td>
          <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">01:59 am</a>
        </td>
        <td id="td-446">
          <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
        </td>
      </tr>
      <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <td>
          <a class="btn btn-feed"><img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924"></a>
        </td>
        <td>
          <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">01:59 am</a>
        </td>
        <td id="td-446">
          <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
        </td>
      </tr>
      <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <td>
          <a class="btn btn-feed"><img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924"></a>
        </td>
        <td>
          <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">01:59 am</a>
        </td>
        <td id="td-446">
          <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
        </td>
      </tr>
      <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <td>
          <a class="btn btn-feed"><img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924"></a>
        </td>
        <td>
          <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">01:59 am</a>
        </td>
        <td id="td-446">
          <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
        </td>
      </tr>
      <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <td>
          <a class="btn btn-feed"><img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924"></a>
        </td>
        <td>
          <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">01:59 am</a>
        </td>
        <td id="td-446">
          <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
        </td>
      </tr>
      <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <td>
          <a class="btn btn-feed"><img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924"></a>
        </td>
        <td>
          <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">01:59 am</a>
        </td>
        <td id="td-446">
          <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
        </td>
      </tr>
      <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <td>
          <a class="btn btn-feed"><img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924"></a>
        </td>
        <td>
          <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">01:59 am</a>
        </td>
        <td id="td-446">
          <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
        </td>
      </tr>
      <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <td>
          <a class="btn btn-feed"><img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924"></a>
        </td>
        <td>
          <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">01:59 am</a>
        </td>
        <td id="td-446">
          <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
        </td>
      </tr>
      <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <td>
          <a class="btn btn-feed"><img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924"></a>
        </td>
        <td>
          <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">01:59 am</a>
        </td>
        <td id="td-446">
          <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
        </td>
      </tr>
      <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <td>
          <a class="btn btn-feed"><img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924"></a>
        </td>
        <td>
          <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">01:59 am</a>
        </td>
        <td id="td-446">
          <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
        </td>
      </tr>
      <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <td>
          <a class="btn btn-feed"><img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924"></a>
        </td>
        <td>
          <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">01:59 am</a>
        </td>
        <td id="td-446">
          <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
        </td>
      </tr>

    </tbody>

  </table>
</div>
biberman
  • 5,606
  • 4
  • 11
  • 35
  • The desktop seems working, but on the phone, show so much extra code in the bottom. See this on the phone : https://www.bunlongheng.com/baby/1?code=rithys4k&date=2021-04-02 It has your code. – code-8 Apr 05 '21 at 15:34
  • I tested the site in 4 browsers on my andoid phone and in 4 browsers on my windows convertible (also in mobile view in the developer tools). But i saw no extra code - not at the bottom and not at any other place. There were just the elements that i see in a desktop browser. The only bug i found was the width of 33% causing in firefox (mobile and desktop) the table head to wrap in a second line. You can fix this by setting the width of 'th' and 'tr' from 33% to 30%. What says the extra code, that you see? Could it be an error message? – biberman Apr 05 '21 at 16:09
  • I apologize I used the wrong word. I mean on mobile I see extra left over open space. – code-8 Apr 05 '21 at 16:25
  • Sorry again for my typo*, please see this : https://i.imgur.com/9H0Bb3U.jpg It won't use the whole width, and show so much extra spaces in the bottom. – code-8 Apr 05 '21 at 16:27
  • When I commented out your code, I see no dead space & it take full width : https://i.imgur.com/d4Qzm7J.jpg – code-8 Apr 05 '21 at 16:29
  • I updated my post also to show the result comparison between what I have vs what you provided. I think you're pretty close the desktop, but still issue on mobile. – code-8 Apr 05 '21 at 16:35
  • As i don't see the empty space at the bottom i can't inspect it. But i can inspect the very small empty stripe on the right side, that i see in microsoft edge (but way smaller then that in your image) This small stripe was difficult to notice because your content looks perfectly centered in every device and browser i tested. I will look if i find something. But what about your phone - could it be, that the browser or the os is to old or are not supporting some features from your code? Which os is it? – biberman Apr 05 '21 at 17:04
  • Since your page is in mobile view scrollable you don't need the scrollable 'tbody' you could simply use one of your media queries (600px or 400px) to "comment out" the styles in the mobile view – something like: ```@media screen and (min-width: 600px) { ...css styles to comment out when below 600px...}``` – biberman Apr 05 '21 at 17:31
  • I’m testing on both iOS and Android iPhone 12 Pro Max and pixel xl 2 – code-8 Apr 05 '21 at 17:47
  • I've seen now that the table is already at 1000px width wrapping down. So maybe you would want to set the media query to ```@media screen and (min-width: 1000px) { ...```... – biberman Apr 05 '21 at 17:51
  • Can you please kindly update your answer ? I will reupload your latest codes with updated media queries also. – code-8 Apr 05 '21 at 18:04