如何在页面上进行水平滚动

时间:2012-11-14 12:44:31

标签: html css scroll horizontal-scrolling

我有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>

4 个答案:

答案 0 :(得分:19)

white-space: nowrap;

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

DEMO

答案 1 :(得分:2)

只需将#items提供给height和overflow-x:auto即可。

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

答案 2 :(得分:0)

<div style="overflow-x:scroll;">
  <div style="width:1600px;">
    <div style="width:1500px;">
      <table>
        <tr>
          <td> your value <td>
        </tr>
      </table
    </div>
  </div>
</div>

答案 3 :(得分:0)

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

: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>