Blogger Information
Blog 9
fans 0
comment 7
visits 6018
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Chapter7 布局原理与实战
无关
Original
634 people have browsed it

一、 浮动元素的塌陷和解决方案

1.效果图

1.1 元素浮动前:

1.2 浮动元素的塌陷图:

1.3 塌陷解决后,恢复原图:

2.源码

  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. .container{
  9. border:3px dashed blue;
  10. background-color: lightseagreen;
  11. }
  12. .item{
  13. width: 150px;
  14. height: 150px;
  15. }
  16. .item:first-of-type{
  17. background-color:lightpink ;
  18. }
  19. .item:last-of-type{
  20. background-color:lightsalmon;
  21. }
  22. .item:nth-of-type(2){
  23. background-color:lightgreen ;
  24. }
  25. /* 当三个子元素全部浮动之后,容器container没有子元素的支持,高度塌陷, */
  26. .item{
  27. float:left;
  28. }
  29. /* 塌陷解决方案 */
  30. /* 解决方案1. 添加一个伪元素解决-可以用,但啰嗦*/
  31. /* .container::after{
  32. content: "";
  33. display: block;
  34. clear:both;
  35. } */
  36. /* overflow:最简单的解决方案*/
  37. .container{
  38. /* overflow: hidden=overflow: auto; */
  39. /* overflow: hidden; */
  40. overflow: auto;
  41. }
  42. </style>
  43. </head>
  44. <body>
  45. <div class="container">
  46. <div class="item">浮动元素1</div>
  47. <div class="item">浮动元素2</div>
  48. <div class="item">浮动元素3</div>
  49. <!-- <div class="clear"></div> -->
  50. </div>
  51. </body>
  52. </html>

二、中国诗词大会:绝对定位

1.效果图

2.源码

  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. /* 初始化 */
  9. * {
  10. margin:0;
  11. padding:0;
  12. box-sizing: border-box;
  13. color:red;
  14. font-size: 1.2rem;
  15. }
  16. /* 页眉样式 与内容 */
  17. .header{
  18. width: 1060px;
  19. height: 500px;
  20. margin:auto;
  21. background:url('header.png') no-repeat center;
  22. }
  23. /* 页脚样式 与内容*/
  24. .footer{
  25. width: 1060px;
  26. height: 130px;
  27. margin:auto;
  28. background:lightgreen url('footer.png') no-repeat center ;
  29. }
  30. /* 主体样式 */
  31. .container{
  32. width: 1060px;
  33. margin: 10px auto;
  34. background-color: #ccc;
  35. min-height:200px;
  36. /* 转为定位元素,作为定位父级*/
  37. position: relative;
  38. }
  39. .container>.left{
  40. width:85px;
  41. min-height: 200px;
  42. background-color: wheat;
  43. background-image:url('left.png') ;
  44. background-repeat:no-repeat;
  45. /* 左绝对定位 */
  46. position: absolute;
  47. top: 0;
  48. left:0;
  49. }
  50. .container>.right{
  51. width:100px;
  52. min-height: 200px;
  53. background-color: wheat;
  54. background-image:url('right.png') ;
  55. background-repeat:no-repeat;
  56. /* 右绝对定位 */
  57. position: absolute;
  58. top: 0;
  59. right:0;
  60. }
  61. .container>.main{
  62. width: 860px;
  63. background-color: limegreen;
  64. min-height: 200px;
  65. background:url('main.png') no-repeat ;
  66. /* 主体绝对定位 */
  67. position: absolute;
  68. left:90px;
  69. top:0;
  70. }
  71. </style>
  72. </head>
  73. <body>
  74. <!-- 页眉 -->
  75. <div class="header">页眉</div>
  76. </div>
  77. <!-- 主体 -->
  78. <div class="container">
  79. <div class="left">左侧</div>
  80. <div class="main">主体</div>
  81. <div class="right">右侧</div>
  82. </div>
  83. <!-- 页脚 -->
  84. <div class="footer">页脚</div>
  85. </body>
  86. </html>
  87. }
  88. .container>.right{
  89. width:100px;
  90. min-height: 200px;
  91. background-color: wheat;
  92. background-image:url('right.png') ;
  93. background-repeat:no-repeat;
  94. /* 右绝对定位 */
  95. position: absolute;
  96. top: 0;
  97. right:0;
  98. }
  99. .container>.main{
  100. width: 860px;
  101. background-color: limegreen;
  102. min-height: 200px;
  103. background:url('main.png') no-repeat ;
  104. /* 主体绝对定位 */
  105. position: absolute;
  106. left:90px;
  107. top:0;
  108. }
  109. </style>

三、中国诗词大会:浮动定位

1.效果图

2.源码

  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. /* 初始化 */
  9. * {
  10. margin:0;
  11. padding:0;
  12. box-sizing: border-box;
  13. color:red;
  14. font-size: 1.2rem;
  15. }
  16. /* 页眉样式 与内容 */
  17. .header{
  18. width: 1060px;
  19. height: 500px;
  20. margin:auto;
  21. background:url('header.png') no-repeat center;
  22. }
  23. /* 页脚样式 与内容*/
  24. .footer{
  25. width: 1060px;
  26. height: 130px;
  27. margin:auto;
  28. background:lightgreen url('footer.png') no-repeat center ;
  29. }
  30. /* 主体样式 */
  31. .container{
  32. width: 1060px;
  33. margin: 10px auto;
  34. background-color: #ccc;
  35. min-height:200px;
  36. /* 转为定位元素,作为定位父级*/
  37. position: relative;
  38. }
  39. .container>.left{
  40. width:85px;
  41. min-height: 200px;
  42. /* 左浮动 */
  43. float: left;
  44. background: wheat url('left.png') no-repeat;
  45. }
  46. .container>.right{
  47. width:100px;
  48. min-height: 200px;
  49. /* 右浮动 */
  50. float: right;
  51. background: wheat url('right.png') no-repeat;
  52. }
  53. .container>.main{
  54. width: 800px;
  55. min-height: 200px;
  56. margin-left:40px;
  57. background:limegreen url('main.png') no-repeat ;
  58. /* 左浮动 */
  59. float: left;
  60. }
  61. </style>
  62. </head>
  63. <body>
  64. <!-- 页眉 -->
  65. <div class="header">页眉</div>
  66. </div>
  67. <!-- 主体 -->
  68. <div class="container">
  69. <div class="left">左侧</div>
  70. <div class="main">主体</div>
  71. <div class="right">右侧</div>
  72. </div>
  73. <!-- 页脚 -->
  74. <div class="footer">页脚</div>
  75. </body>
  76. </html>
Correcting teacher:GuanhuiGuanhui

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
1 comments
2020-07-01 16:34:43
为了体现背景色,页面故意留出来了空隙,看上去不太美观哈。 谢谢老师!
1 floor
Author's latest blog post