博客列表 >html实战之表格表单

html实战之表格表单

写代码的张先森
写代码的张先森原创
2020年06月17日 16:24:37926浏览

一、HTML-结合css编写一个漂亮的表格


注:重点知识都在代码中标注出来了,记得看代码注释
代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>表格实战-购物车</title>
  7. <style>
  8. html {
  9. font-size: 14px;
  10. }
  11. table {
  12. /* 将单元格之间的间隙去掉 */
  13. border-collapse: collapse;
  14. width: 70%;
  15. /* 外边距左右自动居中 */
  16. margin: auto;
  17. color: #666;
  18. /* 字体粗细设置 */
  19. font-weight: lighter;
  20. /* 文本居中显示 */
  21. text-align: center;
  22. }
  23. /* 表格线直接添加到单元格上面 */
  24. table thead th,
  25. table td {
  26. border: 1px solid #8066a0;
  27. border-color: #8066a0;
  28. /* 设置内边距 */
  29. padding: 10px;
  30. }
  31. /* 标题样式 */
  32. table caption {
  33. font-size: 1.5rem;
  34. margin-bottom: 15px;
  35. color: green;
  36. }
  37. table th {
  38. font-weight: lighter;
  39. color: white;
  40. }
  41. table thead tr:first-of-type {
  42. background-color: rgb(157, 145, 172);
  43. }
  44. /* 隔行变色 */
  45. table tbody tr:nth-of-type(even) {
  46. background-color: rgb(78, 224, 243);
  47. }
  48. /* 鼠标悬停效果 */
  49. table tbody tr:hover {
  50. background-color: pink;
  51. color: white;
  52. }
  53. /* 底部样式 */
  54. table tfoot td {
  55. color: rgb(146, 5, 5);
  56. font-size: 1.2rem;
  57. font-weight: bolder;
  58. }
  59. /* 结算按钮 */
  60. body div:last-of-type {
  61. width: 70%;
  62. margin: 10px auto;
  63. }
  64. body div:first-of-type button {
  65. /* 靠右浮动 */
  66. float: right;
  67. width: 120px;
  68. height: 32px;
  69. background-color: seagreen;
  70. color: white;
  71. /* 去掉边框 */
  72. border: none;
  73. /* 设置鼠标样式 */
  74. cursor: pointer;
  75. }
  76. body div:first-of-type button:hover {
  77. background-color: coral;
  78. font-size: 1.1rem;
  79. }
  80. </style>
  81. </head>
  82. <body>
  83. <!-- 表格结构 -->
  84. <table>
  85. <!-- 标题 -->
  86. <caption>
  87. 购物车
  88. </caption>
  89. <!-- 表头 -->
  90. <thead>
  91. <tr>
  92. <th>ID</th>
  93. <th>品名</th>
  94. <th>单价/元</th>
  95. <th>单位</th>
  96. <th>数量</th>
  97. <th>金额/元</th>
  98. </tr>
  99. </thead>
  100. <!-- 主体 -->
  101. <tbody>
  102. <tr>
  103. <td>SN-1010</td>
  104. <td>MacBook Pro电脑</td>
  105. <td>18999</td>
  106. <td></td>
  107. <td>1</td>
  108. <td>18999</td>
  109. </tr>
  110. <tr>
  111. <td>SN-1020</td>
  112. <td>iPhone手机</td>
  113. <td>4999</td>
  114. <td></td>
  115. <td>2</td>
  116. <td>9998</td>
  117. </tr>
  118. <tr>
  119. <td>SN-1030</td>
  120. <td>智能AI音箱</td>
  121. <td>399</td>
  122. <td></td>
  123. <td>3</td>
  124. <td>1995</td>
  125. </tr>
  126. <tr>
  127. <td>SN-1040</td>
  128. <td>SSD移动硬盘</td>
  129. <td>888</td>
  130. <td></td>
  131. <td>2</td>
  132. <td>1776</td>
  133. </tr>
  134. <tr>
  135. <td>SN-1050</td>
  136. <td>黄山毛峰</td>
  137. <td>999</td>
  138. <td></td>
  139. <td>3</td>
  140. <td>2997</td>
  141. </tr>
  142. </tbody>
  143. <!-- 底部 -->
  144. <tfoot>
  145. <tr>
  146. <td colspan="4">总计:</td>
  147. <td>13</td>
  148. <td>35765</td>
  149. </tr>
  150. </tfoot>
  151. </table>
  152. <!-- 结算按钮 -->
  153. <div>
  154. <button>结算</button>
  155. </div>
  156. </body>
  157. </html>

运行效果:

二、HTML-结合css编写一个漂亮的表单


注:重点表单知识都在代码中标注出来了,记得看代码注释
代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>表单元素</title>
  7. <style>
  8. body {
  9. color: #555;
  10. }
  11. h3 {
  12. text-align: center;
  13. font-family: cursive;
  14. color: royalblue;
  15. }
  16. form {
  17. width: 450px;
  18. margin: 30px auto;
  19. border-top: 2px solid #aaa;
  20. }
  21. form fieldset {
  22. border: 1px solid rgb(70, 203, 212);
  23. background-color: lightcyan;
  24. box-shadow: 3px 3px 4px rgb(45, 211, 147);
  25. border-radius: 10px;
  26. margin: 20px;
  27. }
  28. form fieldset legend {
  29. background-color: rgb(178, 231, 201);
  30. border-radius: 10px;
  31. color: seagreen;
  32. padding: 5px 15px;
  33. }
  34. form div {
  35. margin: 5px;
  36. }
  37. form p {
  38. text-align: center;
  39. }
  40. form .btn {
  41. width: 80px;
  42. height: 30px;
  43. border: none;
  44. background-color: seagreen;
  45. color: #ddd;
  46. }
  47. form .btn:hover {
  48. background-color: coral;
  49. color: white;
  50. cursor: pointer;
  51. }
  52. input:focus {
  53. background-color: rgb(226, 226, 175);
  54. }
  55. .sty {
  56. width: 200px;
  57. height: 20px;
  58. border-radius: 3px;
  59. border: 0;
  60. padding-left: 4px;
  61. }
  62. </style>
  63. </head>
  64. <body>
  65. <h3>用户注册</h3>
  66. <form action="" method="POST">
  67. <!-- 控件组 -->
  68. <fieldset>
  69. <legend>基本信息(必填)</legend>
  70. <div>
  71. <label for="name">账号:</label>
  72. <input
  73. type="text"
  74. id="name"
  75. name="name"
  76. placeholder="6-15字符"
  77. autofocus
  78. required
  79. class="sty"
  80. />
  81. </div>
  82. <!-- type=text 文本 -->
  83. <div>
  84. <label for="email">邮箱:</label>
  85. <input
  86. type="email"
  87. id="email"
  88. name="email"
  89. placeholder="123@qq.com"
  90. required
  91. class="sty"
  92. />
  93. </div>
  94. <!-- type=email 输入的信息会进行是否是邮箱验证 -->
  95. <div>
  96. <label for="password">密码:</label>
  97. <input
  98. type="password"
  99. id="password"
  100. name="password"
  101. required
  102. placeholder="不少于六位且数字+字母"
  103. id="pwd"
  104. class="sty"
  105. />
  106. </div>
  107. <div>
  108. <label for="password2">确认:</label>
  109. <input
  110. type="password"
  111. id="password2"
  112. name="password2"
  113. required
  114. placeholder="与以上密码一致"
  115. class="sty"
  116. />
  117. </div>
  118. <!-- type=password 输入的信息是不显示的 安全 常用于输入密码 -->
  119. </fieldset>
  120. <fieldset>
  121. <legend>扩展信息(选填)</legend>
  122. <div>
  123. <label for="birthday">生日:</label>
  124. <input type="date" id="birthday" name="birthday" class="sty" />
  125. </div>
  126. <!-- type=date 选择时间 -->
  127. <!-- 单选按钮 name值必须一样-->
  128. <div>
  129. <label for="male">性别:</label>
  130. <input
  131. type="radio"
  132. id="male"
  133. value="male"
  134. name="male"
  135. checked
  136. /><label for="male"></label>
  137. <input type="radio" id="female" value="female" name="male" /><label
  138. for="female"
  139. ></label
  140. >
  141. </div>
  142. <!-- 复选框 name通常以array[]形式传给后台 便于后台处理-->
  143. <div>
  144. <label for="">爱好:</label>
  145. <input type="checkbox" name="hobby[]" value="game" /><label for=""
  146. >打游戏</label
  147. >
  148. <input type="checkbox" name="hobby[]" value="yinyue" /><label for=""
  149. >音乐</label
  150. >
  151. </div>
  152. <div>
  153. <!-- 选项列表 -->
  154. <label for="brand">手机:</label>
  155. <input
  156. type="search"
  157. list="phone"
  158. name="brand"
  159. id="brand"
  160. class="sty"
  161. />
  162. <datalist id="phone">
  163. <option value="apple">苹果</option>
  164. <option value="huawei">华为</option>
  165. <option value="mi">小米</option>
  166. </datalist>
  167. </div>
  168. </fieldset>
  169. <fieldset>
  170. <legend>其他信息(选填)</legend>
  171. <!-- 文件域 -->
  172. <div>
  173. <label for="pic">上传头像:</label>
  174. <input type="file" />
  175. </div>
  176. <!-- 多行文本框 -->
  177. <div>
  178. <label for="pic">简历:</label>
  179. <textarea
  180. name=""
  181. id=""
  182. cols="30"
  183. rows="5"
  184. placeholder="不超过100字"
  185. ></textarea>
  186. </div>
  187. </fieldset>
  188. <!-- 隐藏域, 例如用户的Id, 注册,登录的时间。。。。 -->
  189. <input type="hidden" name="user_id" value="123" />
  190. <p>
  191. <!-- 两种提交按钮 -->
  192. <input type="submit" value="提交" class="btn" />
  193. <button class="btn">提交</button>
  194. </p>
  195. </form>
  196. </body>
  197. </html>

运行效果:

小结:

1.理解表格的标题元素,表头,主体,底部

2.理解表单元素都有哪些,每个的写法和作用 如:文本框,密码框,邮箱验证,输入日期,单选按钮,复选框,选项列表,下拉列表,文件域,隐藏域,多行文本框

3.结合以上学习内容动手写两个表单,表格的demo更容易记忆,好记性不如烂笔头,动手实践才是真理

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议