【问题标题】:Responsive tables, the smart way响应式表格,智能方式
【发布时间】:2013-11-12 11:14:15
【问题描述】:

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

this fiddle

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

(或者如果你愿意,比如this fiddle。)

现在我的问题是,你是怎么做到的?我更喜欢仅使用 CSS 的方式,但到目前为止,我还没有设法仅在 CSS 中做到这一点。 (第二个小提琴包含 rowspan 属性,没有 CSS 等效项。)

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

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

试验了一下,我来了this close,但是在IE8和IE9下不行。

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

【问题讨论】:

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


【解决方案1】:

检查这个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;
        }
    }

【讨论】:

    【解决方案2】:

    ya 因为您的 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。我会继续寻找一种不涉及两次发送相同数据的方法。
    • eidtted 我的答案。这样你就不必使用 html twise 但添加两次不同屏幕尺寸的 css 类。
    【解决方案3】:

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

    基本上标记保持不变,没有 JS,内容只是按预期重排。您只需将数据类型属性添加到表格单元格。

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

      • 感谢您的帮助。但恐怕这个在 IE9 中也行不通。
      • 啊!好的 - 值得一试(不幸的是,我无法在 IE9 上进行测试)。我们最近确实遇到了类似的问题,发现在标题中添加&lt;meta http-equiv="X-UA-Compatible" content="IE=8"&gt;,但这是IE8。搜索后有IE9的一些信息。
      猜你喜欢
      • 1970-01-01
      • 2014-11-04
      • 1970-01-01
      • 2016-09-03
      • 2014-06-02
      • 1970-01-01
      • 2010-09-26
      • 1970-01-01
      相关资源
      最近更新 更多