html - CSS 响应表不工作

标签 html css responsive-design

我正在为表格开发响应式 CSS。我从这个链接阅读了一个教程 http://css-tricks.com/responsive-data-tables/ (如何在 css 上制作响应表)。它在某些示例页面上有效,但在我的 HTML 页面中,我在调整窗口大小方面遇到了一些问题,并且表格没有正确响应。这是我的 HTML 代码:

    <style>
        /* 
Generic Styling, for Desktops/Laptops 
*/
        table
        {
            width: 100%;
            border-collapse: collapse;
        }
        /* Zebra striping */
        tr:nth-of-type(odd)
        {
            background: #eee;
        }
        th
        {
            background: #333;
            color: white;
            font-weight: bold;
        }
        td, th
        {
            padding: 6px;
            border: 1px solid #ccc;
            text-align: left;
        }
        /* 
Max width before this PARTICULAR table gets nasty
This query will take effect for any screen smaller than 760px
and also iPads specifically.
*/
        @media only screen and (max-width: 760px), (min-device-width: 768px) and (max-device-width: 1024px)
        {
            /* Force table to not be like tables anymore */
            table, thead, tbody, th, td, tr
            {
                display: block;
            }
            /* Hide table headers (but not display: none;, for accessibility) */
            thead tr
            {
                position: absolute;
                top: -9999px;
                left: -9999px;
            }
            tr
            {
                border: 1px solid #ccc;
            }
            td
            {
                /* Behave  like a "row" */
                border: none;
                border-bottom: 1px solid #eee;
                position: relative;
                padding-left: 50%;
            }
            td:before
            {
                /* Now like a table header */
                position: absolute; /* Top/left values mimic padding */
                top: 6px;
                left: 6px;
                width: 45%;
                padding-right: 10px;
                white-space: nowrap;
            }
            /*
    Label the data
    */
            td:nth-of-type(1):before
            {
                content: "First Name";
            }
            td:nth-of-type(2):before
            {
                content: "Last Name";
            }
            td:nth-of-type(3):before
            {
                content: "Job Title";
            }
            td:nth-of-type(4):before
            {
                content: "Favorite Color";
            }
            td:nth-of-type(5):before
            {
                content: "Wars of Trek?";
            }
            td:nth-of-type(6):before
            {
                content: "Porn Name";
            }
            td:nth-of-type(7):before
            {
                content: "Date of Birth";
            }
            td:nth-of-type(8):before
            {
                content: "Dream Vacation City";
            }
            td:nth-of-type(9):before
            {
                content: "GPA";
            }
            td:nth-of-type(10):before
            {
                content: "Arbitrary Data";
            }
        }
    </style>
    <body>
        <table>
            <thead>
                <tr>
                    <th>
                        First Name
                    </th>
                    <th>
                        Last Name
                    </th>
                    <th>
                        Job Title
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        James
                    </td>
                    <td>
                        Matman
                    </td>
                    <td>
                        Chief Sandwich Eater
                    </td>
                </tr>
                <tr>
                    <td>
                        The
                    </td>
                    <td>
                        Tick
                    </td>
                    <td>
                        Crimefighter Sorta
                    </td>
                </tr>
            </tbody>
        </table>
    </body>
</html>

它显示了调整窗口大小的输出,如下图所示:

enter image description here

它在 js-fiddle 上运行良好,如下图所示:

enter image description here

链接:http://jsfiddle.net/Cd9Yp/

我的代码有什么问题?谁能指导一下,谢谢!

最佳答案

尝试在您指定的视口(viewport)之间定义一个 min-width。到父元素 table 和子元素 tr(如果需要)。

table { 
  width: 100%; 
  min-width: 320px; // this should do it
  border-collapse: collapse; 
}

和。

tr { 
   min-width: 200px; // specify if additionally needed or wanted
}

您要优化什么分辨率——我看到您的@media 是平板电脑,但您的拍摄显然更小——您不需要低于 320px;在大多数情况下,因此,如果您将其最小化到该值以下,那无关紧要。

此外,为什么不使用嵌套的 div

关于html - CSS 响应表不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21824080/

相关文章:

c# - 如何在 AngularJS 中包含动态模板?

css - 使用css在图像上垂直对齐

html - CSS:通过文本区域的动态高度

javascript - 检查是否按下任何键 Javascript 没有表单

javascript - 使用从服务器动态填充的信息对 HTML 表进行排序

javascript - 仅当 JavaScript 加载后,链接才应变为可点击状态

javascript - 如何在使用 Angular 在文本区域中键入时将每个单词的首字母大写

javascript - 即使滚动它也让 div 粘在底部

html - 在移动 View 中单击时菜单不会展开

responsive-design - 根据窗口宽度响应字体大小,CSS 有可能吗?