0

I am showing a scroll bar on mouse hover and it makes a flicker effect in UI. Can anyone suggest how to avoid it? The code is here

  .parent {
        width:100%;
        max-height: 400px;
        overflow-y: hidden;
      }
     .parent:hover {
        overflow-y: scroll;
     }
     .table {
       width: 100%;
     }
<div class="parent">
   <table class="table">
      <tr><td>Key</td><td>Value</td></tr>
      <tr><td>Key</td><td>Value</td></tr>
      <tr><td>Key</td><td>Value</td></tr>
    </table>
</div>
balaG
  • 431
  • 1
  • 8
  • 16
  • The exact code to reproduce the issue is here https://stackblitz.com/edit/angular-wjd2f6?file=src%2Fapp%2Fapp.component.html – balaG Jul 24 '19 at 18:56
  • Is it a common problem and can't be fixed? – balaG Jul 24 '19 at 21:19
  • You are required to post a [mcve] here **within your question** and NEVER a third party site! – Rob Jul 24 '19 at 22:28

6 Answers6

4

You have two options:

  1. add a margin to the right at the same size of the scroll width (I believe default to be 16px). margin-right: 16px. I'm not pretty sure about this method though.

  2. If you don't care too much about cross-browser compatibility, just use overflow: overlay and you got it

Edit: IE is dead so we can all be happy now with #2

0

If this table is to be scrollable, its bad UI practice to only show that it is scrollable when the user hovers over it (what about touch devices?).

The best way to solve this and the flickering would be to not hide the scrollbars at all like so:

  .parent {
        width:100%;
        max-height: 400px;
        overflow-y: scroll; // or overflow-y:auto; 
      }
     .table {
       width: 100%;
     }
Anthony Cregan
  • 960
  • 11
  • 25
0

Use this trick to solve that:

.parent {
  width: 100%;
  max-height: 200px;
  overflow: auto;
  visibility: hidden;
}

.parent:hover,
.parent:focus {
  visibility: visible;
}

.child {
  visibility: visible;
}

.table {
  width: 100%;
}
<div class="parent">
  <div class="child">
    <table class="table">
      <tr>
        <td>Key</td>
        <td>Value</td>
      </tr>
      <tr>
        <td>Key</td>
        <td>Value</td>
      </tr>
      <tr>
        <td>Key</td>
        <td>Value</td>
      </tr>
      <tr>
        <td>Key</td>
        <td>Value</td>
      </tr>
      <tr>
        <td>Key</td>
        <td>Value</td>
      </tr>
      <tr>
        <td>Key</td>
        <td>Value</td>
      </tr>
      <tr>
        <td>Key</td>
        <td>Value</td>
      </tr>
      <tr>
        <td>Key</td>
        <td>Value</td>
      </tr>
      <tr>
        <td>Key</td>
        <td>Value</td>
      </tr>
      <tr>
        <td>Key</td>
        <td>Value</td>
      </tr>
      <tr>
        <td>Key</td>
        <td>Value</td>
      </tr>
      <tr>
        <td>Key</td>
        <td>Value</td>
      </tr>
    </table>
  </div>
</div>
ThS
  • 4,597
  • 2
  • 15
  • 27
Behzad AM
  • 111
  • 5
  • Adding a child div and using visibility. See the code. – Behzad AM Jul 24 '19 at 22:33
  • Thank you for explaining yourself. You should do that every time. https://stackoverflow.com/help/how-to-answer – Rob Jul 25 '19 at 00:35
  • Thank you Rob. I'll do it. CSS isn't like other programming languages that implement algorithms.In CSS world some techniques have been found by developers and they are hard to explain. – Behzad AM Jul 25 '19 at 11:57
  • When an element `visibility: hidden;` hides the element, but it still takes up space in the layout. Descendants of the element will be visible if they have visibility set to visible. Now we know why child is visible. Easily you can change the visibility parent on the hover event. – Behzad AM Jul 25 '19 at 12:18
0

Firstyl you can add CSS width: to scroll bar. After that, add to .parent{} width (subtract scroll bar width from 100%).You can use CSS calc(); function for this. When you hover parent, add CSS width 100% . But if you want to use the table width absolutely 100%, there is a guy solved via jQuery: https://stackoverflow.com/a/17380697/8660891 *Sorry for my bad English :)

::-webkit-scrollbar {
    width: 15px;
    padding:0px;
}
::-webkit-scrollbar-thumb {
  min-height:50px;
  background-color:#888;
  height: 50px;
}
::-webkit-scrollbar-track {
    background: #e3e3e3;
}
.parent {
        width:calc(100% - 15px);
        max-height: 200px;
        overflow-y: hidden;
      }
     
     .parent:hover {
        overflow-y: scroll;
        width: 100%;
     }
     .table {
       width: 100%;
     }
<div class="parent">
   <table class="table">
      <tr><td>Key</td><td>Value</td></tr>
      <tr><td>Key</td><td>Value</td></tr>
      <tr><td>Key</td><td>Value</td></tr>
      <tr><td>Key</td><td>Value</td></tr>
      <tr><td>Key</td><td>Value</td></tr>
      <tr><td>Key</td><td>Value</td></tr>
      <tr><td>Key</td><td>Value</td></tr>
      <tr><td>Key</td><td>Value</td></tr>
      <tr><td>Key</td><td>Value</td></tr>
      <tr><td>Key</td><td>Value</td></tr>
      <tr><td>Key</td><td>Value</td></tr>
      <tr><td>Key</td><td>Value</td></tr>
      <tr><td>Key</td><td>Value</td></tr>
    </table>
</div>
ariferol01
  • 221
  • 4
  • 13
  • Hi This solution works for Chrome and not for firefox. Is there a way to work on FF – balaG Jul 25 '19 at 15:59
  • Can you try this `::-moz-scrollbar { width: 15px; }` on Firefox. I dont use now FF. I ifthis solve problem, i will add to my answer' css code. @balaG – ariferol01 Jul 25 '19 at 16:36
0

instead of manually adding the scrollbar width you can use the scrollbar-gutter CSS property:

The scrollbar-gutter CSS property allows authors to reserve space for the scrollbar, preventing unwanted layout changes as the content grows while also avoiding unnecessary visuals when scrolling isn't needed.

Gianmarco
  • 792
  • 2
  • 14
  • 38
0

This resolved the issue for me:

scrollbar-gutter: stable both-edges;
Rich
  • 6,470
  • 15
  • 32
  • 53