调整HTML表格的列
adjust columns of a table in HTML
我想有一个表格,这样,如果页面的宽度减小,表格的列应该显示一个在另一个下面
您可以使用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;
}
}
希望这对你有帮助
相关文章:
- 将HTML表格导出到excel时,无法将数据加载到excel文件中
- 打印预览没有应用程序页眉和页脚的html表格
- 将HTML表格导出到带有文本和图像的excel中
- JS图表和html表格重叠
- 带有单选按钮的html表格复制粘贴到xls中
- 应用程序脚本中导入HTML的数据擦除;谷歌表格
- HTML/JavaScript表格标题
- 如何使用谷歌脚本将html表单数据保存到谷歌电子表格中
- 如何在HTML中制作带导航的表格
- HTML表格导出为具有适当列宽和字体的pdf
- html表格的浮动水平滚动条
- 使用HTML / Javascript将Web应用程序生成的图表/表格转换和导出到可编辑的Powerpoint(.ppt
- 如何在不导致 HTML 表格失真的情况下正确使用追加
- 如何使用 JQuQuery 解析 JSON 对象并在 HTML 页面中以表格格式打印输出
- ModalDialog:HTML表格应该是可滚动的,而不是整个页面
- HTML表格在打印后缺少边框颜色和背景颜色
- 如何将表格html表格导出为PDF
- 从下拉列表中选择所选值后显示表格 (HTML)
- 导入Excel文件到表格HTML页面
- 用javascript将表格html转换为Excel