为什么我的桌子在小屏幕上占据超过100%的身体宽度?

时间:2016-07-12 15:12:31

标签: html css responsive-design width html-table

我在2个包装div中有一个表的代码,外部带有display: table,内部带有display:table-cell; vertical-align: middle,因此表格将显示在屏幕的中心。

但问题是当我将窗口大小调整到小于550px时,我的代码没有按预期显示。该表停止在该宽度以下调整大小。

当我删除.page.vcenter包装时,表格按预期调整大小。



* {
  -webkit-touch-callout: none;
  /* iOS Safari */
  -webkit-user-select: none;
  /* Chrome/Safari/Opera */
  -khtml-user-select: none;
  /* Konqueror */
  -moz-user-select: none;
  /* Firefox */
  -ms-user-select: none;
  /* Internet Explorer/Edge */
  user-select: none;
  /* Non-prefixed version, currently
										  not supported by any browser */
}
/*************************************/

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
}
.page {
  display: table;
  text-align: center;
  width: 100%;
  height: 100%;
}
.vcenter {
  display: table-cell;
  vertical-align: middle;
}
/*************************************/

table {
  display: flex;
  overflow: hidden;
}
thead {
  display: flex;
  flex-shrink: 0;
  width: 30%;
}
tbody {
  display: flex;
  position: relative;
  overflow-x: auto;
  overflow-y: hidden;
  width: 70%;
}
tr {
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}
td,
th {
  display: block;
}
td {
  border-left: 0;
}
td,
th {
  border: 1px solid;
}
th:not(:last-child),
td:not(:last-child) {
  border-bottom: 0;
}
tr {
  width: 100%;
}
th {
  background-color: DarkSlateBlue;
  text-weight: bold;
  color: white;
}

<div class="page">
  <div class="vcenter">

    <table>
      <thead>
        <tr>
          <th>&nbsp;</th>
          <th>Filipe</th>
          <th>Crisp</th>
          <th>Marco</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Fase 1</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Fase 2</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Fase 3</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Fase 4</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Fase 5</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Fase 6</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Fase 7</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Fase 8</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Fase 9</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Fase 10</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Fase 11</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Fase 12</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Fase 13</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Fase 14</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Fase 15</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
      </tbody>
    </table>

  </div>
</div>
&#13;
&#13;
&#13;

(点击&#34;查看整页&#34;并尝试将窗口大小调整为小于550px以查看会发生什么)

发生了什么事?

1 个答案:

答案 0 :(得分:1)

您需要使用table-layout: fixed

* {
  -webkit-touch-callout: none;
  /* iOS Safari */
  -webkit-user-select: none;
  /* Chrome/Safari/Opera */
  -khtml-user-select: none;
  /* Konqueror */
  -moz-user-select: none;
  /* Firefox */
  -ms-user-select: none;
  /* Internet Explorer/Edge */
  user-select: none;
  /* Non-prefixed version, currently
										  not supported by any browser */
}
/*************************************/

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
}
.page {
  display: table;
  table-layout: fixed;
  text-align: center;
  width: 100%;
  height: 100%;
}
.vcenter {
  display: table-cell;
  vertical-align: middle;
}
/*************************************/

table {
  display: flex;
  overflow: hidden;
}
thead {
  display: flex;
  flex-shrink: 0;
  width: 30%;
}
tbody {
  display: flex;
  position: relative;
  overflow-x: auto;
  overflow-y: hidden;
  width: 70%;
}
tr {
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}
td,
th {
  display: block;
}
td {
  border-left: 0;
}
td,
th {
  border: 1px solid;
}
th:not(:last-child),
td:not(:last-child) {
  border-bottom: 0;
}
tr {
  width: 100%;
}
th {
  background-color: DarkSlateBlue;
  text-weight: bold;
  color: white;
}
<div class="page">
  <div class="vcenter">

    <table>
      <thead>
        <tr>
          <th>&nbsp;</th>
          <th>Filipe</th>
          <th>Crisp</th>
          <th>Marco</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Fase 1</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Fase 2</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Fase 3</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Fase 4</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Fase 5</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Fase 6</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Fase 7</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Fase 8</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Fase 9</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Fase 10</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Fase 11</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Fase 12</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Fase 13</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Fase 14</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Fase 15</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
      </tbody>
    </table>

  </div>
</div>