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
858 views
in Technique[技术] by (71.8m points)

html - Child table with 100% width extends parent

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 following css the child has a scrollbar and for some reason extends the parent containers width. It works fine, if I use a static size like 500px, but it fails with a width of 100%.

HTML

(simplified)

<main class="main">
    <table>
        <tr>
            <td>...</td>
            <td>...</td>
        </tr>
        <tr>
            <td>...</td>
            <td>...</td>
        </tr>
        <tr>
            <td>...</td>
            <td>...</td>
        </tr>
    </table>
</main>

CSS

(simplified)

.main {
    margin: 0 10%;
    padding: 00;
    width: 80%;
    display: table;
}

table {
    margin: 0;
    padding: 0;
    width: 100%;
    display: block;
    overflow-x: auto;
    white-space: nowrap;
}

Snippets

Example with static width of 500px

<!DOCTYPE html>
<html>

<head>
  <style>
    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;
      width: 500px;
      display: block;
      overflow-x: auto;
      white-space: nowrap;
    }
  </style>
</head>

<body>
  <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>
</body>

</html>
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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>

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

1.4m articles

1.4m replys

5 comments

57.0k users

...