Blogger Information
Blog 37
fans 1
comment 0
visits 27025
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
CSS基础之表格控制、元素浮动定位、元素定位 九期第五课
叮叮当当
Original
1071 people have browsed it

1. 制作一张商品信息表,内容自定,要求用到行与列的合并

  1. <head>
  2. <meta charset="UTF-8">
  3. <title>商品信息表</title>
  4. <style>
  5. /*给单元格加边框*/
  6. td, th {
  7. border: 1px solid #444;
  8. text-align: center;
  9. /*每个单元格加内边距*/
  10. padding: 10px;
  11. }
  12. table {
  13. box-sizing: border-box;
  14. color: #444;
  15. font-size: 0.9rem;
  16. /*水平 垂直 扩散像素 颜色*/
  17. box-shadow: 1px 1px 1px #999;
  18. /*将表格中的边框进行折叠,相当于表格的cellspacing*/
  19. border-collapse:collapse ;
  20. width: 500px;
  21. margin: 20px auto;
  22. }
  23. table caption {
  24. font-size: 1.1rem;
  25. margin-bottom: 15px;
  26. }
  27. /*隔行变色*/
  28. tbody tr:nth-of-type(odd) {
  29. background-color: lightgray;
  30. }
  31. /*设置上午单元格的背景色*/
  32. tbody tr:first-of-type > td:first-of-type {
  33. background-color:lightcoral ;
  34. color: white;
  35. text-shadow: 1px 1px 1px black;
  36. }
  37. /*设置下午单元格的背景色*/
  38. tbody tr:nth-last-of-type(2) > td:first-of-type {
  39. background-color:lightsalmon;
  40. color: white;
  41. text-shadow: 1px 1px 1px black;
  42. }
  43. /*表头表尾样式*/
  44. table thead > tr:first-of-type, table tfoot >tr:last-of-type {
  45. background-color: lightseagreen;
  46. color: white;
  47. /*描边*/
  48. text-shadow: 1px 1px 1px black;
  49. }
  50. </style>
  51. </head>
  52. <body>
  53. <table>
  54. <caption>超市今日促销</caption>
  55. <thead>
  56. <tr>
  57. <th>商品分类</th>
  58. <th>果蔬</th>
  59. <th>肉鱼</th>
  60. <th>粮油</th>
  61. <th>乳饮</th>
  62. <th>零食</th>
  63. <th>百货</th>
  64. </tr>
  65. </thead>
  66. <tbody>
  67. <tr>
  68. <td rowspan="2">早晨<br>7-9点</td>
  69. <td>火龙果</td>
  70. <td>乌鸡</td>
  71. <td>大米</td>
  72. <td>老酸奶</td>
  73. <td>小面包</td>
  74. <td>不粘锅</td>
  75. </tr>
  76. <tr>
  77. <td>大青芒</td>
  78. <td>黑虎虾</td>
  79. <td>葵花油</td>
  80. <td>红参茶</td>
  81. <td>海苔</td>
  82. <td>洗衣液</td>
  83. </tr>
  84. <tr>
  85. <td rowspan="2">下午<br>4-5点</td>
  86. <td>猕猴桃</td>
  87. <td>鸭腿</td>
  88. <td>红豆</td>
  89. <td>椰汁</td>
  90. <td>酥饼</td>
  91. <td>***桌</td>
  92. </tr>
  93. <tr>
  94. <td>雪莲果</td>
  95. <td>三文鱼</td>
  96. <td>薏仁</td>
  97. <td>纯牛奶</td>
  98. <td>锅巴</td>
  99. <td>抽纸</td>
  100. </tr>
  101. </tbody>
  102. <tfoot>
  103. <tr>
  104. <td>备注:</td>
  105. <td colspan="6">每种促销商品每人限购一份</td>
  106. </tr>
  107. </tfoot>
  108. </table>
  109. </body>

2. 使用<div><span><p><ul>…等标签来制作一张课程表

  1. <head>
  2. <meta charset="UTF-8">
  3. <title>课程表</title>
  4. <style>
  5. .table {
  6. display: table;
  7. box-sizing: border-box;
  8. /*边框折叠*/
  9. border-collapse: collapse;
  10. border: 1px solid #444;
  11. box-shadow: 1px 1px 1px #999;
  12. width: 500px;
  13. margin: auto;
  14. color: #444;
  15. text-align:center;
  16. }
  17. .caption {
  18. display: table-caption;
  19. }
  20. .thead {
  21. display: table-header-group;
  22. font-weight: bold;
  23. /*字间距*/
  24. letter-spacing: 4px;
  25. }
  26. .thead, .tfoot {
  27. background-color: lightseagreen;
  28. color: white;
  29. text-shadow: 1px 1px 1px black;
  30. }
  31. .tbody {
  32. display: table-row-group;
  33. }
  34. span ul {
  35. display: table-row;
  36. }
  37. span ul li {
  38. display: table-cell;
  39. border: 1px solid #444;
  40. padding: 4px;
  41. }
  42. span ul li p {
  43. color: lightseagreen;
  44. font-weight: bold;
  45. }
  46. .tfoot {
  47. display: table-footer-group;
  48. }
  49. </style>
  50. </head>
  51. <body>
  52. <div class="table">
  53. <h2 class="caption">课程安排</h2>
  54. <span class="thead">
  55. <ul>
  56. <li>序号</li>
  57. <li>课程</li>
  58. <li>描述</li>
  59. </ul>
  60. </span>
  61. <span class="tbody">
  62. <ul>
  63. <li>1</li>
  64. <li>英语结构</li>
  65. <li><p>英语常用结构语法讲解</p></li>
  66. </ul>
  67. <ul>
  68. <li>2</li>
  69. <li>英语阅读</li>
  70. <li><p>大量英语文章阅读练习分析</p></li>
  71. </ul>
  72. <ul>
  73. <li>3</li>
  74. <li>英语口语</li>
  75. <li><p>小电影带你进入口语的世界</p></li>
  76. </ul>
  77. </span>
  78. <span class="tfoot">
  79. <ul>
  80. <li>备注:</li>
  81. <li>全程直播互动教学</li>
  82. <li>每周六12:00 - 15:00,我们不见不散</li>
  83. </ul>
  84. </span>
  85. </div>
  86. </body>

3. 使用绝对定位,实现用户登录框在页面中始终居中显示

  1. <head>
  2. <meta charset="UTF-8">
  3. <title>绝对定位</title>
  4. <style>
  5. body {
  6. /*定位父级 / 定位上下文到<body>上*/
  7. position: relative;
  8. height: 500px;
  9. }
  10. .box {
  11. box-sizing: border-box;
  12. margin:40px 0;
  13. width: 60%;
  14. height: 280px;
  15. /*圆角*/
  16. border-radius: 8px 8px 8px 8px;
  17. background-color: lightblue;
  18. text-align: center;
  19. position: absolute;
  20. left: 20%;
  21. }
  22. .box h3 {
  23. font-size: 1.2rem;
  24. color:white;
  25. text-shadow: 1px 1px 1px black;
  26. }
  27. .box1 {
  28. box-sizing: border-box;
  29. width: 250px;
  30. /*由于父元素设置了宽高,以及绝对定位,并没有出现传递问题*/
  31. margin: auto ;
  32. padding: 10px;
  33. background-color: lightcyan;
  34. border-radius: 8px 8px 8px 8px;
  35. }
  36. </style>
  37. </head>
  38. <body>
  39. <div class="box">
  40. <h3>用户登录</h3>
  41. <div class="box1">
  42. <form action="" method="post">
  43. <p>
  44. <label for="username">用户:</label>
  45. <input type="text" name="username" id="username" value="" >
  46. </p>
  47. <p>
  48. <label for="psd">密码:</label>
  49. <input type="password" name="psd" id="psd" placeholder="请输密码">
  50. </p>
  51. <p>
  52. <input type="submit" name="submit" value="提交">
  53. </p>
  54. </form>
  55. </div>
  56. </div>
  57. </body>

4.模仿课堂案例, 实现圣杯布局,并写出完整流程与布局思路

  1. <head>
  2. <meta charset="UTF-8">
  3. <title>圣杯布局</title>
  4. <style>
  5. body {
  6. text-align: center;
  7. }
  8. header, footer {
  9. height: 60px;
  10. background-color: lightyellow;
  11. /*与高度值一致,垂直居中*/
  12. line-height: 30px;
  13. }
  14. /*主体*/
  15. main {
  16. box-sizing: border-box;
  17. /*给左右边栏预留空间*/
  18. padding-left: 100px;
  19. padding-right: 100px;
  20. /*将main转为BFC块, 使浮动元素包含在内, 撑开父级*/
  21. overflow: hidden;
  22. }
  23. main > article {
  24. box-sizing: border-box;
  25. background-color: lavender;
  26. /*占据父容器全部空间*/
  27. width: 100%;
  28. /*演示案例,使用最小高度弥补内容不足问题*/
  29. min-height: 100px;
  30. line-height: 100px;
  31. }
  32. /*左右侧通用样式*/
  33. main > aside {
  34. box-sizing: border-box;
  35. width: 100px;
  36. min-height: 100px;
  37. line-height: 100px;
  38. }
  39. main > aside:first-of-type {
  40. background-color: lightcyan;
  41. }
  42. main > aside:last-of-type {
  43. background-color: lightpink;
  44. }
  45. /*主体区内容全部浮动*/
  46. main > article,
  47. main > aside:first-of-type,
  48. main > aside:last-of-type {
  49. float: left;
  50. }
  51. /*将左侧拉到主体的左边*/
  52. main > aside:first-of-type {
  53. /*从当前位置从左向右推-100%,即从右向左推100%(一个父元素宽度)*/
  54. margin-left: -100%;
  55. /*相对定位*/
  56. position: relative;
  57. /*在margin-left位置从左向右推-100,即从右向左推100 */
  58. left: -100px;
  59. }
  60. main > aside:last-of-type {
  61. margin-right: -100px;
  62. }
  63. </style>
  64. </head>
  65. <body>
  66. <header>头部</header>
  67. <main>
  68. <article>中间</article>
  69. <aside>左侧</aside>
  70. <aside>右侧</aside>
  71. </main>
  72. <footer>底部</footer>
  73. </body>

思路:

  • 整个布局的DOM结构:头部,底部,主体,其中主体包括左中右三栏;
  • (1)将主体转为BFC块, 使浮动元素能够包含在内, 撑开父级overflow: hidden;;

  • (2)给主体左右边栏各预留空间100px,以便后面主体的左右侧区块浮动;
    padding-left: 100px;padding-right: 100px;

  • (3)将主体中间区块的宽度设置100%,占据父容器全部空间;

  • (4)将主体左右区块宽度各设为100px,以便后面浮动后宽度正好合适;

  • (5)主体区内容全部浮动 float: left;

  • (ps: 由于中间区宽度100%,所以左右侧被挤到下一行了)

  • (6)将左侧拉到主体的左边margin-left: -100%;

  • (7)将左侧从当前位置拉到左侧预留空间;
    position: relative;left: -100px;

  • (8)将右侧拉到右侧预留空间margin-right: -100px;

5. (选做): 不使用<table>...写表格时,如何实现行与列合并

  1. <head>
  2. <meta charset="UTF-8">
  3. <title>Title</title>
  4. <style>
  5. ul{
  6. list-style: none;
  7. padding: 0;
  8. }
  9. .table {
  10. box-sizing: border-box;
  11. width: 505px;
  12. margin: auto;
  13. text-align: center;
  14. }
  15. .caption{
  16. margin: 20px 0 0 0;
  17. color: #333333;
  18. }
  19. .box{
  20. box-sizing: border-box;
  21. width: 100%;
  22. margin: 0;
  23. }
  24. .box ul{
  25. float: left;
  26. }
  27. .box li {
  28. width: 100px;
  29. height: 30px;
  30. float: left;
  31. line-height: 30px;
  32. border: 1px solid black;
  33. /*margin负值去除重叠边框*/
  34. margin: 0 -1px -1px 0;
  35. }
  36. .box ul .rowspan1 {
  37. height: 61px;
  38. line-height: 61px;
  39. }
  40. .box ul .tbody2 {
  41. position: relative;
  42. top: -31px;
  43. }
  44. .box ul .tbody3 {
  45. position: relative;
  46. left: -303px;
  47. }
  48. .box ul .tbody3-rest {
  49. position: relative;
  50. left: 202px;
  51. top: -31px;
  52. }
  53. .box ul .tfoot {
  54. position: relative;
  55. top: -31px;
  56. }
  57. .thead, .tfoot {
  58. background-color: lightseagreen;
  59. color: white;
  60. text-shadow: 1px 1px 1px black;
  61. }
  62. </style>
  63. </head>
  64. <body>
  65. <div class="table">
  66. <h2 class="caption">商品信息表</h2>
  67. <div class="box">
  68. <ul>
  69. <li class="thead">编号</li>
  70. <li class="thead">名称</li>
  71. <li class="thead">单价(元)</li>
  72. <li class="thead">数量</li>
  73. <li class="thead">金额(元)</li>
  74. <li>1</li>
  75. <li>铅笔</li>
  76. <li>1</li>
  77. <li class="rowspan1">3</li>
  78. <li class="rowspan1">3</li>
  79. <li class="tbody2">2</li>
  80. <li class="tbody2">橡皮</li>
  81. <li class="tbody2">1</li>
  82. <li class="tbody3">3</li>
  83. <li class="tbody3">文具盒</li>
  84. <li class="tbody3-rest">10</li>
  85. <li class="tbody3-rest">1</li>
  86. <li class="tbody3-rest">10</li>
  87. <li class="tfoot" style="width: 302px;">合计:</li>
  88. <li class="tfoot">4</li>
  89. <li class="tfoot">13</li>
  90. </ul>
  91. </div>
  92. </div>
  93. </body>

6.(选做): 将圣杯布局中的左右二列,使用绝对定位来实现

  1. <head>
  2. <meta charset="UTF-8">
  3. <title>圣杯布局左右二列:绝对定位</title>
  4. <style>
  5. body {
  6. text-align: center;
  7. }
  8. header, footer {
  9. height: 30px;
  10. background-color: lightyellow;
  11. line-height: 30px;
  12. }
  13. main {
  14. box-sizing: border-box;
  15. /*给左右边栏预留空间*/
  16. padding-left: 100px;
  17. padding-right: 100px;
  18. overflow: auto;
  19. /*子元素有绝对定位时,祖先元素需设置定位属性(除static定位)*/
  20. /*最靠近的第一个祖先元素就是该子元素的参照物*/
  21. position: relative;
  22. }
  23. main > article {
  24. box-sizing: border-box;
  25. background-color: lavender;
  26. width: 100%;
  27. min-height: 100px;
  28. line-height: 100px;
  29. }
  30. /*左右侧通用样式*/
  31. main > aside {
  32. box-sizing: border-box;
  33. width: 100px;
  34. min-height: 100px;
  35. line-height: 100px;
  36. }
  37. main > aside:first-of-type {
  38. background-color: lightcyan;
  39. }
  40. main > aside:last-of-type {
  41. background-color: lightpink;
  42. }
  43. /*主体区内容全部浮动*/
  44. article, aside {
  45. float: left;
  46. }
  47. /*绝对定位*/
  48. main > aside:first-of-type {
  49. position: absolute;
  50. left: 0;
  51. }
  52. main > aside:last-of-type {
  53. position: absolute;
  54. right: 0;
  55. }
  56. </style>
  57. </head>
  58. <body>
  59. <header>头部</header>
  60. <main>
  61. <article>主体内容区</article>
  62. <aside>左侧</aside>
  63. <aside>右侧</aside>
  64. </main>
  65. <footer>底部</footer>
  66. </body>

7.(选做): 与圣杯类似的”双飞翼”布局如何实现,并实例演示

  1. <head>
  2. <meta charset="UTF-8">
  3. <title>双飞翼</title>
  4. <style>
  5. header, footer {
  6. height: 30px;
  7. background-color: lightyellow;
  8. text-align: center;
  9. /*与高度值一致,垂直居中*/
  10. line-height: 30px;
  11. }
  12. /*主体*/
  13. main {
  14. box-sizing: border-box;
  15. overflow: hidden;
  16. }
  17. main > article {
  18. box-sizing: border-box;
  19. width: 100%;
  20. background-color: lavender;
  21. min-height: 100px;
  22. line-height: 100px;
  23. }
  24. main > aside {
  25. box-sizing: border-box;
  26. width: 100px;
  27. min-height: 100px;
  28. line-height: 100px;
  29. }
  30. /*主体区内容全部浮动*/
  31. article, aside {
  32. float: left;
  33. }
  34. main > aside:first-of-type {
  35. background-color: lightcyan;
  36. margin-left: -100%;
  37. }
  38. main > aside:last-of-type {
  39. background-color: lightpink;
  40. margin-left: -100px;
  41. }
  42. /*设置左右外边框,使其不被左右侧区块盖住*/
  43. .center {
  44. margin: 0 100px;
  45. }
  46. </style>
  47. </head>
  48. <body>
  49. <header>头部</header>
  50. <main>
  51. <article><div class="center">内容区</div></article>
  52. <aside>左侧</aside>
  53. <aside>右侧</aside>
  54. </main>
  55. <footer>底部</footer>
  56. </body>

作业手抄


总结:CSS完成表格,圣杯布局的方法很多样,很灵活,条条大陆通罗马。

Correction status:qualified

Teacher's comments:你的进度有点慢
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post