调整HTML表格的列

adjust columns of a table in HTML

本文关键字:表格 HTML 调整      更新时间:2023-09-26

我想有一个表格,这样,如果页面的宽度减小,表格的列应该显示一个在另一个下面

您可以使用CSS显示属性来改变表的行为方式。为了使表单元格一个接一个地放在另一个上面,您需要创建一个媒体查询,它将表和每个单元格设置为display: block,在最适合您需要的断点处。

在下面的例子中,当屏幕宽度缩小到500px时,表格单元格将自动换行。

<标题> 例子

@media (max-width: 500px) {
    table {
        display: block;
        border: solid 1px #f00;
    }
    table td {
        display: block;
        border: solid 1px #f00;
    }
}
<table>
    <tr>
        <td>Column 1</td>
        <td>Column 2</td>
    </tr>
</table>

<标题>

默认table标签使用display: table,表单元格使用display: table-cell。通过改变这些属性,我们可以改变表的显示方式。

有关显示属性的更多信息,请参阅本文:

https://developer.mozilla.org/en-US/docs/Web/CSS/display

有关媒体查询的更多信息,请参阅以下文章:

https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Media_queries

好吧,如果我理解正确的话,你想要这样的东西:http://codepen.io/geoffyuen/pen/FCBEg

这个解决方案将使你的表响应,所以它可以做你想做的(至少我想我知道你想做什么)

只是一些html代码的例子

<h1>RWD List to Table</h1>
<table class="rwd-table">
  <tr>
    <th>Movie Title</th>
    <th>Genre</th>
    <th>Year</th>
    <th>Gross</th>
  </tr>
  <tr>
    <td data-th="Movie Title">Star Wars</td>
    <td data-th="Genre">Adventure, Sci-fi</td>
    <td data-th="Year">1977</td>
    <td data-th="Gross">$460,935,665</td>
  </tr>
  <tr>
    <td data-th="Movie Title">Howard The Duck</td>
    <td data-th="Genre">"Comedy"</td>
    <td data-th="Year">1986</td>
    <td data-th="Gross">$16,295,774</td>
  </tr>
  <tr>
    <td data-th="Movie Title">American Graffiti</td>
    <td data-th="Genre">Comedy, Drama</td>
    <td data-th="Year">1973</td>
    <td data-th="Gross">$115,000,000</td>
  </tr>
</table>

然后是CSS

@import "compass/css3";
// More practical CSS...
// using mobile first method (IE8,7 requires respond.js polyfill https://github.com/scottjehl/Respond)
$breakpoint-alpha: 480px; // adjust to your needs
.rwd-table {
  margin: 1em 0;
  min-width: 300px; // adjust to your needs
  tr {
    border-top: 1px solid #ddd;
    border-bottom: 1px solid #ddd;
  }
  th {
    display: none; // for accessibility, use a visually hidden method here instead! Thanks, reddit!   
  }
  td {
    display: block; 
    &:first-child {
      padding-top: .5em;
    }
    &:last-child {
      padding-bottom: .5em;
    }
    &:before {
      content: attr(data-th)": "; // who knew you could do this? The internet, that's who.
      font-weight: bold;
      // optional stuff to make it look nicer
      width: 6.5em; // magic number :( adjust according to your own content
      display: inline-block;
      // end options
      @media (min-width: $breakpoint-alpha) {
        display: none;
      }
    }
  }
  th, td {
    text-align: left;
    @media (min-width: $breakpoint-alpha) {
      display: table-cell;
      padding: .25em .5em;
      &:first-child {
        padding-left: 0;
      }
      &:last-child {
        padding-right: 0;
      }
    }
  }

}

// presentational styling
@import 'http://fonts.googleapis.com/css?family=Montserrat:300,400,700';
body {
  padding: 0 2em;
  font-family: Montserrat, sans-serif;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  color: #444;
  background: #eee;
}
h1 {
  font-weight: normal;
  letter-spacing: -1px;
  color: #34495E;
}
.rwd-table {
  background: #34495E;
  color: #fff;
  border-radius: .4em;
  overflow: hidden;
  tr {
    border-color: lighten(#34495E, 10%);
  }
  th, td {
    margin: .5em 1em;
    @media (min-width: $breakpoint-alpha) { 
      padding: 1em !important; 
    }
  }
  th, td:before {
    color: #dd5;
  }
}

希望这对你有帮助