Scrolling div without fixed height

前端 未结 3 887
故里飘歌
故里飘歌 2021-02-01 03:44

I need to build a dynamically-resizing scrolling div.

The div should dynamically resize to fit the screen. But if the content doesn\'t fit on the screen, it should disp

相关标签:
3条回答
  • 2021-02-01 04:28

    Since IE9 you can use viewport units.

    Let's say that the height of your container is dynamic, unless its size is greater than the window height. In that case we stop the expansion & activate the scroll.

    #container{
      background: #eaeaea;
      max-height: 100vh;
      overflow-y: scroll;
    }
    
    div{
      outline: 1px solid orange;
      width: 200px;
      height: 200px;
    }
    <div id='container'>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
    </div>

    0 讨论(0)
  • 2021-02-01 04:37

    If you are trying to make element fit the screen then you can set the value of hight and width of the element to 100%.

    You will also need to set the height of html and body

    html, body {height: 100%}
    #gridcontaine {
       height: 100%;
       width: 100%;
     }
    
    0 讨论(0)
  • 2021-02-01 04:41

    You could stretch the div using absolute positioning. This way it will always take the size of the browser window (or the closest positioned ancestor).

    Given this HTML:

    <div id="gridcontainer"></div>
    

    the CSS should be something like:

    #gridcontainer {
      position: absolute;
      top: 0; bottom: 0; left: 0; right: 0;
      overflow: auto;
    }
    

    Live Demo

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