Blogger Information
Blog 47
fans 3
comment 0
visits 38261
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
定位的类型与应用场景和使用条件、实现一个模态框
Original
758 people have browsed it

定位的类型与应用场景和使用条件、实现一个模态框

1.定位的类型

定位的类型:静态定位static,相对定位relative,绝对定位absolute,固定定位fixed

1.静态定位:position(static)默认,也就是文档流的定位,元素的显示位置与源码顺序一致

2.相对定位:position(relative),相对于元素在文档流中的元素位置进行偏移

3.绝对定位:position(absolue),相对于它的祖先中离它最近的”定位元素”的位置进行偏移

4.固定定位:position(fixed),是绝对定位的一个特例,它始终相对于html定位
如果祖先元素中不存在定位元素,它就参照根元素(html)进行定位
position(static)不是定位元素
只有定位元素才有资格充当绝对定位元素的定位祖先元素(定位参考元素,定位父级)

2.应用场景和使用条件

  • 相对定位:position(relative)

    1. <html lang="en">
    2. <head>
    3. <meta charset="UTF-8">
    4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    5. <title>定位的种类</title>
    6. <style>
    7. * {
    8. padding: 0;
    9. margin: 0;
    10. box-sizing: border-box;
    11. }
    12. .box {
    13. width: 15em;
    14. height: 15em;
    15. border: 1px solid red;
    16. margin: 2em auto;
    17. }
    18. .box h2 {
    19. border: 1px solid black;
    20. background-color: skyblue;
    21. /* 使用相对定位 */
    22. position: relative;
    23. top: 1em;
    24. left: 1em;
    25. }
    26. </style>
    27. </head>
    28. <body>
    29. <div class="box">
    30. <h2>Hello World</h2>
    31. <p>广告位广告位广告位!</p>
    32. </div>
    33. </body>
    34. </html>

    演示截图

  • 绝对定位并完全覆盖父元素 :position(absolue)

    1. <html lang="en">
    2. <head>
    3. <meta charset="UTF-8">
    4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    5. <title>定位的种类</title>
    6. <style>
    7. * {
    8. padding: 0;
    9. margin: 0;
    10. box-sizing: border-box;
    11. }
    12. .box {
    13. width: 15em;
    14. height: 15em;
    15. border: 1px solid red;
    16. margin: 2em auto;
    17. }
    18. .box {
    19. position: relative;
    20. }
    21. .box h2 {
    22. border: 1px solid violet;
    23. background-color: skyblue;
    24. position: absolute;
    25. top: 0;
    26. left: 0;
    27. right: 0;
    28. bottom: 0;
    29. }
    30. </style>
    31. </head>
    32. <body>
    33. <div class="box">
    34. <h2>Hello World</h2>
    35. <p>广告位广告位广告位!</p>
    36. </div>
    37. </body>
    38. </html>

    演示截图

  • 固定定位实现右下角广告: position(fixed)

    1. <html lang="en">
    2. <head>
    3. <meta charset="UTF-8">
    4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    5. <title>定位的种类</title>
    6. <style>
    7. * {
    8. padding: 0;
    9. margin: 0;
    10. box-sizing: border-box;
    11. }
    12. .box {
    13. width: 15em;
    14. height: 15em;
    15. border: 1px solid red;
    16. margin: 2em auto;
    17. }
    18. .box {
    19. position: relative;
    20. }
    21. .box h2 {
    22. border: 1px solid violet;
    23. background-color: skyblue;
    24. position: fixed;
    25. bottom: 0;
    26. right: 0;
    27. }
    28. html {
    29. min-height: 100em;
    30. }
    31. </style>
    32. </head>
    33. <body>
    34. <div class="box">
    35. <h2>Hello World</h2>
    36. <p>广告位广告位广告位!</p>
    37. </div>
    38. </body>
    39. </html>

2.实现一个模态框

  1. <html lang="en">
  2. <head>
  3. <meta charset="UTF-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <title>固定定位实现:模态框</title>
  6. <style>
  7. * {
  8. padding: 0;
  9. margin: 0;
  10. box-sizing: border-box;
  11. }
  12. header {
  13. background-color: skyblue;
  14. overflow: hidden;
  15. }
  16. header h2 {
  17. float: left;
  18. }
  19. header button {
  20. height: 3em;
  21. width: 10em;
  22. float: right;
  23. }
  24. header button:hover {
  25. cursor: pointer;
  26. background-color: #fff;
  27. }
  28. .close {
  29. float: right;
  30. height: 2.5em;
  31. width: 3em;
  32. border: 0;
  33. border-radius: 0.5em;
  34. outline: none;
  35. background-color: rgb(1, 231, 135);
  36. }
  37. .close:hover {
  38. background-color: #eee;
  39. }
  40. /* 蒙版 */
  41. .modal .modal-backdrap {
  42. background-color: rgb(0,0,0,0.5);
  43. position: fixed;
  44. top: 0;
  45. left: 0;
  46. right: 0;
  47. bottom: 0;
  48. }
  49. .modal .modal-body {
  50. padding: 1em;
  51. min-width: 20em;
  52. border-radius: 0.5em;
  53. border: 1px solid rgb(255, 255, 255);
  54. background: linear-gradient(to right,rgb(54, 206, 252),violet);
  55. /* 固定定位 */
  56. position: fixed;
  57. top: 5em;
  58. left: 30em;
  59. right: 30em;
  60. }
  61. .dl {
  62. margin: 0.5em 5em ;
  63. height: 2em;
  64. width: 8em;
  65. outline: none;
  66. border: 0;
  67. border-radius: 0.5em;
  68. background-color: rgb(17, 255, 215);
  69. }
  70. .dl:hover{
  71. background-color: #fff;
  72. }
  73. .modal {
  74. display: none;
  75. }
  76. </style>
  77. </head>
  78. <body>
  79. <header>
  80. <h2>我的博客</h2>
  81. <button>登录</button>
  82. </header>
  83. <div class="modal">
  84. <!-- 蒙版 -->
  85. <div class="modal-backdrap"></div>
  86. <!-- 主体 -->
  87. <div class="modal-body">
  88. <button class="close">关闭</button>
  89. <form action="" method="POST">
  90. <h2>用户登录</h2>
  91. <label>账号:</label>
  92. <input type="text" name="user" id="user" value="" placeholder="请输入用户名" /><br>
  93. <label>密码:</label>
  94. <input type="password" name="password" id="password" value="" placeholder="请输入密码" /><br>
  95. <button class="dl">登录</button>
  96. </form>
  97. </div>
  98. </div>
  99. <script src="modal.js"></script>
  100. </body>
  101. </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