软件测试Python圈Python基础

HTML基础10--表格的使用

2020-09-12  本文已影响0人  伊洛的小屋
HTML表格

在HTML中构建表格数据可以让web显示数据变得更容易,表格在实际的生活中是极其常见的直观的表达方式

创建一个表格
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link href="table.css" rel="stylesheet" type="text/css">
    <title>TABLE</title>
</head>
<body>
<table>
    <tr>
      <td>表格1</td>
      <td>表格2</td>
      <td>表格3</td>
      <td>表格4</td>
    </tr>
    <tr>
      <td>1</td>
      <td>2</td>
      <td>3</td>
      <td>4</td>
    </tr>
</table>
</body>
</html>

设置CSS表格样式

html {
  font-family: sans-serif;
}

table {
  border-collapse: collapse;
  border: 2px solid rgb(200,200,200);
  letter-spacing: 1px;
  font-size: 0.8rem;
}

td, th {
  border: 1px solid rgb(190,190,190);
  padding: 10px 20px;
}

th {
  background-color: rgb(235,235,235);
}

td {
  text-align: center;
}

tr:nth-child(even) td {
  background-color: rgb(250,250,250);
}

tr:nth-child(odd) td {
  background-color: rgb(245,245,245);
}

caption {
  padding: 10px;
}

打开浏览器


单元格跨越多行和列

需要用到colspan和rowspan两个属性

公众号:伊洛的小屋
个人主页:https://yiluotalk.com/
博客园:https://www.cnblogs.com/yiluotalk/
<html>
  <head>
    <meta charset="utf-8">
    <title>表格TITLE</title>
    <link href="table.css" rel="stylesheet" type="text/css">
  </head>
  <body>
    <h1>城市表格</h1>
    <table>
      <tr>
        <th colspan="2">北京</th>
      </tr>
      <tr>
        <th colspan="2">上海</th>
      </tr>
      <tr>
        <th rowspan="2">广东</th>
        <td>广州</td>
      </tr>
      <tr>
        <td>深圳</td>
      </tr>
      <tr>
        <th colspan="2">天津</th>
      </tr>
      <tr>
        <th rowspan="2">辽宁</th>
        <td>沈阳</td>
      </tr>
      <tr>
        <td>大连</td>
      </tr>
    </table>
  </body>
</html>

打开浏览器


共同的样式

HTML有一种方法可以定义整列数据的样式信息<col>,<colgroup>

为表格增加标题

通过<caption></caption>元素

<html>
  <head>
    <meta charset="utf-8">
    <title>表格TITLE</title>
    <link href="table.css" rel="stylesheet" type="text/css">
  </head>
  <body>
    <h1>城市表格</h1>
    <table>
        <caption>中国城市</caption>
      <tr>
        <th colspan="2">北京</th>
      </tr>
      <tr>
        <th colspan="2">上海</th>
      </tr>
      <tr>
        <th rowspan="2">广东</th>
        <td>广州</td>
      </tr>
      <tr>
        <td>深圳</td>
      </tr>
      <tr>
        <th colspan="2">天津</th>
      </tr>
      <tr>
        <th rowspan="2">辽宁</th>
        <td>沈阳</td>
      </tr>
      <tr>
        <td>大连</td>
      </tr>
    </table>
  </body>
</html>

标题放在<table>下面,打开浏览器查看效果


上一篇 下一篇

猜你喜欢

热点阅读