博客列表 >绝对定位与固定定位和flex必会的容器与项目属性

绝对定位与固定定位和flex必会的容器与项目属性

P粉890456325
P粉890456325原创
2023年02月13日 12:24:23271浏览

1. 实例演示绝对定位与固定定位,并描述联系与区别 2. 实例演示flex必会的容器与项目属性

绝对定位

绝对: position: absolute, 相对距离最近的定位元素发生偏移

宽度自动收缩到内容宽度, 释放所占空间,后面顶上去,但是还在父容器中
li 定位元素
li ul 不是定位元素
li ul body 非定位
html 非定位
所有祖先都不是可定位元素,没有参考物了,就得找一个默认的,视口或html

  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="relative">item1</li>
  12. <li class="absolute">item2</li>
  13. <!-- <li style="background: green">item3</li> -->
  14. </ul>
  15. <style>
  16. /* 初始化 */
  17. * {
  18. margin: 0;
  19. padding: 0;
  20. box-sizing: border-box;
  21. }
  22. body {
  23. border: 5px solid red;
  24. }
  25. /* 基本样式 */
  26. ul {
  27. width: 400px;
  28. height: 400px;
  29. border: 5px solid blue;
  30. }
  31. ul li {
  32. height: 30px;
  33. border: 1px solid #000;
  34. list-style: none;
  35. }
  36. ul li:first-child {
  37. background-color: yellow;
  38. }
  39. ul li:first-child + * {
  40. background-color: lightgreen;
  41. }
  42. /* 相对定位 */
  43. ul li.relative {
  44. /* 转为"可定位元素" */
  45. position: relative;
  46. top: 10px;
  47. left: 10px;
  48. /* item2没有向上偏移,说明 item1 还在文档流中,所占空间没有释放 */
  49. }
  50. /* 绝对定位 */
  51. ul li.absolute {
  52. /* 定位元素 */
  53. position: absolute;
  54. /* 宽度自动收缩到内容宽度, 释放所占空间,后面顶上去,但是还在父容器中 */
  55. /* top: 40px
  56. item1.height(28+2) + ul.border(5) + body.border(5) = 40px
  57. left: 10px
  58. ul.border(5) + body.border(5) = 10px
  59. */
  60. top: 100px;
  61. left: 100px;
  62. width: auto;
  63. height: auto;
  64. right: 200px;
  65. bottom: 100px;
  66. /* 宽度受限, 高度受内容伸展的空间进行布局 */
  67. /* 水平居中非常容易,但垂直极困难 */
  68. top: 0;
  69. left: 0;
  70. right: 0;
  71. bottom: 0;
  72. width: 200px;
  73. height: 200px;
  74. margin: auto auto;
  75. }
  76. /* li 定位元素 */
  77. /* li ul 不是定位元素 */
  78. /* li ul body 非定位 */
  79. /* html 非定位 */
  80. /* 所有祖先都不是可定位元素,没有参考物了,就得找一个默认的,视口或html */
  81. ul {
  82. /* 将 li 的包容器,转为定位元素 */
  83. position: relative;
  84. }
  85. </style>
  86. </body>
  87. </html>

固定定位

固定: position: fixed, 总相对于最初包含块(html)偏移

  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. <div class="box">广告位/客服</div>
  11. <style>
  12. * {
  13. margin: 0;
  14. padding: 0;
  15. }
  16. .box {
  17. width: 150px;
  18. height: 150px;
  19. background-color: lightgreen;
  20. /* 固定定位 */
  21. position: fixed;
  22. /* top: 0;
  23. left: 0; */
  24. /* 右上角 */
  25. /* top: 0;
  26. right: 0; */
  27. /* 右下角 */
  28. /* left: 0;
  29. bottom: 0; */
  30. /* 右下角 */
  31. right: 20px;
  32. bottom: 50px;
  33. }
  34. body {
  35. min-height: 2000px;
  36. }
  37. </style>
  38. </body>
  39. </html>

绝对定位与固定定位的区别在 绝对: position: absolute, 相对距离最近的定位元素发生偏移,而固定: position: fixed, 总相对于最初包含块(html)偏移。

flex属性

  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>flex属性</title>
  8. </head>
  9. <body>
  10. <div class="container">
  11. <div class="item">item1</div>
  12. <div class="item">item2</div>
  13. <div class="item">item3</div>
  14. </div>
  15. <style>
  16. /* flex容器 */
  17. .container {
  18. /* width: 450px; */
  19. height: 100px;
  20. /* 使用flex进行布局,先将它转为flex容器元素 */
  21. display: flex;
  22. /* display: inline-flex; */
  23. /* 容器属性,必须写到容器元素中 .container */
  24. /* 主轴方向 */
  25. /* flex-direction: row;默认是水平行的方向
  26. flex-wrap: nowrap; 默认是不换行 wrap是换行*/
  27. /* flex-flow: flex-direction flex-wrap; 合并属性*/
  28. flex-flow: row nowrap;
  29. /* 为了与后面的Grid布局属性对应 */
  30. /* `place-content`: 项目在"主轴"上的排列方式 */
  31. place-content: start;
  32. /* place-content: start;默认是左边排列 */
  33. /* place-content: end; 右边排列*/
  34. /* 实际上, 主轴上的项目,可以独立进行排列 */
  35. /* 二端对齐 */
  36. /* place-content: space-between; */
  37. /* 分散对齐 两边空间两倍*/
  38. /* place-content: space-around; */
  39. /* 平均对齐 两边空间重叠*/
  40. /* place-content: space-evenly; */
  41. /* place-items: 项目在"交叉轴"上的排列方式 */
  42. /* place-items: stretch;默认值
  43. place-items: start;顶部对齐
  44. place-items: end;底部对齐
  45. place-items: center;居中对齐 */
  46. }
  47. /* flex容器中的子元素,就是flex项目 */
  48. .container > .item {
  49. background-color: lightgreen;
  50. /* height: 50px; */
  51. /* width: 150px;
  52. width: 250px; */
  53. /* 1. flex: 放大比例 允许收缩 主轴空间 */
  54. /* 默认,0不放大,1但可以自动收缩 */
  55. flex: 0 1 auto;
  56. /* flex: initial;默认值 */
  57. /* 2. 完全响应式,允许放大和自动收缩 */
  58. flex: 1 1 auto;
  59. /* flex: auto; */
  60. /* 完全不响应 */
  61. flex: 0 0 auto;
  62. /* flex: none; */
  63. /* 常用 */
  64. flex: 1;
  65. /* 等价于,后面二个值取默认值 1 auto */
  66. flex: 1 1 auto;
  67. flex: 1; /* flex: auto */
  68. }
  69. /* :first-child第一个 */
  70. /* :last-child最后一个 */
  71. .container .item:first-child,
  72. .container .item:last-child {
  73. background-color: yellow;
  74. flex: 1;
  75. }
  76. /* 中间占据三份 两边各占一份 */
  77. .container .item:first-child + * {
  78. flex: 3;
  79. /* place-self: start;顶部对齐 交叉轴上对齐方式 */
  80. }
  81. /* 默认每个项目的order = 0 */
  82. /* 越小越靠前 */
  83. .container .item:first-child {
  84. order: 5;
  85. }
  86. .container .item:first-child + * {
  87. order: 6;
  88. }
  89. .container .item:first-child {
  90. order: 7;
  91. order: -1;
  92. }
  93. </style>
  94. </body>
  95. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议