针对移动设备将 5 列表拆分为两部分

Posted

技术标签:

【中文标题】针对移动设备将 5 列表拆分为两部分【英文标题】:Break a 5-column table into two parts for mobile devices 【发布时间】:2021-06-25 10:03:27 【问题描述】:

我有一个在桌面浏览器中正确显示的 5 列表。

在移动浏览器中,它使页面太宽,并添加了我不想要的水平滚动条。

是否有媒体查询 CSS 规则可以帮助将此表拆分为 3 列 / 2 列垂直堆叠而不是 5 列?

示例:在移动设备上,而不是:

A  B  C  D  E
aa bb cc dd ee
ff gg hh ii jj

应该是

A  B  C  
aa bb cc 
ff gg hh 

D  E
dd ee
ii jj

<table>
<tbody>
<tr>
<th scope="col"><strong>A</strong></th>
<th scope="col"><strong>B</strong></th>
<th scope="col"><strong>C</strong></th>
<th scope="col"><strong>D</strong></th>
<th scope="col"><strong>E</strong></th>
</tr>
<tr><td>aa</td><td>bb</td><td>cc</td><td>dd</td><td>ee</td></tr>
<tr><td>ff</td><td>gg</td><td>hh</td><td>ii</td><td>jj</td></tr>
<tr><td>ff</td><td>gg</td><td>hh</td><td>ii</td><td>jj</td></tr>
</tbody>
</table>

【问题讨论】:

作为此解决方案的替代方案,您可以使用word-break 属性来避免创建分表 另外,你可以看这篇关于responsive data table的帖子 @StepUp word-break 终于解决了,请随时发布有关此问题的答案! 好的,很高兴它对您有所帮助! :) 【参考方案1】:

可以使用word-break属性来避免冗长的句子

.your-class 
  width: 80px;  
  word-break: break-word;

一个例子:

.your-class 
  width: 80px;
  word-break: break-word;
&lt;div class="your-class"&gt;It is a very long string&lt;/div&gt;

【讨论】:

这不是我最初的想法(我最初想将表格分成两部分),但它终于奏效了 :) @Basj 很高兴您找到了 CSS 解决方案:)【参考方案2】:

通常当我需要做这样的事情,或者我使用水平滚动或者像这个例子一样破坏表格时,你可以使用 flexbox 或你喜欢的东西来控制块的大小:

@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); 

            
<section id="no-more-tables">
          <h2>No More Tables</h2>
         
          <table class="table-bordered table-striped table-condensed cf">
              <thead class="cf">
                  <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>
                    <tr>
                        <td data-title="Code">AAD</td>
                        <td data-title="Company">ARDENT LEISURE GROUP</td>
                        <td data-title="Price" class="numeric">$1.15</td>
                        <td data-title="Change" class="numeric">  +0.02</td>
                        <td data-title="Change %" class="numeric">1.32%</td>
                        <td data-title="Open" class="numeric">$1.14</td>
                        <td data-title="High" class="numeric">$1.15</td>
                        <td data-title="Low" class="numeric">$1.13</td>
                        <td data-title="Volume" class="numeric">56,431</td>
                    </tr>
                    <tr>
                        <td data-title="Code">AAX</td>
                        <td data-title="Company">AUSENCO LIMITED</td>
                        <td data-title="Price" class="numeric">$4.00</td>
                        <td data-title="Change" class="numeric">-0.04</td>
                        <td data-title="Change %" class="numeric">-0.99%</td>
                        <td data-title="Open" class="numeric">$4.01</td>
                        <td data-title="High" class="numeric">$4.05</td>
                        <td data-title="Low" class="numeric">$4.00</td>
                        <td data-title="Volume" class="numeric">90,641</td>
                    </tr>
                    <tr>
                        <td data-title="Code">ABC</td>
                        <td data-title="Company">ADELAIDE BRIGHTON LIMITED</td>
                        <td data-title="Price" class="numeric">$3.00</td>
                        <td data-title="Change" class="numeric">  +0.06</td>
                        <td data-title="Change %" class="numeric">2.04%</td>
                        <td data-title="Open" class="numeric">$2.98</td>
                        <td data-title="High" class="numeric">$3.00</td>
                        <td data-title="Low" class="numeric">$2.96</td>
                        <td data-title="Volume" class="numeric">862,518</td>
                    </tr>
                    <tr>
                        <td data-title="Code">ABP</td>
                        <td data-title="Company">ABACUS PROPERTY GROUP</td>
                        <td data-title="Price" class="numeric">$1.91</td>
                        <td data-title="Change" class="numeric">0.00</td>
                        <td data-title="Change %" class="numeric">0.00%</td>
                        <td data-title="Open" class="numeric">$1.92</td>
                        <td data-title="High" class="numeric">$1.93</td>
                        <td data-title="Low" class="numeric">$1.90</td>
                        <td data-title="Volume" class="numeric">595,701</td>
                    </tr>
                    <tr>
                        <td data-title="Code">ABY</td>
                        <td data-title="Company">ADITYA BIRLA MINERALS LIMITED</td>
                        <td data-title="Price" class="numeric">$0.77</td>
                        <td data-title="Change" class="numeric">  +0.02</td>
                        <td data-title="Change %" class="numeric">2.00%</td>
                        <td data-title="Open" class="numeric">$0.76</td>
                        <td data-title="High" class="numeric">$0.77</td>
                        <td data-title="Low" class="numeric">$0.76</td>
                        <td data-title="Volume" class="numeric">54,567</td>
                    </tr>
                    <tr>
                        <td data-title="Code">ACR</td>
                        <td data-title="Company">ACRUX LIMITED</td>
                        <td data-title="Price" class="numeric">$3.71</td>
                        <td data-title="Change" class="numeric">  +0.01</td>
                        <td data-title="Change %" class="numeric">0.14%</td>
                        <td data-title="Open" class="numeric">$3.70</td>
                        <td data-title="High" class="numeric">$3.72</td>
                        <td data-title="Low" class="numeric">$3.68</td>
                        <td data-title="Volume" class="numeric">191,373</td>
                    </tr>
                    <tr>
                        <td data-title="Code">ADU</td>
                        <td data-title="Company">ADAMUS RESOURCES LIMITED</td>
                        <td data-title="Price" class="numeric">$0.72</td>
                        <td data-title="Change" class="numeric">0.00</td>
                        <td data-title="Change %" class="numeric">0.00%</td>
                        <td data-title="Open" class="numeric">$0.73</td>
                        <td data-title="High" class="numeric">$0.74</td>
                        <td data-title="Low" class="numeric">$0.72</td>
                        <td data-title="Volume" class="numeric">8,602,291</td>
                    </tr>
                    <tr>
                        <td data-title="Code">AGG</td>
                        <td data-title="Company">ANGLOGOLD ASHANTI LIMITED</td>
                        <td data-title="Price" class="numeric">$7.81</td>
                        <td data-title="Change" class="numeric">-0.22</td>
                        <td data-title="Change %" class="numeric">-2.74%</td>
                        <td data-title="Open" class="numeric">$7.82</td>
                        <td data-title="High" class="numeric">$7.82</td>
                        <td data-title="Low" class="numeric">$7.81</td>
                        <td data-title="Volume" class="numeric">148</td>
                    </tr>
                    <tr>
                        <td data-title="Code">AGK</td>
                        <td data-title="Company">AGL ENERGY LIMITED</td>
                        <td data-title="Price" class="numeric">$13.82</td>
                        <td data-title="Change" class="numeric">  +0.02</td>
                        <td data-title="Change %" class="numeric">0.14%</td>
                        <td data-title="Open" class="numeric">$13.83</td>
                        <td data-title="High" class="numeric">$13.83</td>
                        <td data-title="Low" class="numeric">$13.67</td>
                        <td data-title="Volume" class="numeric">846,403</td>
                    </tr>
                    <tr>
                        <td data-title="Code">AGO</td>
                        <td data-title="Company">ATLAS IRON LIMITED</td>
                        <td data-title="Price" class="numeric">$3.17</td>
                        <td data-title="Change" class="numeric">-0.02</td>
                        <td data-title="Change %" class="numeric">-0.47%</td>
                        <td data-title="Open" class="numeric">$3.11</td>
                        <td data-title="High" class="numeric">$3.22</td>
                        <td data-title="Low" class="numeric">$3.10</td>
                        <td data-title="Volume" class="numeric">5,416,303</td>
                    </tr>
                </tbody>
          </table>
        
      </section>

您可以在此链接上看到一些解决响应式表格的选项: https://elvery.net/demo/responsive-tables/

希望对你有帮助!

【讨论】:

非常棒的解决方案。感谢显示代码。这就是我在这里的原因。只是一个评论:为什么不使用.class 而不是#id 使其可用于页面上的多用途?如果优先考虑选择器应该是该构造的问题,则可能与额外的通用标签结合使用(即div.no-more-table 将其设置在 div 包装器中)。

以上是关于针对移动设备将 5 列表拆分为两部分的主要内容,如果未能解决你的问题,请参考以下文章

移动web——移动开发选择和技术解决方案

基于移动设备上点击位置的下拉列表弹出 CSS

ECMAScript 5.1 移动兼容性

针对目标移动设备(第一)、平板电脑和桌面设备的特定媒体查询

针对窄桌面而非移动设备的媒体查询

移动应用程序 - 针对 iPhone、WP7、Android 和 Blackberry