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

编辑: admin 分类: 电脑知识 发布时间: 2023-06-14 来源:互联网
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>
效果展示

【文章原创作者:建湖网站设计公司 http://www.1234xp.com/jianhu.html 提供,感恩】