Blogger Information
Blog 23
fans 0
comment 3
visits 23610
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
定位的类型与应用场景和使用条件及模态框案例
a.
Original
727 people have browsed it

定位的类型

定位属性position在前端开发中非常常用,他的定位类型有哪些呢?

编号 类型 说明
1 静态定位 static 默认,也就是文档流定位,元素的显示位置与源码顺序一致
2 相对定位 relative 相对于之前的位置偏移,它在文档流中的原始空间会保留下来
3 绝对定位 absolue 相对于它的祖先中离它最近的定位元素的位置发生偏移如果祖先中不存在定位元素,它就参考根元素(HTML)进行定位。它在文档流中的原始空间不会保留下来
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. margin: 0;
  10. padding: 0;
  11. box-sizing: border-box;
  12. }
  13. .box {
  14. height: 20em;
  15. width: 20em;
  16. /* 设置相对定位 */
  17. position: relative;
  18. border: solid red 1px;
  19. top: 1em;
  20. }
  21. .box h2 {
  22. /* 设置绝对定位 */
  23. position: absolute;
  24. background-color: rgb(36, 230, 204);
  25. top: 0;
  26. left: 0;
  27. right: 0;
  28. bottom: 0;
  29. margin: auto;
  30. width: 5em;
  31. height: 1.5em;
  32. }
  33. </style>
  34. </head>
  35. <body>
  36. <div class="box">
  37. <h2>php中文网</h2>
  38. </div>
  39. </body>
  40. </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. .main {
  9. background-color: rgba(255, 192, 203, 0.212);
  10. height: 100em;
  11. position: relative;
  12. }
  13. .back-top img {
  14. width: 50px;
  15. position: fixed;
  16. right: 2em;
  17. top: 80vh;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div class="main">
  23. 主体
  24. <a href="#" class="back-top">
  25. <img
  26. src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1608572122793&di=512f5d7c2bd0a352b72abf76124aaa3d&imgtype=0&src=http%3A%2F%2Fku.90sjimg.com%2Felement_origin_min_pic%2F01%2F47%2F97%2F1157440c088f8f9.jpg"
  27. />
  28. </a>
  29. </div>
  30. </body>
  31. </html>
  • 固定定位使用条件:需要参考定位元素
    • 定位元素:只要这个元素中有position:relative;或者position:absolute;

模态框案例

  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. margin: 0;
  10. padding: 0;
  11. box-sizing: border-box;
  12. overflow: hidden;
  13. }
  14. header {
  15. background-color: cadetblue;
  16. padding: 0.5em 2em;
  17. }
  18. header > h2 {
  19. float: left;
  20. }
  21. header > button {
  22. float: right;
  23. height: 2.5em;
  24. width: 12em;
  25. border-radius: 0.3em;
  26. }
  27. header > button:hover {
  28. background-color: rgb(255, 255, 255);
  29. cursor: pointer;
  30. }
  31. .modal {
  32. display: none;
  33. }
  34. /* 蒙板 */
  35. .backdrop {
  36. background-color: rgba(0, 0, 0, 0.281);
  37. position: fixed;
  38. top: 0;
  39. left: 0;
  40. right: 0;
  41. bottom: 0;
  42. }
  43. .modal-body {
  44. padding: 1em;
  45. max-width: 20em;
  46. border: 1px solid red;
  47. background: linear-gradient(to right, red, #efefef);
  48. position: fixed;
  49. top: 30vh;
  50. left: 0em;
  51. right: 0em;
  52. margin: auto;
  53. }
  54. .modal-body form button {
  55. margin: 1em;
  56. width: 20em;
  57. height: 2.5em;
  58. }
  59. .close {
  60. position: absolute;
  61. top: 0;
  62. right: 0;
  63. width: 5em;
  64. }
  65. </style>
  66. </head>
  67. <body>
  68. <header>
  69. <h2>我的博客</h2>
  70. <button>登录</button>
  71. </header>
  72. <!-- 模态框 -->
  73. <div class="modal">
  74. <!-- 蒙板 -->
  75. <div class="backdrop">
  76. <div class="modal-body">
  77. <button class="close">关闭</button><br />
  78. <form action="#" method="POST">
  79. <label for="username">用户:</label>
  80. <input type="text" name="username" id="username" required /><br />
  81. <label for="password">密码:</label>
  82. <input type="text" name="password" id="password" required /><br />
  83. <br />
  84. <hr />
  85. <button>登录</button>
  86. </form>
  87. </div>
  88. </div>
  89. </div>
  90. <script>
  91. const btn = document.querySelector("header button");
  92. const modal = document.querySelector(".modal");
  93. const close = document.querySelector(".close");
  94. btn.addEventListener("click", setModal, false);
  95. close.addEventListener("click", setModal, false);
  96. function setModal(ev) {
  97. ev.preventDefault();
  98. let status = window
  99. .getComputedStyle(modal, null)
  100. .getPropertyValue("display");
  101. modal.style.display = status === "none" ? "block" : "none";
  102. }
  103. </script>
  104. </body>
  105. </html>
Correcting teacher:天蓬老师天蓬老师

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