2015-02-23 36 views
1

我试图在响应式网站上设计购物篮的样式。 在小屏幕上,设计要求“名称”列为全宽,并且位于同一表格行中的其他值之上。只有一行​​在行中为全宽(显示:块)在手机上

在过去,我使用'display:block'来允许在移动设备上创建样式表 - 但是在将它应用到一个td时,这似乎不起作用。什么是我最好的选择,以保持不同宽度字段的表格单元格的灵活性,同时使名称字段全宽并且位于行的其余部分之上?

查看片段,例如。

table td.name { 
 
    display: block; 
 
    width: 100%; 
 
}
<table> 
 
    <thead> 
 
    <tr> 
 
     <th scope="col">Name</th> 
 
     <th scope="col">Quantity</th> 
 
     <th scope="col">Price</th> 
 
     <th scope="col">Line Total</th> 
 
    </tr> 
 
    </thead> 
 
    <tbody> 
 
    <tr> 
 
     <td class="name">Product name, can be quite long and may span several lines</td> 
 
     <td>3</td> 
 
     <td>£201.38</td> 
 
     <td>£604.14</td> 
 
    </tr> 
 
    </tbody> 
 
</table>

+1

我怀疑,这将工作以任何方式使用'display'。什么样的作品是将表格单元格绝对定位,然后给其他单元格填充顶部以“保留”必要的空间 - 但只有当您知道第一个单元格的内容有多高呈现时才是一种选择。除此之外,您可以通过不使用HTML表格元素并仅以表格的方式对其进行格式化来实现此目的 - 这将允许您为其余三个元素插入额外的分组元素......但当然这样'd失去表格语义... – CBroe 2015-02-23 23:52:00

+0

但是有几个jQuery插件和其他类似的解决方案用于“响应式”表 - 也许其中一个可以提供帮助。 – CBroe 2015-02-23 23:53:23

+0

谢谢@CBroe - 我得出了类似的结论。 JS方法似乎最有可能成功。 – steve 2015-02-24 08:18:37

回答

1

一种方式做到这一点不用JavaScript是把名字到文档中两次,并使用媒体查询隐藏你不需要的副本。我不确定屏幕阅读器如何处理这个问题。

table { width: 100%; } 
 

 
@media (min-width:600px) { 
 
    .name1 { display: none; } 
 
    .name2 { display: table-cell; } 
 
} 
 
@media (max-width:600px) { 
 
    .name1 { display: block; } 
 
    .name2 { display: none; } 
 
}
<div class="name1">Product name, can be quite long and may span several lines</div> 
 

 
<table> 
 
    <thead> 
 
    <tr> 
 
     <th scope="col" class="name2">Name</th> 
 
     <th scope="col">Quantity</th> 
 
     <th scope="col">Price</th> 
 
     <th scope="col">Line Total</th> 
 
    </tr> 
 
    </thead> 
 
    <tbody> 
 
    <tr> 
 
     <td class="name2">Product name, can be quite long and may span several lines</td> 
 
     <td>3</td> 
 
     <td>£201.38</td> 
 
     <td>£604.14</td> 
 
    </tr> 
 
    </tbody> 
 
</table>

jsfiddle here

+0

从配音的快速测试来看,这对屏幕阅读器来说肯定是不利的,但它让我想到了一个可行的想法,非常感谢。 – steve 2015-02-24 08:18:08

+0

做屏幕阅读器了解任何css命令吗?像一些@media查询? – roeland 2015-02-24 08:24:04