8

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

4 回答 4

23

使用white-space: nowrap;#items

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

演示

于 2012-11-14T12:47:58.013 回答
2

只需将#items 设置为 height 和 overflow-x:auto。

#items{
      overflow-x: auto;
      width: auto;
       height: 200px;
      }
于 2012-11-14T12:51:26.963 回答
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>
于 2013-11-23T22:37:17.167 回答
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>

于 2021-01-02T04:13:59.893 回答