博客列表 >相对定位、绝对定位与固定定位的位置偏移演示

相对定位、绝对定位与固定定位的位置偏移演示

vbokok
vbokok原创
2023年02月12日 14:40:57323浏览
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>相对定位与绝对定位</title>
  8. </head>
  9. <body>
  10. <ul>
  11. <li class="one">item1</li>
  12. <li class="two">item2</li>
  13. </ul>
  14. <div class="box">box</div>
  15. </body>
  16. </html>
  17. <style>
  18. * {
  19. margin: 0;
  20. padding: 0;
  21. box-sizing: border-box;
  22. }
  23. body {
  24. border: 5px solid red;
  25. height: 20em;
  26. }
  27. ul {
  28. border: 5px solid blue;
  29. height: 14em;
  30. width: 20em;
  31. }
  32. ul li {
  33. border: 1px solid black;
  34. list-style: none;
  35. }
  36. ul li:first-child {
  37. background-color: yellow;
  38. }
  39. ul li:first-child + * {
  40. background-color: violet;
  41. }
  42. ul li.one {
  43. position: relative;
  44. top: 10px;
  45. left: 20px;
  46. }
  47. ul li.two {
  48. position: absolute;
  49. height: 200px;
  50. width: 200px;
  51. left: 0;
  52. right: 0;
  53. top: 0;
  54. bottom: 0;
  55. margin: auto auto;
  56. }
  57. .box {
  58. border: 1px solid darkgreen;
  59. background-color: greenyellow;
  60. height: 150px;
  61. width: 150px;
  62. position: fixed;
  63. right: 10px;
  64. bottom: 10px;
  65. }
  66. </style>

relative:在原始位置上稍偏移位置。
absolute:在附近位置发生元素偏移。
rixed:在HTML块内偏移,不受文档限制

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