如何在不明确设置高度的情况下使表格显示垂直滚动条?

时间:2015-01-27 17:45:33

标签: html css html5 css3

我有一些显示表格的代码。如果浏览器宽度减小使得表格比它宽,则会为表格而不是浏览器显示滚动条。这按照我想要的方式工作。但是,当我减小浏览器窗口的高度时,滚动条会出现在浏览器上而不是桌面上。

如何让滚动条出现在桌面上?

如果我将包含表格的div的高度设置为设定值(例如height:100px;),则滚动条会按预期显示。我不想做这个。我只是希望桌子根据需要耗尽屏幕上的所有垂直空间,一旦溢出,桌面上就会出现一个滚动条。

以下是fiddle,以下是代码:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Pivot Web</title>
    <link rel="stylesheet" href="style3.css">
</head>
<body>

<div id="main-content">

    <div id="field-selection-area">
        <ul class="no-text-selection">
            <li draggable="true">Period</li>
            <li draggable="true">Trade ID</li>
        </ul>
    </div>

    <div id="field-table-area">
        <div id="field-config-area">
            <div id="row-field-area" class="no-text-selection field-area">
                Drop Row Fields Here
            </div>
            <div id="column-field-area" class="no-text-selection field-area">
                Drop Column Fields Here
            </div>
        </div>

        <div id="table-area">
            <table>
                <tr><td>18.0</td><td>18.0</td><td>18.0</td><td>18.0</td><td>18.0</td><td>18.0</td><td>18.0</td><td>18.0</td><td>18.0</td><td>18.0</td></tr>
                <tr><td>5.3</td><td>5.3</td><td>5.3</td><td>5.3</td><td>5.3</td><td>5.3</td><td>5.3</td><td>5.3</td><td>5.3</td><td>5.3</td></tr>
                <tr><td>5.3</td><td>5.3</td><td>5.3</td><td>5.3</td><td>5.3</td><td>5.3</td><td>5.3</td><td>5.3</td><td>5.3</td><td>5.3</td></tr>
                <tr><td>5.3</td><td>5.3</td><td>5.3</td><td>5.3</td><td>5.3</td><td>5.3</td><td>5.3</td><td>5.3</td><td>5.3</td><td>5.3</td></tr>
                <tr><td>5.3</td><td>5.3</td><td>5.3</td><td>5.3</td><td>5.3</td><td>5.3</td><td>5.3</td><td>5.3</td><td>5.3</td><td>5.3</td></tr>
            </table>
        </div>
    </div>
</div>

</body>
</html>

和css:

#main-content{
    display: flex;
    background-color: lightblue;
}

#field-selection-area{
    flex-shrink: 0;
    border: 1px darkgrey solid;
    background-color: aliceblue;
}

#field-selection-area ul{
    list-style: none;
    padding: 4px;
    margin: 0;
}

#field-selection-area ul li{
    padding: 4px;
}

#field-table-area{
    border-right: 1px darkgrey solid;
    border-bottom: 1px darkgrey solid;
}

.field-area {
    border-top: 1px darkgrey solid;
    padding: 4px;
    background-color: aliceblue;
}

#table-area{
    border-top: 1px darkgrey solid;
    overflow: auto;
    background-color: white;
    height: 100px;
}

#table-area table td{
    border: 1px darkgrey solid;
}

我希望我已经解释得很好。

2 个答案:

答案 0 :(得分:0)

设置相对于视口的高度。

#table-area {max-height: 100vh;}

或者,您可以根据视口调整整个正文的大小,将其拆分为flexboxes的子部分,这些部分会根据可用空间和内容需求自动调整大小,然后设置overflow:auto只是在表的flexbox容器上。 这将为页面提供更多的桌面应用程序&#34;感觉在包含批量数据的可滚动子部分周围有一些固定的UI元素。

答案 1 :(得分:0)

我最终设法做到了这一点,所以把代码放在这里以防其他人需要它。

*,
*:before,
*:after {
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}
html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}
#main-content {
  height: 100%;
  display: flex;
  flex-direction: row;
}
#field-table-area {
  /*width: 100%;*/
  height: 100%;
  display: flex;
  flex-direction: column;
}
.field-area {
  border-bottom: 1px darkgrey solid;
  padding: 4px;
  background-color: aliceblue;
}
.flexbox-item-grow {
  flex: 1;
  /* same as flex: 1 1 auto; */
}
#field-selection-area {
  /*height: 100%;*/
  overflow: auto;
  border-right: 1px darkgrey solid;
}
#field-selection-area ul {
  list-style: none;
  padding: 4px;
  margin: 0;
}
#field-selection-area ul li {
  padding: 2px;
  white-space: nowrap;
}
#field-selection-area ul li:hover {
  border: 1px darkgrey solid;
  border-radius: 3px;
  padding: 1px;
  background-color: rgb(220, 230, 241);
}
#table-area {
  display: flex;
  flex-direction: row;
}
#table-holder {
  overflow: auto;
}
#table-holder table td {
  border: 1px darkgrey solid;
}
<!DOCTYPE html>
<html>

<head lang="en">
  <meta charset="UTF-8">
  <title>Pivot Web</title>
  <link rel="stylesheet" href="style4.css">
</head>

<body>

  <div id="main-content">
    <div id="field-selection-area">
      <ul class="no-text-selection">
        <li draggable="true">Period</li>
        <li draggable="true">Market</li>
        <li draggable="true">Trade ID</li>
        <li draggable="true">Something</li>
        <li draggable="true">Something Space</li>
      </ul>
    </div>

    <div id="field-table-area" class="flexbox-item-grow">
      <div id="field-config-area">
        <div id="filter-field-area" class="no-text-selection field-area">
          Drop Filter Fields Here
        </div>

        <div id="row-field-area" class="no-text-selection field-area">
          Drop Row Fields Here
        </div>

        <div id="column-field-area" class="no-text-selection field-area">
          Drop Column Fields Here
        </div>
      </div>

      <div id="table-area" class="flexbox-item-grow">
        <div id="table-holder" class="flexbox-item-grow">
          <table>
            <tr>
              <td>18.0</td>
              <td>18.0</td>
              <td>18.0</td>
              <td>18.0</td>
              <td>18.0</td>
              <td>18.0</td>
              <td>18.0</td>
              <td>18.0</td>
              <td>18.0</td>
              <td>18.0</td>
              <td>18.0</td>
              <td>18.0</td>
              <td>18.0</td>
              <td>18.0</td>
              <td>18.0</td>
            </tr>
            <tr>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
            </tr>
            <tr>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
            </tr>
            <tr>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
            </tr>
            <tr>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
              <td>5.3</td>
            </tr>
          </table>
        </div>
      </div>
    </div>
  </div>

</body>

</html>

这允许表格在比浏览器窗口更宽或更高时滚动。

屏幕截图显示处于以下状态:

Scrolling Table