No padding when using overflow: auto

后端 未结 10 2085
天涯浪人
天涯浪人 2020-12-13 23:33

I can\'t get padding-bottom to work when I use overflow-y: auto on a box.

HTML:

相关标签:
10条回答
  • 2020-12-14 00:00

    Based on isHristov's answer:

    #container {
        padding: 3em 3em 0 3em; /* padding-bottom: 0 */
        overflow-x: hidden;
        overflow-y: auto;
        width: 300px;
        height: 300px;
        background: red;
    }
    
    #container:after {
        content: "";
        display: block;
        height: 0;
        width: 100%;
        margin-bottom: 3em; /* length you wanted on padding-bottom */
    }
    

    However, his solution adds extra space in browsers that handle this situation properly.

    Dan Robinson's answer is great too unless you have multiple elements, in #container, that are dynamically shown/hidden. In that case :last-child might target a hidden element and have no effect.

    0 讨论(0)
  • 2020-12-14 00:06

    Here is a possible approach that is working perfectly :

    #container {
        overflow-x: hidden;
        overflow-y: auto;
        width: 300px;
        height: 300px;
    }
    
    #some_info {
        height: 900px;
        background: #000;
        border: 3em solid red;
    }
    
    0 讨论(0)
  • 2020-12-14 00:07

    Demo

    Hi now used to this css

    #container {
        padding: 3em;
        overflow-x: hidden;
        overflow-y: auto;
        width: 300px;
        height: 300px;
        background: red;
        padding-bottom:0; // add this line in your css
    }
    
    #some_info {
        height: 900px;
        background: #000;
        margin-bottom:3em; // add this line in your css
    }
    

    Demo

    0 讨论(0)
  • 2020-12-14 00:08

    The solutions above were not working for my needs, and I think I stumbled on a simple solution.

    If your container and overflowing content share the same background color, you can add a top and bottom border with the color matching the background color. To create equal padding all around, set the border width equal to the left and right padding of the container.

    Link to modified version of OP's fiddle: http://jsfiddle.net/dennisoneil/rwgZu/508/

    A simple example below.

    Note: Stack Overflow puts the snippet results into an overflow scroll, which makes it a little harder to see what's going on. The fiddle may be your best preview option.

    #container {
      background: #ccc;
      overflow-y: scroll;
      height: 190px;
      padding: 0 20px;
      border-top: 20px solid #ccc;
      border-bottom: 20px solid #ccc;
    }
    #overflowing {
      background: #ccc;
    }
    <div id="container">
      <div id="overflowing">
        This is content<br/>
        This is content<br/>
        This is content<br/>
        This is content<br/>
        This is content<br/>
        This is content<br/>
        This is content<br/>
        This is content<br/>
        This is content<br/>
        This is content<br/>
        This is content<br/>
        This is content<br/>
        This is content<br/>
        This is content<br/>
        This is content<br/>    
      </div>
    </div>

    0 讨论(0)
提交回复
热议问题