Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

html - How to make horizontal overflow for table inside a div?

I am trying to bring the scroll bar to bottom so that all tables should display when I scroll horizontally. I tried many steps but not able to achieve it.

Here is the code - JSFiddle

I have tried following code but not able to achieve it.

overflow-x: auto;
white-space: nowrap;

Here is an image what I want to achieve. image

<div style="width: 750px; height: 321px; overflow-x: auto;">
  <table align="left" cellspacing="0">
    <tbody>
      <tr>
        <th style="width: 200px; height: 85px">Demo
        </th>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
    </tbody>
  </table>
  <table align="left" cellspacing="0">
    <tbody>
      <tr>
        <th style="width: 200px; height: 85px">Demo
        </th>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
    </tbody>
  </table>
  <table align="left" cellspacing="0">
    <tbody>
      <tr>
        <th style="width: 200px; height: 85px">Demo
        </th>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
    </tbody>
  </table>
  <table align="left" cellspacing="0">
    <tbody>
      <tr>
        <th style="width: 200px; height: 85px">Demo
        </th>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
    </tbody>
  </table>
  <table align="left" cellspacing="0">
    <tbody>
      <tr>
        <th style="width: 200px; height: 85px">Demo
        </th>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
      <tr>
        <td style="height: 24px">12 (0)</td>
      </tr>
    </tbody>
  </table>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I removed nearly all your inline-style and re-added them as css-style. This makes the code way shorter and easier.

you have 5 tables within your div-wrapper. Each table has a width of 200px. So you need to enlarge the wrapping div width to 1000px. 750px would not allow to contain 5 tables with 200px each. You are missing 250px width. This causes 2 of the tables to drop down below the first 3 tables.

Last but not least, I gave the tables the property: box-sizing: border box;. That way you dont have to calculate the paddings and border thickness. Otherwise you would need a wrappign div width of 1010px to take the 5x left + right borders with 1px each into account.

.table-wrapper {
  min-width: 1000px;
  overflow-x: auto;
  box-sizing: border-box;
}

.table-wrapper * {
  box-sizing: border-box;
}

th {
  width: 200px;
  height: 85px;
  text-align: center;
}

td {
  height: 24px;
}
<div style="width: 750px; overflow-x: auto;">
  <div class="table-wrapper">
    <table align="left" cellspacing="0">
      <tbody>
        <tr>
          <th>Demo</th>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
      </tbody>
    </table>
    <table align="left" cellspacing="0">
      <tbody>
        <tr>
          <th>Demo</th>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
      </tbody>
    </table>
    <table align="left" cellspacing="0">
      <tbody>
        <tr>
          <th>Demo</th>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
      </tbody>
    </table>
    <table align="left" cellspacing="0">
      <tbody>
        <tr>
          <th>Demo</th>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
      </tbody>
    </table>
    <table align="left" cellspacing="0">
      <tbody>
        <tr>
          <th>Demo</th>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
        <tr>
          <td>12 (0)</td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...