Home > other >  css: overflow scroll adds extra padding
css: overflow scroll adds extra padding

Time:08-17

I have the following html & css code:

<!DOCTYPE html>
<html>
  <head>
    <style type="text/css">
      html, body {
        margin: 0;
      }
      #container{
        width:100vw;
        height:100vh;
        background-color: green;
      }
      #left-bar{
        position: absolute;
        width: 300px;
        height: 100vh;
        background-color: red;
        overflow: scroll;
      }
    </style>
  </head>
  <body>
    <div id="container">
      <div id="left-bar" >
      </div>
    </div>
  </body>
</html>

It gives: extra padding overflow scroll

There is a padding inside the left bar, i do not understand why and how i can remove it.

Things I tried:

  • add padding: 0; on left bar, it does not work (with !important as well).
  • Use inspector to understand where the padding comes from. I did not find.
  • remove overflow: scroll on left-bar. It works but I want the left-bar to have the behavior overflow: scroll

How can I remove this padding inside the left-bar ?

EDIT: The bug disappeared with the exact same code.

Now with the same code i have: no padding

What i did:

  • restart my computer, the bug is still here
  • add css code to style the scroll bar
  • remove the code i added --> the bug disappeared

I do not understand why...

CodePudding user response:

What you are seeing is an empty scrollbar. Set the overflow overflow: auto. This will only show the scrollbar if the content is overflowing.

<!DOCTYPE html>
<html>
  <head>
    <style type="text/css">
      html, body {
        margin: 0;
      }
      #container{
        width:100vw;
        height:100vh;
        background-color: green;
      }
      #left-bar{
        position: absolute;
        width: 300px;
        height: 100vh;
        background-color: red;
        overflow: auto;
      }
    </style>
  </head>
  <body>
    <div id="container">
      <div id="left-bar" >
      </div>
    </div>
  </body>
</html>

CodePudding user response:

You might consider using a plugin that does a minimal scrollbar (maybe simplerbar). Also consider doing only overflow-y:scroll for more acceptable appearance. You could also change colors of the scroll gutter colors

var el1 = document.querySelector('[data-id=div1]');
var el2 = document.querySelector('[data-id=div2]');

instance1 = new SimpleBar(el1);
instance2 = new SimpleBar(el2);
<script src="https://cdnjs.cloudflare.com/ajax/libs/simplebar/5.3.8/simplebar.min.js" integrity="sha512-2SAzoBQi2FbvWkvcsoYW4PH478pUu0gwa7BRA6YQxg6kCWUIOIDfFHMLH57GbWeLa pkYitOD5j9mIhIt3CLKA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/simplebar/5.3.8/simplebar.min.css" integrity="sha512-uZTwaYYhJLFXaXYm1jdNiH6JZ1wLCTVnarJza7iZ1OKQmvi6prtk85NMvicoSobylP5K4FCdGEc4vk1AYT8b9Q==" crossorigin="anonymous" referrerpolicy="no-referrer"
/>

<div dir="ltr">

  <div data-id="div1" style="height: 300px; width: 200px; padding:10px; display: inline-block; border:1px solid gray; background: lightblue; ">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
      dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
  </div>

  <section data-id="div2" style="height: 300px; width: 200px; padding:10px; display: inline-block; border:1px solid gray">
    <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia
      voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi
      tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui
      in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</p>
    <p>But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of
      human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves
      or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical
      exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?</p>
  </section>


</div>

  • Related