【问题标题】:How to make horizontal scroll on page如何在页面上进行水平滚动
【发布时间】:2012-11-02 23:06:18
【问题描述】:

我有一个 div,我需要在其中放置内部 div,我需要使所有内部 div 成行并且应该显示水平滚动条(我知道这听起来很疯狂,但我需要那个)。我尝试了容器宽度自动和溢出滚动,但没有。

如何做到这一点?

我的标记:

   <!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <title>text-overflow</title>
  <style>
  body{
    width: auto;
  }
  #items{
  overflow-x: scroll;
  width: auto;
  }
  .item{
     display: inline-block;
     width: 400px;
     height: 100px;
     border:1px solid;
  }
  </style>
 </head>
 <body>

        <div id="items">
           <div class="item">
             Item content
           </div>
           <div class="item">
             Item content
           </div>
           <div class="item">
             Item content
           </div>
           <div class="item">
             Item content
           </div>
           <div class="item">
             Item content
           </div>
           <div class="item">
             Item content
           </div>
        </div>

 </body>
</html>

【问题讨论】:

    标签: html css scroll horizontal-scrolling


    【解决方案1】:

    #items 上使用white-space: nowrap;

    #items{
        overflow-x: scroll;
        width: auto;
        white-space: nowrap;
    }
    

    DEMO

    【讨论】:

      【解决方案2】:

      只需将 #items 赋予 height 和 overflow-x:auto。

      #items{
            overflow-x: auto;
            width: auto;
             height: 200px;
            }
      

      【讨论】:

      • 要检查水平滚动,只需将 width:300px 给#items 而不是 width:auto。
      • nope - jsfiddle.net/Bhjvg/2 仔细阅读 OP 要求 " 我需要让所有内部 div in line"
      【解决方案3】:
      <div style="overflow-x:scroll;">
        <div style="width:1600px;">
          <div style="width:1500px;">
            <table>
              <tr>
                <td>  Your Value  </td>
              </tr>
            </table>
          </div>
        </div>
      </div>
      

      【讨论】:

        【解决方案4】:

        我想你想要这样的东西......

        :root {
          --gutter: 20px;
        }
        
        .app {
          padding: var(--gutter) 0;
          display: grid;
          grid-gap: var(--gutter) 0;
          grid-template-columns: var(--gutter) 1fr var(--gutter);
          align-content: start;
        }
        
        .app > * {
          grid-column: 2 / -2;
        }
        
        .app > .full {
          grid-column: 1 / -1;
        }
        
        .hs {
          display: grid;
          grid-gap: calc(var(--gutter) / 2);
          grid-template-columns: repeat(6, calc(50% - var(--gutter) * 2));
          grid-template-rows: minmax(150px, 1fr);
          
          overflow-x: scroll;
          scroll-snap-type: x proximity;
          padding-bottom: calc(.75 * var(--gutter));
          margin-bottom: calc(-.25 * var(--gutter));
        }
        
        
        /* Demo styles */
        
        html,
        body {
          height: 100%;
        }
        
        body {
          display: grid;
          place-items: center;
          background: #456173;
        }
        
        ul {
          list-style: none;
          padding: 0;
        }
        
        h1,
        h2,
        h3 {
          margin: 0;
        }
        
        .app {
          width: 375px;
          height: 667px;
          background: #DBD0BC;
          overflow-y: scroll;
        }
        
        .hs > li,
        .item {
          scroll-snap-align: center;
          padding: calc(var(--gutter) / 2 * 1.5);
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          background: #fff;
          border-radius: 8px;
        }
        <div class="app">
          <h1>Some headline</h1>
          
          <ul class="hs">
            <li class="item">test</li>
            <li class="item">test</li>
            <li class="item">test</li>
            <li class="item">test</li>
            <li class="item">test</li>
            <li class="item">test</li>
          </ul>
          
          <div class="container">
            <div class="item">
              <h3>Block for context</h3>
            </div>
          </div>
        </div>

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-01-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-08-17
          • 2016-03-14
          相关资源
          最近更新 更多