响应表,聪明的方式

时间:2013-11-01 08:58:28

标签: html css responsive-design css-tables fluid-layout

我有一个包含数据的表。表格数据。它看起来像这样。

enter image description here

请参阅this fiddle

现在我想要的是,当它显示在较窄的屏幕上时,表格看起来像这样,这样你就不会得到一个水平滚动条,它保持相同的视觉结构:

enter image description here

(或者如果你愿意,可以像this fiddle一样。)

现在我的问题是,你是怎么做到的?我更喜欢CSS的方式,但到目前为止,我还没有设法只用CSS。 (第二个小提琴包含rowspan属性,它们没有CSS等价物。)

HTML 生成的服务器端,因此我可以根据窗口宽度生成两种布局中的一种,但是它不会响应窗口大小调整。

我不反对一个小Javascript,但在这种情况下,要将第一个表转换为第二个窗口调整大小,它需要拆分并重建,逐个单元格,我认为这太过分了。仍在寻找可以完成所有工作的媒体查询。

尝试了一下,我来了this close,但这在IE8和IE9中不起作用。

那么,有没有人有任何想法如何解决这个问题?理想的解决方案适用于不同高度的表格单元格(2行文本或更多)和任意数量的列。

4 个答案:

答案 0 :(得分:2)

你的html是相同的,你可以根据媒体查询更改css属性的样式,更好的解决方案是 - fiddle

@media only screen and (min-width: 769px) {
       #content {
    border:1px solid;
    border-collapse:collapse;
}
#content td, #content th {
    border:1px solid;
    text-align:left;
    padding:.07em .2em;
    white-space:nowrap;
    font-weight:400;
}
}
@media only screen and (max-width: 768px) {
#content {
    border:1px solid;
    border-collapse:collapse;
}
#content tr {
    height:4em; border-bottom:1px solid;
}
#content th {
    border:1px solid;
    text-align:left;
    padding:.07em .2em;
    white-space:nowrap;
    font-weight:400;
    height:4em;
}
#content td {
    padding:.07em .2em;
    white-space:nowrap;
    height:1.4em;
    display:inline;
}
#content td:not(:last-child)::after {
    display:block; content:'';
    height:0;
    border-bottom:1px solid;
}

}

一种可能的解决方案是使用媒体查询隐藏相应的块 或相应更改样式

这是一个fiddle
将较小的表ID更改为content2

@media only screen and (max-width: 768px) {
    #content{
        display:none !important;
    }
}
@media only screen and (min-width: 769px) {
    #content2{
        display:none !important;
    }
}

答案 1 :(得分:1)

我知道这不是你想要的,但我前段时间创建了一个jsfiddle作为参考可能有所帮助:jsfiddle.net/webbymatt/MVsVj/1

基本上标记保持不变,没有JS,内容只是按预期回流。您只需要将data-type属性添加到表格单元格中。

答案 2 :(得分:0)

您可以内联阻止元素。我没有太多时间玩,但有类似的东西:

#content {
    border:1px solid;
    border-collapse:collapse;
}
#content td, #content th {
    border:1px solid;
    text-align:left;
    padding:.07em .2em;
    white-space:nowrap;
    font-weight:400;
}
#content td {
    display: inline-block;
    width: 100px;
}

但这不是最漂亮的创作!

http://jsfiddle.net/8H3bN/

答案 3 :(得分:0)

选中CodePen

我很久以前在css-tricks.com找到了这个解决方案。

桌子有点乱:

<table>
    <tr>
        <td>
            Description 1
        </td>
        <td>
            <table class="responsive" cellpadding="0" cellspacing="0">
                <tbody>
                    <tr>
                        <td class="text-center">Data 1A</td>
                        <td class="text-center">Data 1B</td>
                        <td class="text-center">Data 1C</td>
                    </tr>
                </tbody>
            </table>                
        </td>
    </tr>
    <tr>
        <td>
            Description 2
        </td>
        <td>
            <table class="responsive" cellpadding="0" cellspacing="0">
                <tbody>
                    <tr>
                        <td>Data 2A</td>
                        <td>Data 2B</td>
                        <td>Data 2C</td>
                    </tr>
                </tbody>
            </table>                
        </td>
    </tr>
</table>

这就是css:

    /* Small display targeting */
    @media only screen and (max-width: 767px) {
        /* Force table to not be like tables anymore */
        .responsive, .responsive thead, .responsive tbody, .responsive th, .responsive td, .responsive tr {
            display: block;
        }

        /* Hide table headers (but not display: none;, for accessibility) */
        .responsive thead tr {
            position: absolute;
            top: -9999px;
            left: -9999px;
        }

        .responsive td {
            /* Behave  like a "row" */
            position: relative;
        }

        .responsive td:before {
            /* Now like a table header */
            position: absolute;
        }
    }