常见的响应式表格

简单自适应表格

通过给表格的外面加了一个.table-container的标签

.table-container
{
width: 100%;
overflow-y: auto;
_overflow: auto;
margin: 0 0 1em;
}
table{border:0; border-collapse:collapse;}
table td,table th{border:1px solid #999; padding:.5em 1em}
//添加IOS下滚动条
.table-container::-webkit-scrollbar
{
-webkit-appearance: none;
width: 14px;
height: 14px;
}

.table-container::-webkit-scrollbar-thumb
{
border-radius: 8px;
border: 3px solid #fff;
background-color: rgba(0, 0, 0, .3);
}

演示1

bootstrap3自适应表格

Bootstrap3.0也类似这样子的简单自适应,当屏幕小于767像素时,表格就会自适应,多的隐藏可以滚动。

@media (max-width: 767px) {
.table-responsive {
width: 100%;
margin-bottom: 15px;
overflow-x: scroll;
overflow-y: hidden;
border: 1px solid #dddddd;
-ms-overflow-style: -ms-autohiding-scrollbar;
-webkit-overflow-scrolling: touch;
}
.table-responsive > .table {
margin-bottom: 0;
}
.table-responsive > .table > thead > tr > th,
.table-responsive > .table > tbody > tr > th,
.table-responsive > .table > tfoot > tr > th,
.table-responsive > .table > thead > tr > td,
.table-responsive > .table > tbody > tr > td,
.table-responsive > .table > tfoot > tr > td {
white-space: nowrap;
}

隐藏表格栏目

随着屏幕宽度变小而删除一些内容

@media only screen and (max-width: 800px) {
#unseen table td:nth-child(2),
#unseen table th:nth-child(2) {display: none;}
}

@media only screen and (max-width: 640px) {
#unseen table td:nth-child(4),
#unseen table th:nth-child(4),
#unseen table td:nth-child(7),
#unseen table th:nth-child(7),
#unseen table td:nth-child(8),
#unseen table th:nth-child(8){display: none;}
}

翻转滚动表格

当屏幕宽度小于800时,表格内容则会发生翻转,表头的内容会放在左边。右边则是会出现滚动,超出的隐藏。这个要求是表格比较完整,不然不是很好看。在表格的外面加个#flip-scroll,该技术来源于这里

table tr td, table tr th{white-space:nowrap;}
@media only screen and (max-width: 800px) {

#flip-scroll .cf:after { visibility: hidden; display: block; font-size: 0; content: " "; clear: both; height: 0; }
#flip-scroll * html .cf { zoom: 1; }
#flip-scroll *:first-child+html .cf { zoom: 1; }

#flip-scroll table { width: 100%; border-collapse: collapse; border-spacing: 0; }

#flip-scroll th,
#flip-scroll td { margin: 0; vertical-align: top; }
#flip-scroll th { text-align: left; }

#flip-scroll table { display: block; position: relative; width: 100%; }
#flip-scroll thead { display: block; float: left; }
#flip-scroll tbody { display: block; width: auto; position: relative; overflow-x: auto; white-space: nowrap; }
#flip-scroll thead tr { display: block; }
#flip-scroll th { display: block; text-align: right; }
#flip-scroll tbody tr { display: inline-block; vertical-align: top; }
#flip-scroll td { display: block; min-height: 1.25em; text-align: left; }

/* sort out borders */

#flip-scroll th { border-bottom: 0; border-left: 0; }
#flip-scroll td { border-left: 0; border-right: 0; border-bottom: 0; }
#flip-scroll tbody tr { border-left: 1px solid #babcbf; }
#flip-scroll th:last-child,
#flip-scroll td:last-child { border-bottom: 1px solid #babcbf; }
}

没有更多的表格

该项技术在这里靠的是使用HTML5的数据属性(data)和CSS来让他们显示在表头里面。另一个就是直接在CSS里面写文字,但我们知道这个对于CSS来说是一个巨大禁忌。

<!doctype html>
<html>
    <head>
    <link id="bootstrap_221" rel="stylesheet" type="text/css" class="library" href="/js/sandbox/bootstrap-2.2.1/css/bootstrap.min.css">
    <script id="jquery_172" type="text/javascript" class="library" src="/js/sandbox/jquery/jquery-1.7.2.min.js"></script>
    <script id="bootstrap_221" type="text/javascript" class="library" src="/js/sandbox/bootstrap-2.2.1/js/bootstrap.min.js"></script>   
</head>
<body>
    <div id="no-more-tables">
    <table class="table table-bordered">
    <thead>
        <tr>
            <th>Code</th>
            <th>Company</th>
            <th class="numeric">Price</th>
            <th class="numeric">Change</th>
            <th class="numeric">Change %</th>
            <th class="numeric">Open</th>
            <th class="numeric">High</th>
            <th class="numeric">Low</th>
            <th class="numeric">Volume</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td data-title="Code">AAC</td>
            <td data-title="Company">AUSTRALIAN AGRICULTURAL COMPANY LIMITED.</td>
            <td data-title="Price" class="numeric">$1.38</td>
            <td data-title="Change" class="numeric">-0.01</td>
            <td data-title="Change %" class="numeric">-0.36%</td>
            <td data-title="Open" class="numeric">$1.39</td>
            <td data-title="High" class="numeric">$1.39</td>
            <td data-title="Low" class="numeric">$1.38</td>
            <td data-title="Volume" class="numeric">9,395</td>
        </tr>
    </tbody>
</table>
</div>
</body>
</html>

@media only screen and (max-width: 800px) {

    /* Force table to not be like tables anymore */
    #no-more-tables table, 
    #no-more-tables thead, 
    #no-more-tables tbody, 
    #no-more-tables th, 
    #no-more-tables td, 
    #no-more-tables tr { 
        display: block; 
    }

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

    #no-more-tables tr { border: 1px solid #ccc; }

    #no-more-tables td { 
        /* Behave  like a "row" */
        border: none;
        border-bottom: 1px solid #eee; 
        position: relative;
        padding-left: 50%; 
        white-space: normal;
        text-align:left;
    }

    #no-more-tables 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;
        text-align:left;
        font-weight: bold;
    }

    /*
    Label the data
    */
    #no-more-tables td:before { content: attr(data-title); }
}

no-more-tables

另外还发现了一些结合了JS使用的表格。

foundation的自适应表格

演示地址

原文来自:http://caibaojian.com/responsive-tables.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值