html - 如何使用CSS设计表格网格?

标签 html css

我想创建一个表格网格之类的图像,如下所示: enter image description here

我也创建了一个表,但它看起来不像我们图片中的那样。这是代码

<table border="2" cellpadding="1">
  <tr border="1">
    <td>
      <p id="p_tit" class="p_tit">=====================================Q-05========================================</p>
    </td>
  </tr>
</table>
<table border="2" cellpadding="1">
  <tr border="1">
    <td colspan="2">
      <p id="p_tit3">Question</p>
      <p>1604100016</p>
    </td>
    <td colspan="2">
      <p id="p_tit3">
        A train 125 m long passes a man, running at 5 km/hr in the same direction in which the train is going, in 10 seconds. The speed of the train is:
      </p>
    </td>
  </tr>
  <tr border="1">
    <td colspan="2">
      <p id="p_tit3">Options</p>
    </td>
    <td colspan="2">
      <table align="center">
        <tr>
          <td id="opt">45 km/h</td>
          <td id="opt">50 km/h</td>
        </tr>
        <tr>
          <td id="opt">40 km/h</td>
          <td id="opt">55 km/h</td>
        </tr>
      </table>
    </td>
  </tr>
  <tr border="1">
    <td colspan="2">
      <p id="p_tit3">Explanation</p>
    </td>
    <td colspan="2">
      <p>
        Explanation
      </p>
    </td>
  </tr>
</table>

它看起来像 enter image description here

那么如何创建如图 1 所示的表格?请帮助我。

谢谢

最佳答案

你想要的是属性rowspan。我在表格的第一行添加了(并给它 20% 的宽度,它不会折叠):

    <td rowspan="3" style="width: 20%;">          
    </td>

全表代码:

<table border="2" cellpadding="1">
    <tr border="1">
        <td colspan="2">
            <p id="p_tit3">Question</p>
            <p>1604100016</p>
        </td>
        <td colspan="2">
            <p id="p_tit3"> 
                A train 125 m long passes a man, running at 5 km/hr in the same direction in which the train is going, in 10 seconds. The speed of the train is:
            </p>
        </td>
        <td rowspan="3" style="width: 20%;">          
        </td>
    </tr>
    <tr border="1">
        <td colspan="2">
            <p id="p_tit3">Options</p>
        </td>
        <td colspan="2">
            <table align="center">
                <tr>
                    <td id="opt">45 km/h</td>
                    <td id="opt">50 km/h</td>
                </tr>
                <tr>
                    <td id="opt">40 km/h</td>
                    <td id="opt">55 km/h</td>
                </tr>
            </table>
        </td>
    </tr>
    <tr border="1">
        <td colspan="2">
            <p id="p_tit3">Explanation</p>
        </td>
        <td colspan="2">
            <p>
                Explanation
            </p>
        </td>
    </tr>
</table>

这是一个 fiddle :https://jsfiddle.net/72htupn4/

这是一些关于 rowspan 的文档:https://www.w3schools.com/tags/att_td_rowspan.asp

关于html - 如何使用CSS设计表格网格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48261833/

相关文章:

javascript - 显示 :none 的代码执行 html/脚本

html - 我希望这些图像垂直对齐,我该如何实现?

jquery - 用图像映射显示 Div?

css - Flexbox 容器不包括 margin-bottom

android - 移动设备上显示的网页发生了变化和压缩(与桌面浏览器的区别)

jquery - 将滑动切换的元素保持在顶部

html - 图像在 IE 上不重叠

javascript - 如何使用 Javascript 使用 Google Drive API 进行搜索?

javascript - 如何使用 jQuery 创建多个 HTML 元素?

html - 如何在CSS中使用 Sprite 图像?