• 元素定位(绝对、相对、粘滞、固定)


    目录

    一、定位

    1、效果

    2、特点

    3、开启方式

    二、相对定位(relative)

    1、特点

    2、定位原理

    三、绝对定位(absolute)

    1、特点

    2、定位标准

    3、包含块(containing block)

    四、粘滞定位(sticky)

    1、特点

    2、定位标准

    3、例子

    五、固定定位(fixed)

    1、特点

    2、定位标准

    3、例子

    六、代码

    1、相对定位

    2、绝对定位

    3、粘滞定位

    4、固定定位


    一、定位

    1、效果

    在盒子模型中,可以用定位的手段达到修改边界的效果,更准确来说,可以达到在父子类元素、浏览器中随意定位的效果

    2、特点

    不同于修改边界、浮动,定位手段仅仅影响当前元素,而不会排挤其它元素

    3、开启方式

    (1)在元素中打开position,默认值为static,没有定位效果,而其他值才有定位效果

    (2)坐标修改值有top、right、bottom、left,通常对位元素只用其一,例如left和right,因为二维坐标轴只需要两个坐标就够了

    二、相对定位(relative)

    1、特点

    (1)开启后不设置偏移量所有元素不会发生改变

    (2)不会超出文档流

    (3)不会影响元素性质(块元素、行内元素仍保持属性)

    (4)层级会升高(会覆盖以前的元素)

    2、定位原理

    (1)概念

    -相对元素(需要移动的元素)

    -参照物(理解中的坐标原点)

    (2)定位标准

    -相对原文档流中原本的坐标进行移动(通常默认为原定元素的左上角)

    (3)例子

    在本例中,橙色元素本来是块元素,它本来在黄色元素下方,在相对定位中,它的坐标原点就是黄色元素的左下角 

    三、绝对定位(absolute)

    1、特点

    (1)超脱文档流

    (2)如果不设置偏移量,则不设置偏移量的“元素位置”不会发生改变(意思就是,在绝对定位中,位置是不会变化的,但其它元素会受影响,上面是原因)

    (3)层级提升

    (4)元素性质会被改变(因为超脱了文本流)

    2、定位标准

    绝对定位元素是根据其“包含块”进行定位的

    3、包含块(containing block

    (1)常规情况

    -离当前元素最近的祖先块元素

    (2)绝对定位情况

    -离当前绝对定位元素最近的开启定位的祖先元素

    -如果最近的都没有开启定位,则以根元素作为定位标准

    (3)根元素(html,初始包含块)

    四、粘滞定位(sticky)

    1、特点

    有元素到达某个位置时固定的独特特点

    2、定位标准

    粘滞定位特点和相对定位基本一致

    3、例子

    这里的例子不太好看出效果,最好是自己跑代码体会一下效果 

    五、固定定位(fixed)

    1、特点

    (1)唯一不同的是,固定定位永远参照于浏览器的视口定位

    (2)固定定位元素不会跟随网页滚动条滚动,多用于弹窗广告

    (3)但因为版本兼容性问题,不常用

    2、定位标准

    是一种特殊的绝对定位,定位标准与绝对定位差不多

    3、例子

    这里效果不太好通过截图展示,请运行代码体会

    六、代码

    1、相对定位

    1. 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>定位-相对定位title>
    8. <style>
    9. .box1 {
    10. width: 100px;
    11. height: 100px;
    12. background-color: #bfa;
    13. }
    14. .box2 {
    15. width: 100px;
    16. height: 100px;
    17. background-color: red;
    18. position: relative;
    19. top: -100px;
    20. left: 100px;
    21. }
    22. .box3 {
    23. width: 100px;
    24. height: 100px;
    25. background-color: yellow;
    26. }
    27. .box4 {
    28. width: 100px;
    29. height: 100px;
    30. background-color: orange;
    31. position: relative;
    32. top: -100px;
    33. left: 50px;
    34. }
    35. style>
    36. head>
    37. <body>
    38. <div class="box1">div>
    39. <div class="box2">div>
    40. <div class="box3">div>
    41. <div class="box4">div>
    42. body>
    43. html>

    2、绝对定位

    1. 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>定位-绝对定位title>
    8. <style>
    9. .box1 {
    10. margin-top: 100px;
    11. width: 300px;
    12. height: 300px;
    13. background-color: #bfa;
    14. /* position: relative; */
    15. }
    16. .box2 {
    17. width: 200px;
    18. height: 200px;
    19. background-color: red;
    20. /* position: relative; */
    21. }
    22. .box3 {
    23. width: 100px;
    24. height: 100px;
    25. background-color: yellow;
    26. position: absolute;
    27. top: 100px;
    28. left: 100px;
    29. }
    30. .box4 {
    31. width: 100px;
    32. height: 100px;
    33. background-color: orange;
    34. }
    35. style>
    36. head>
    37. <body>
    38. <div class="box1">
    39. <div class="box2">
    40. <div class="box3">div>
    41. div>
    42. div>
    43. <div class="box4">div>
    44. body>
    45. html>

    3、粘滞定位

    1. 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>定位-粘滞定位title>
    8. <style>
    9. * {
    10. padding: 0px;
    11. margin: 0px;
    12. }
    13. body {
    14. height: 2000px;
    15. }
    16. .box1 {
    17. height: 400px;
    18. width: 400px;
    19. background-color: #bfa;
    20. }
    21. ul li {
    22. list-style: none;
    23. float: left;
    24. }
    25. ul {
    26. width: 492px;
    27. height: 48px;
    28. background-color: #ffe;
    29. margin: 40px auto;
    30. position: sticky;
    31. top: 10px;
    32. }
    33. li a {
    34. color: #999;
    35. line-height: 48px;
    36. display: block;
    37. text-decoration: none;
    38. padding: 0px 10px;
    39. }
    40. li a:hover {
    41. background-color: #444;
    42. color: #eee;
    43. }
    44. style>
    45. head>
    46. <body>
    47. <div class="box1">div>
    48. <ul>
    49. <li>
    50. <a href="https://www.w3school.com.cn/h.asp">HTML/CSSa>
    51. li>
    52. <li>
    53. <a href="https://www.w3school.com.cn/b.asp">Brower Sidea>
    54. li>
    55. <li>
    56. <a href="https://www.w3school.com.cn/s.asp">Sever Sidea>
    57. li>
    58. <li>
    59. <a href="https://www.w3school.com.cn/p.asp">Programminga>
    60. li>
    61. <li>
    62. <a href="https://www.w3school.com.cn/x.asp">XMLa>
    63. li>
    64. ul>
    65. body>
    66. html>

    4、固定定位

    1. 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>定位-固定定位title>
    8. <style>
    9. body {
    10. height: 2000px;
    11. }
    12. .box1 {
    13. width: 100px;
    14. height: 100px;
    15. background-color: #bfa;
    16. position: fixed;
    17. right: 0px;
    18. }
    19. .box2 {
    20. width: 100px;
    21. height: 100px;
    22. background-color: red;
    23. }
    24. .box3 {
    25. width: 100px;
    26. height: 100px;
    27. background-color: yellow;
    28. }
    29. .box4 {
    30. width: 100px;
    31. height: 100px;
    32. background-color: orange;
    33. }
    34. style>
    35. head>
    36. <body>
    37. <div class="box1">div>
    38. <div class="box2">div>
    39. <div class="box3">div>
    40. <div class="box4">div>
    41. body>
    42. html>

  • 相关阅读:
    【链接装载与库】 Linux共享库的组织
    Opengl Fence 内部实现
    【算法训练营】 - ①① 暴力递归
    即时通讯开发如何撸一个WebSocket服务器
    Java毕设项目——校园出入管理系统(java+SSM+Maven+Mysql+Jsp)
    【rust/egui】(十一)使用rfd选择文件并使用serde_json进行序列化
    基于遗传算法的微电网调度(风、光、蓄电池、微型燃气轮机)(Matlab代码实现)
    永磁同步电机恒压频比(V/F)开环控制系统Matlab/Simulink仿真分析及代码生成到工程实现
    TensorFlow实战教程(三十二)-Transformer的商品评论情感分析 机器学习和深度学习的Baseline模型实现
    04、SpringBoot + 微信支付 --- 内网穿透ngrok(安装、使用)
  • 原文地址:https://blog.csdn.net/comegoing_xin_lv/article/details/126165972