Child table with 100% width extends parent

后端 未结 1 1334
广开言路
广开言路 2020-12-21 07:44

at the moment I am trying to create a child table, that can have a width of more than 100% of the parent. Therefor they should have a scrollbar. But, when I try this with my

相关标签:
1条回答
  • 2020-12-21 08:03

    You need to use table-layout:fixed; ref. the automatic layout may give unexpected result.

    Fixed table layout

    With this (fast) algorithm, the horizontal layout of the table does not depend on the contents of the cells; it only depends on the table's width, the width of the columns, and borders or cell spacing.

    As a side note, the same issue happen with or without width:100%

    body {
      margin: 0;
      padding: 0;
      width: 100%;
      background-color: green;
    }
    
    .main {
      margin: 0 10%;
      padding: 00;
      width: 80%;
      display: table;
    }
    
    table {
      margin: 0;
      padding: 0;
      display: block;
      overflow-x: auto;
      white-space: nowrap;
    }
    <main class="main">
      <table>
        <tr>
          <td>A very very very veeeeeeeeeeeeeeeeeeeeery loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong line</td>
          <td>...</td>
        </tr>
        <tr>
          <td>...</td>
          <td>...</td>
        </tr>
        <tr>
          <td>...</td>
          <td>...</td>
        </tr>
      </table>
    </main>
    <main class="main" style="table-layout: fixed;">
      <table>
        <tr>
          <td>A very very very veeeeeeeeeeeeeeeeeeeeery loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong line</td>
          <td>...</td>
        </tr>
        <tr>
          <td>...</td>
          <td>...</td>
        </tr>
        <tr>
          <td>...</td>
          <td>...</td>
        </tr>
      </table>
    </main>

    0 讨论(0)
提交回复
热议问题