38_定位.html 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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>Document</title>
  7. <style>
  8. .box{
  9. width: 300px;
  10. height: 30000px;
  11. border:3px solid blue;
  12. /* background-color: #ccc; */
  13. /* 如果只是relative定位 在没有加任何位移方向的时候那么对于当前元素没有任何影响 ,一般情况下让子元素相对父元素进行定位就添加relative定位即可 */
  14. position: relative;
  15. }
  16. .item{
  17. width: 100px;
  18. height: 100px;
  19. background-color: red;
  20. /* margin-top: 100px;
  21. margin-left: 100px; */
  22. /* positon 定位 */
  23. /* 相对定位 relative 相对于自己的位置进行定位 */
  24. /* 定位元素会出现层级覆盖的情况 */
  25. /* position: relative; */
  26. /* 定位需要配合 top left bottom right */
  27. /* top:100px;
  28. left:100px; */
  29. /* 绝对定位 absolute 相对于最近的有定位的父元素进行定位 如果父元素没有定位那么继续向上查找 直到找到body元素 */
  30. /* 绝对定位的元素会脱离文档流 所以不会占用文档流的位置 */
  31. /* 绝对定位无法恢复到文档流的位置 */
  32. /* 当想把一个元素放到另一个元素的指定位置时 可以使用绝对定位 */
  33. /* position: absolute;
  34. top: 0;
  35. right: 0; */
  36. /* 固定定位 fixed 相对于浏览器窗口进行定位 */
  37. /* 固定定位的元素会脱离文档流 所以不会占用文档流的位置 */
  38. /* 一般用作与返回顶部的按钮 */
  39. position: fixed;
  40. bottom: 100px;
  41. right: 100px;
  42. }
  43. .span1{
  44. position:relative;
  45. top:20px;
  46. }
  47. </style>
  48. </head>
  49. <body>
  50. <div class="box">
  51. <div class="item"></div>
  52. <span class="span1">hello</span>
  53. <span class="span2">world</span>
  54. </div>
  55. </body>
  56. </html>