博客列表 >绝对定位与固定定位的理解

绝对定位与固定定位的理解

风残念北的博客
风残念北的博客原创
2023年04月12日 17:03:03306浏览

1.绝对定位

代码演示

  1. <!DOCTYPE html>
  2. <html lang="en">
  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>Document</title>
  8. </head>
  9. <body>
  10. <ul>
  11. <li>item1</li>
  12. <li class="absolute">item2</li>
  13. </ul>
  14. <style>
  15. * {
  16. margin: 0;
  17. padding: 0;
  18. box-sizing: border-box;
  19. }
  20. body {
  21. border: 3px solid red;
  22. }
  23. ul {
  24. border: 3px solid blue;
  25. width: 400px;
  26. height: 400px;
  27. }
  28. ul li{
  29. height: 30px;
  30. border: 1px solid black;
  31. list-style: none;
  32. }
  33. ul li:first-child {
  34. background-color: yellow;
  35. }
  36. ul li:first-child + * {
  37. background-color: green;
  38. }
  39. ul li.absolute {
  40. position: absolute;
  41. top: 200px;
  42. left: 300px;
  43. }
  44. </style>
  45. </body>
  46. </html>

效果演示

2.固定定位

代码演示

  1. <!DOCTYPE html>
  2. <html lang="en">
  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>Document</title>
  8. </head>
  9. <body>
  10. <div class="box">广告位</div>
  11. <style>
  12. body {
  13. min-height: 2000px;
  14. }
  15. .box {
  16. background-color: green;
  17. height: 100px;
  18. width: 150px;
  19. position: fixed;
  20. bottom: 10px;
  21. right: 5px;
  22. }
  23. </style>
  24. </body>
  25. </html>

效果展示

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