使用 HTML/CSS 使表格在移动设备上响应

2024-04-25

我有这张表,它是一封较大电子邮件的一部分。当用户的设备是手机或小屏幕时,我希望表格从 2 列/2 行变为 1 列 4 行。希望使用尽可能少的代码。

<table style="width:100%;table-layout:fixed;border-collapse:separate !important;border-spacing:20px 40px; font-family:Helvetica, Arial, sans-serif; color:#333333; font-size:16px; line-height:24px; font-weight:100;" class="bodywrapcenter">
  <tr>
    <td>
      Dr. Andrew Borg<br>
      Chapter Advisor<br>
      [email protected] /cdn-cgi/l/email-protection<br>
    </td>
    <td>
      Stuart J. Sigman, PhD<br>
      Dean, College of Arts and Sciences<br>
    </td>
  </tr>
  <tr>
    <td>
      Dr. Dale Mancini<br>
      Chapter Advisor<br>
      [email protected] /cdn-cgi/l/email-protection<br>
    </td>
    <td>
      David DeHaven, PhD<br>
      Dean, Graduate School of Business and Management<br>
    </td>
  </tr>
</table>

/*
    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 {
    margin: 0 0 1rem 0;
  }
  tr:nth-child(odd) {
    background: #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: 0;
    left: 6px;
    width: 45%;
    padding-right: 10px;
    white-space: nowrap;
  }
  /*
        Label the data
    You could also use a data-* attribute and content for this. That way "bloats" the HTML, this way means you need to keep HTML and CSS in sync. Lea Verou has a clever way to handle with text-shadow.
        */
  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: "Secret Alias";
  }
  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";
  }
}
<table role="table">
  <thead role="rowgroup">
    <tr role="row">
      <th role="columnheader">First Name</th>
      <th role="columnheader">Last Name</th>
      <th role="columnheader">Job Title</th>
      <th role="columnheader">Favorite Color</th>
      <th role="columnheader">Wars or Trek?</th>
      <th role="columnheader">Secret Alias</th>
      <th role="columnheader">Date of Birth</th>
      <th role="columnheader">Dream Vacation City</th>
      <th role="columnheader">GPA</th>
      <th role="columnheader">Arbitrary Data</th>
    </tr>
  </thead>
  <tbody role="rowgroup">
    <tr role="row">
      <td role="cell">James</td>
      <td role="cell">Matman</td>
      <td role="cell">Chief Sandwich Eater</td>
      <td role="cell">Lettuce Green</td>
      <td role="cell">Trek</td>
      <td role="cell">Digby Green</td>
      <td role="cell">January 13, 1979</td>
      <td role="cell">Gotham City</td>
      <td role="cell">3.1</td>
      <td role="cell">RBX-12</td>
    </tr>
    <tr role="row">
      <td role="cell">The</td>
      <td role="cell">Tick</td>
      <td role="cell">Crimefighter Sorta</td>
      <td role="cell">Blue</td>
      <td role="cell">Wars</td>
      <td role="cell">John Smith</td>
      <td role="cell">July 19, 1968</td>
      <td role="cell">Athens</td>
      <td role="cell">N/A</td>
      <td role="cell">Edlund, Ben (July 1996).</td>
    </tr>
    <tr role="row">
      <td role="cell">Jokey</td>
      <td role="cell">Smurf</td>
      <td role="cell">Giving Exploding Presents</td>
      <td role="cell">Smurflow</td>
      <td role="cell">Smurf</td>
      <td role="cell">Smurflane Smurfmutt</td>
      <td role="cell">Smurfuary Smurfteenth, 1945</td>
      <td role="cell">New Smurf City</td>
      <td role="cell">4.Smurf</td>
      <td role="cell">One</td>
    </tr>
    <tr role="row">
      <td role="cell">Cindy</td>
      <td role="cell">Beyler</td>
      <td role="cell">Sales Representative</td>
      <td role="cell">Red</td>
      <td role="cell">Wars</td>
      <td role="cell">Lori Quivey</td>
      <td role="cell">July 5, 1956</td>
      <td role="cell">Paris</td>
      <td role="cell">3.4</td>
      <td role="cell">3451</td>
    </tr>
    <tr role="row">
      <td role="cell">Captain</td>
      <td role="cell">Cool</td>
      <td role="cell">Tree Crusher</td>
      <td role="cell">Blue</td>
      <td role="cell">Wars</td>
      <td role="cell">Steve 42nd</td>
      <td role="cell">December 13, 1982</td>
      <td role="cell">Las Vegas</td>
      <td role="cell">1.9</td>
      <td role="cell">Under the couch</td>
    </tr>
  </tbody>
</table>

View :https://codepen.io/Waruna/pen/pmEJvg https://codepen.io/Waruna/pen/pmEJvg

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 HTML/CSS 使表格在移动设备上响应 的相关文章

  • jQuery 对表列重新排序

    我找到了适用于行的各种解决方案 以及适用于普通表列的各种解决方案 但我需要一个允许我拖放 colspan gt 1 的列标题的解决方案 例如 table thead tr th A1 th th A2 th th A4 th th A5 t
  • 使用CSS按比例调整图像大小? [复制]

    这个问题在这里已经有答案了 有没有办法仅使用 CSS 按比例调整 缩小 图像大小 我正在使用 JavaScript 方式 但只是想看看这是否可以用 CSS 实现 要使用 CSS 按比例调整图像大小 img resize width 540p
  • 搜索引擎可以读取 CSS 吗?

    我用标签来表示句子的重要性 然而 它破坏了页面风格的一致性 所以我用CSS把它改回来 结果是 对于访问者来说是相同的 但对于搜索引擎 SE 来说 显然是不同的 这正是SE们所烦恼的 所以我的问题是SE们能否读取CSS 并用它进一步判断整个页
  • 如何防止 HTML 文本孤儿?

    我经常在图像周围包裹文字 有时文字包裹起来很笨拙 如下所示 在 HTML 中 图像向左浮动 文本如下 p img src images image p p This is my David Copperfield em I was born
  • 引导导航栏后面的空间

    使用引导程序navbar 我试图弄清楚如何使其不隐藏主体部分的顶部 实际上 使用这里推荐的方法可以很好地解决这个问题 Twitter Bootstrap 顶部导航栏阻挡页面顶部内容 https stackoverflow com quest
  • 如何在 Mac 上的 Safari 中删除所选元素的光泽?

    在 Mac 和 iOS 设备上的 Safari 中
  • 使用 jQuery/JavaScript 提醒特殊字符

    如何在 Javascript jQuery 警报中显示带有特殊字符 例如 的字符串 例如 我想显示一个消息框 价格为 10 欧元 但是当我使用下面的代码时 alert The Price is euro 10 消息框中显示的输出是 The
  • 进行 URL 重写

    当我点击网站上给定条目的评论部分时 URL 如下所示 http www com comments index php submission Portugal 20Crushes 20North 20Korea submissionid 62
  • jQuery 函数在不应该运行的时候运行

    我有一个函数 仅当宽度小于特定值时才需要运行 我已经这样做了if window width lt n 但当宽度大于 n 时 该函数也会运行 The if if window width lt n 由于背景颜色发生变化 浅灰色 1000px
  • 如何从数组中删除空数组值(“”)?

    我有一个二维数组 是用 jQuery 从 html 表生成的 但有些值是空的 所以 被展示 如何删除空值 table tr th 1A th th 1B th th 1C th tr tr td 2A td td 2B td td 2C t
  • 如何在没有 Web 服务器的情况下运行 ajax 代码?

    我在系统上没有服务器的情况下运行ajax 我用它创建了一个index html JavaScript 函数 function do the click url alert inside this method do the click aj
  • 提交表单问题... Enter key

    我有一个包含几个下拉列表和一个文本字段以及一个按钮的表单 当我单击按钮时 将调用 ajax 函数 ajax 然后调用一个 php 函数 该函数从 mysql 数据库获取结果 问题是我不能通过在表单中 按回车键来完成同样的事情 页面只会刷新
  • Firefox 中的相对位置[重复]

    这个问题在这里已经有答案了 可能的重复 Firefox 是否支持表格元素上的position relative https stackoverflow com questions 5148041 does firefox support p
  • 使用 CSS 定位文本节点

    我正在研究容器对象的 CSS 我大部分时间都在工作 具体来说 我正在查看测试用例 1 2 和 3 它们都有文本节点 有没有办法像对待任何子元素一样对待文本节点 有什么方法可以调整 CSS 以使文本节点及其兄弟节点正常工作吗 除非有一个 CS
  • 如何在 jQuery 中获取会话中的值

    我是 jQuery 的初学者 我想在 HTML 页面中设置值 并且必须在另一个 HTML 页面中获取它们 这是我现在正在尝试的代码片段 要在会话中设置值 session set userName uname val 要从会话中获取值 ses
  • Jsoup遍历DOM树时节点哈希码冲突

    我正在使用 java jsoup 构建 HTML DOM 树 其中Node hashCode 用来 但我发现在遍历DOM树时存在很多哈希码冲突 使用以下代码 doc traverse new NodeVisitor Override pub
  • 在多个 html 文件上运行 javascript

    我有一个包含 1000 个 html 文件的文件夹 我必须使用 xpath 从每个 html 中删除某些节点 所以我已经制作了javascript 我无法打开每个文件并通过 Firefox 控制台运行 javascript 我用的是linu
  • 媒体查询在 VueJS 样式标签中不起作用

    我正在尝试在 VueJS 组件的样式标签中使用 media media 中的样式始终有效 而不是使用宽度规则
  • 仅当元素未分配类时,如何才能选择该元素?

    我正在修改现有 WordPress 主题的 CSS 主题有很多特殊样式的列表 附在 li 元素 正因为如此 有一个通用的list style none规则适用于 li li 元素 我想更新 CSS 以重新设置list style默认开启 l
  • 鼠标悬停在图像上显示 x

    我想在图像上显示 X 标记 尺寸为 24x24 为此我采用 li 元素和 in 元素 li style display inline block background 283038 border 1px solid 161b1f margi

随机推荐