css - 页面中有两个独立的响应表

标签 css

尝试在页面中创建分隔表:Kompiuterių nuoma

手机上的第一个表格看起来不错,但是,第二个使用与第一个相同的标签。如何让它继续下去?

CSS:

 table {
   width: 100%;
   border-collapse: collapse;
 }
 tr:nth-of-type(odd) {
   background: #eee;
 }
 th {
   background: #fbfbfb;
   color: black;
   font-weight: bold;
 }
 td,
 th {
   padding: 6px;
   border: 1px solid #ccc;
   text-align: left;
 }
 */@media only screen and (max-width: 760px),
 (min-device-width: 768px) and (max-device-width: 1024px) {
   table,
   thead,
   tbody,
   th,
   td,
   tr {
     display: block;
   }
   thead tr {
     position: absolute;
     top: -9999px;
     left: -9999px;
   }
   tr {
     border: 1px solid #ccc;
   }
   td {
     border: none;
     border-bottom: 1px solid #eee;
     position: relative;
     padding-left: 50%;
   }
   td:before {
     position: absolute;
     top: 6px;
     left: 6px;
     width: 45%;
     padding-right: 10px;
     white-space: nowrap;
   }
   td:nth-of-type(1):before {
     content: "";
   }
   td:nth-of-type(2):before {
     content: "MSO 365 Business";
   }
   td:nth-of-type(3):before {
     content: "ESET Endpoint AV";
   }
   td:nth-of-type(4):before {
     content: "ESET Endpoint SEC";
   }
   td:nth-of-type(5):before {
     content: "Krepšys";
   }
   td:nth-of-type(6):before {
     content: "Klaviatūra?";
   }
   td:nth-of-type(7):before {
     content: "Laidinė pelė";
   }
   td:nth-of-type(8):before {
     content: "Bevielė pelė";
   }
   td:nth-of-type(9):before {
     content: "USB Dock";
   }
   td:nth-of-type(10):before {
     content: "Basic Dock";
   }
   td:nth-of-type(11):before {
     content: "Pro Dock";
   }
   td:nth-of-type(12):before {
     content: "Kaina";
   }
 }

最佳答案

如果您向包含标题文本的 td 添加一个属性,您就可以完全消除将所有标题存储在 CSS 中的需要,并将您的代码简化为:

td:before {
    content: attr(title);
}

这适用于任意数量的表格,也意味着您可以轻松地将数据源中的正确标题直接输出到您的标记中;将它从你的 CSS 中完全移除。

例子:

table {
   width: 100%;
   border-collapse: collapse;
    margin-bottom: 30px;
 }
 tr:nth-of-type(odd) {
   background: #eee;
 }
 th {
   background: #fbfbfb;
   color: black;
   font-weight: bold;
 }
 td,
 th {
   padding: 6px;
   border: 1px solid #ccc;
   text-align: left;
 }
 @media only screen and (max-width: 760px) {
   table,
   thead,
   tbody,
   th,
   td,
   tr {
     display: block;
   }
   thead tr {
     position: absolute;
     top: -9999px;
     left: -9999px;
   }
   tr {
     border: 1px solid #ccc;
   }
   td {
     border: none;
     border-bottom: 1px solid #eee;
     position: relative;
     padding-left: 50%;
   }
   td:before {
     position: absolute;
     top: 6px;
     left: 6px;
     width: 45%;
     padding-right: 10px;
     white-space: nowrap;
   }
   td:before {
     content: attr(title);
   }
 }
<table>
    <tr>
        <th>Title 1</th>
        <th>Title 2</th>
    </tr>
    <tr>
        <td title="Title 1">1 TD</td>
        <td title="Title 2">2 TD</td>
    </tr>
    <tr>
        <td title="Title 1">3 TD</td>
        <td title="Title 2">4 TD</td>
    </tr>
    <tr>
        <td title="Title 1">5 TD</td>
        <td title="Title 2">6 TD</td>
    </tr>
    <tr>
        <td title="Title 1">7 TD</td>
        <td title="Title 2">8 TD</td>
    </tr>
</table>

<table>
    <tr>
        <th>Title 3</th>
        <th>Title 4</th>
    </tr>
    <tr>
        <td title="Title 3">9 TD</td>
        <td title="Title 4">10 TD</td>
    </tr>
    <tr>
        <td title="Title 3">11 TD</td>
        <td title="Title 4">12 TD</td>
    </tr>
    <tr>
        <td title="Title 3">13 TD</td>
        <td title="Title 4">14 TD</td>
    </tr>
    <tr>
        <td title="Title 3">15 TD</td>
        <td title="Title 4">16 TD</td>
    </tr>
</table>

JSFIDDLE VERSION

关于css - 页面中有两个独立的响应表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33649693/

相关文章:

html - 样式属性在嵌套段落元素中不起作用

ruby-on-rails - Rails 中的 CSS 样式

php - Paypal 代码错误。重定向到错误 400

html - 尽管设置了最小高度,图像仍扩展了容器 div?

css - 使用 Flexbox 重新创建 CSS 网格布局

html - 使图像适合最外层的 div,使包含图像的 div 适合图像

javascript - 如何在一个元素出现在屏幕上后立即更改它的 css 属性

html - 响应式图像对齐

html - 了解 html/body height 属性和粘性页脚(不固定)

html - 是否可以使用 css 将中间 div 包装到下一行