博客列表 >《浮动定位的理解应用》20201221

《浮动定位的理解应用》20201221

杨凡的博客
杨凡的博客原创
2020年12月22日 13:50:371067浏览

浮动

  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. }
  14. .box{
  15. padding: 1em;
  16. border: 1px solid #000;
  17. }
  18. .box img{
  19. width: 15em;
  20. /* 图片圆角 */
  21. border-radius: 0.5em;
  22. /* 图片向左浮动 */
  23. float: left;
  24. /* 添加右边的外边距 */
  25. margin-right: 1em;
  26. }
  27. /* 通过清除浮动来处理图片脱离box的范围,父元素塌陷的问题 */
  28. /* 附加元素 */
  29. /* .clear{
  30. clear: left;
  31. } */
  32. /* 伪元素 */
  33. .box::after{
  34. content: '';
  35. display: block;
  36. /* 清除左右浮动 */
  37. clear: both;
  38. }
  39. /* 创建BFC */
  40. .box .desc{
  41. overflow: hidden;
  42. }
  43. </style>
  44. </head>
  45. <body>
  46. <h1>浮动</h1>
  47. <div class="box">
  48. <img src="https://img.php.cn/upload/course/000/000/001/5fae4f08a043a576.png" alt="">
  49. <div class="desc">
  50. <h2>第十四期_前端开发</h2>
  51. <p>php中文网第十四期前端开发部分学习目标:1、HTML5+CSS3基础知识与实战; 2、完成网站所有静态页面布局;重点:弹性布局与网格布局;3.JavaScript、jQuery、ES6基础与实战 ;4.Vue.js基础与实战</p>
  52. </div>
  53. <!-- <div class="clear"></div> -->
  54. </div>
  55. </body>
  56. </html>

创建BFC的方式: 任何一个元素添加上以下任何一个属性后就是是一个BFC容器

  1. 1. float: left / right, 不能是 none
  2. 2. overflow: hidden / auto / scroll , 不能是 visible
  3. 3. display: inline-block / table-cell
  4. 4. display: flex / inline-flex
  5. 5. display: grid / inline-grid
  6. 6. position: absolute / fiexed

总结

  1. 浮动仅限于水平方向
  2. 浮动只会影响后面的元素不会影响上级元素
  3. 浮动的元素是脱离文档流的,后面的元素会上移填充它原来的空间
  4. 设置浮动后的任何元素都会具有块级元素效果
  5. 通过清除浮动可以处理父元素塌陷的问题
  6. BFC的方式也可以解决父元素塌陷的问题

定位

position属性

  1. 静态定位static,文档流默认定位
  2. 相对定位relative,相对于该元素在文档流中的原始位置进行偏移
  3. 绝对定位absolute,相对于他的父级元素中离它最近的定位元素的位置发生便宜
  4. 固定定位fixed,是绝对定位的一个特例,始终相对于html定位
  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. }
  14. .box{
  15. position: relative;
  16. }
  17. .box1{
  18. position: absolute;
  19. height: 2em;
  20. width: 20em;
  21. background-color: green;
  22. top: 20em;
  23. left: 10em;
  24. }
  25. .box2{
  26. position: absolute;
  27. height: 20em;
  28. width: 2em;
  29. background-color: red;
  30. top: 11em;
  31. left: 19em;
  32. }
  33. .ring{
  34. position: relative;
  35. top: 5em;
  36. left: 30em;
  37. }
  38. .ring :nth-of-type(1){
  39. width: 15em;
  40. height: 15em;
  41. border:1.5em solid #2ba7da;
  42. border-radius: 50%;
  43. position: absolute;
  44. }
  45. .ring :nth-of-type(2){
  46. position: absolute;
  47. width: 15em;
  48. height: 15em;
  49. border:1.5em solid #000000;
  50. border-radius: 50%;
  51. left: 16em;
  52. }
  53. .ring :nth-of-type(3){
  54. position: absolute;
  55. width: 15em;
  56. height: 15em;
  57. border:1.5em solid #fa0300;
  58. border-radius: 50%;
  59. left: 32em;
  60. }
  61. .ring :nth-of-type(4){
  62. position: absolute;
  63. width: 15em;
  64. height: 15em;
  65. border:1.5em solid #fced1e;
  66. border-radius: 50%;
  67. left: 8em;
  68. top: 8em;
  69. }
  70. .ring :nth-of-type(5){
  71. position: absolute;
  72. width: 15em;
  73. height: 15em;
  74. border:1.5em solid #3cbc4d;
  75. border-radius: 50%;
  76. left: 24em;
  77. top: 8em;
  78. }
  79. </style>
  80. </head>
  81. <body>
  82. <div class="box">
  83. <div class="box1"></div>
  84. <div class="box2"></div>
  85. </div>
  86. <div class="ring">
  87. <div></div>
  88. <div></div>
  89. <div></div>
  90. <div></div>
  91. <div></div>
  92. </div>
  93. </body>
  94. </html>

模态框使用

  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. }
  14. /* 页眉 */
  15. header{
  16. background-color: #ccc;
  17. padding: .5em 1em;
  18. /* BFC 清除浮动 */
  19. overflow: hidden;
  20. }
  21. header h2{
  22. /* 左浮动 */
  23. float: left;
  24. }
  25. header button {
  26. /* 右浮动 */
  27. float: right;
  28. width: 5em;
  29. height: 2.5em;
  30. }
  31. header button:hover{
  32. cursor: pointer;
  33. }
  34. /* 遮罩层 */
  35. .modal .modal-backdrop {
  36. background-color: rgb(0,0,0,0.5);
  37. position: fixed;
  38. top:0;
  39. left:0;
  40. right:0;
  41. bottom: 0;
  42. }
  43. .modal .modal-body {
  44. background-color: #fff;
  45. padding: 1em;
  46. overflow: hidden;
  47. max-width: 20em;
  48. max-height: 20em;
  49. /* 固定定位 */
  50. position: fixed;
  51. /* 水平垂直自动居中 */
  52. top: 0;
  53. left: 0;
  54. right: 0;
  55. bottom: 0;
  56. margin: auto;
  57. }
  58. .modal form table {
  59. width: 100%;
  60. }
  61. .modal form table caption {
  62. font-size: 14px;
  63. font-weight: bold;
  64. margin-bottom:1.6em;
  65. margin-right:1em;
  66. }
  67. .modal form table td {
  68. padding: 0.5em 0;
  69. }
  70. .modal form table td:first-of-type {
  71. width: 4em;
  72. }
  73. .modal form table input {
  74. width: 12em;
  75. height: 2em;
  76. }
  77. .modal form table button {
  78. width:8em;
  79. height: 2em;
  80. }
  81. /* 定位父级 */
  82. .modal-body {
  83. position: relative;
  84. }
  85. .modal .close {
  86. position: absolute;
  87. width: 3em;
  88. height: 1.5em;
  89. top: 1.1em;
  90. left: 1em;
  91. }
  92. .modal .close:hover {
  93. cursor: pointer;
  94. background-color: red;
  95. color: white;
  96. }
  97. /* 模态框初始化隐藏 */
  98. .modal {
  99. display: none;
  100. }
  101. </style>
  102. </head>
  103. <body>
  104. <!-- 页眉 -->
  105. <header>
  106. <h2>百万弟弟的博客</h2>
  107. <button>登录</button>
  108. </header>
  109. <!-- 模态框 -->
  110. <div class="modal">
  111. <!-- 蒙版盖住后面的内容,使他半透明 -->
  112. <div class="modal-backdrop"></div>
  113. <!-- 主体 -->
  114. <div class="modal-body">
  115. <button class="close">关闭</button>
  116. <form action="" method="POST">
  117. <table>
  118. <caption>用户登录</caption>
  119. <tr>
  120. <td><label for="username">用户名:</label></td>
  121. <td><input type="username" name="username" id="username" /></td>
  122. </tr>
  123. <tr>
  124. <td><label for="password">密&nbsp;&nbsp;&nbsp;&nbsp;码:</label></td>
  125. <td><input type="password" name="password" id="password" /></td>
  126. </tr>
  127. <tr>
  128. <td></td>
  129. <td><button>登录</button></td>
  130. </tr>
  131. </table>
  132. </form>
  133. </div>
  134. </div>
  135. </body>
  136. </html>
  137. <script>
  138. const btn = document.querySelector('header button');
  139. const modal = document.querySelector('.modal');
  140. const close = document.querySelector('.close');
  141. btn.addEventListener('click', setModal, false);
  142. close.addEventListener('click', setModal, false);
  143. function setModal(ev) {
  144. ev.preventDefault();
  145. let status = window.getComputedStyle(modal, null).getPropertyValue('display');
  146. modal.style.display = status === 'none' ? 'block' : 'none';
  147. }
  148. </script>


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