经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » CSS » 查看文章
轻松掌握 CSS 3 定位布局特性 -cyy
来源:cnblogs  作者:陈莺莺呀  时间:2020/11/9 15:18:26  对本文有异议

通过定位设置尺寸:

当元素没有设置宽度和高度时,定位会影响元素的尺寸

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta name="keywords" content="关键词1,关键词2,关键词3">
  7. <meta name="description" content="网站描述bla bla">
  8. <title>网站标题</title>
  9. <link rel="stylesheet" href="style.css">
  10. <style>
  11. *{
  12. margin:0;
  13. padding:0;
  14. }
  15. main{
  16. width:200px;
  17. height:200px;
  18. border:1px solid red;
  19. position:relative;
  20. }
  21. div{
  22. background:pink;
  23. position:absolute;
  24. left:100px;
  25. right:0;
  26. top:0;
  27. bottom:0;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <main>
  33. <div></div>
  34. </main>
  35. </body>
  36. </html>

 

 

控制元素居中定位的方式:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta name="keywords" content="关键词1,关键词2,关键词3">
  7. <meta name="description" content="网站描述bla bla">
  8. <title>网站标题</title>
  9. <link rel="stylesheet" href="style.css">
  10. <style>
  11. *{
  12. margin:0;
  13. padding:0;
  14. }
  15. main{
  16. width:200px;
  17. height:200px;
  18. border:1px solid red;
  19. position:relative;
  20. }
  21. div{
  22. width:100px;
  23. height:100px;
  24. background:pink;
  25. position:absolute;
  26. left:50%;
  27. top:50%;
  28. margin-top:-50px;
  29. margin-left:-50px;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <main>
  35. <div></div>
  36. </main>
  37. </body>
  38. </html>

 

 

滚动对定位元素的影响:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta name="keywords" content="关键词1,关键词2,关键词3">
  7. <meta name="description" content="网站描述bla bla">
  8. <title>网站标题</title>
  9. <link rel="stylesheet" href="style.css">
  10. <style>
  11. *{
  12. margin:0;
  13. padding:0;
  14. }
  15. article{
  16. width:300px;
  17. height:300px;
  18. border:2px solid red;
  19. position:relative;
  20. overflow:scroll;
  21. }
  22. section{
  23. width:300px;
  24. height:1000px;
  25. background:#ddd;
  26. }
  27. div{
  28. width:50px;
  29. height:50px;
  30. position:absolute;
  31. left:0;
  32. bottom:0;
  33. background:pink;
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <article>
  39. <section>
  40. <div></div>
  41. </section>
  42. </article>
  43. </body>
  44. </html>

 

 

滚动会导致绝对定位的元素也进行滚动

 

定位叠加:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta name="keywords" content="关键词1,关键词2,关键词3">
  7. <meta name="description" content="网站描述bla bla">
  8. <title>网站标题</title>
  9. <link rel="stylesheet" href="style.css">
  10. <style>
  11. *{
  12. margin:0;
  13. padding:0;
  14. }
  15. article{
  16. width:300px;
  17. height:300px;
  18. border:2px solid red;
  19. box-sizing: border-box;
  20. cursor:pointer;
  21. }
  22. section{
  23. width:300px;
  24. height:300px;
  25. background:#ddd;
  26. position:absolute;
  27. left:0;
  28. top:0;
  29. }
  30. img{
  31. width:300px;
  32. height:300px;
  33. position:absolute;
  34. left:0;
  35. top:0;
  36. z-index:-1;
  37. }
  38. article:hover img{
  39. z-index:2;
  40. }
  41. </style>
  42. </head>
  43. <body>
  44. <article>
  45. <section></section>
  46. <img src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2616821908,285454954&fm=26&gp=0.jpg" alt="">
  47. </article>
  48. </body>
  49. </html>

可以看到鼠标移入后显示图片

 

 

京东商城购物车部件:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta name="keywords" content="关键词1,关键词2,关键词3">
  7. <meta name="description" content="网站描述bla bla">
  8. <title>网站标题</title>
  9. <link rel="stylesheet" href="style.css">
  10. <style>
  11. *{
  12. margin:0;
  13. padding:0;
  14. }
  15. body{
  16. padding:200px;
  17. }
  18. article{
  19. width:150px;
  20. position:relative;
  21. cursor:pointer;
  22. }
  23. div{
  24. height:50px;
  25. border:1px solid #ddd;
  26. text-align:center;
  27. line-height:50px;
  28. background:#fff;
  29. color:#555;
  30. }
  31. div:nth-of-type(2){
  32. position:absolute;
  33. width:300px;
  34. top:50px;
  35. right:0;
  36. display:none;
  37. z-index:-1;
  38. }
  39. article:hover div:nth-of-type(1){
  40. border-bottom:none;
  41. border-color:red;
  42. }
  43. article:hover div:nth-of-type(2){
  44. display:block;
  45. border-color:red;
  46. }
  47. </style>
  48. </head>
  49. <body>
  50. <article>
  51. <div>我的购物车</div>
  52. <div>购物车中暂无商品</div>
  53. </article>
  54. </body>
  55. </html>

 

 

粘性定位效果很感人:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta name="keywords" content="关键词1,关键词2,关键词3">
  7. <meta name="description" content="网站描述bla bla">
  8. <title>网站标题</title>
  9. <link rel="stylesheet" href="style.css">
  10. <style>
  11. *{
  12. margin:0;
  13. padding:0;
  14. }
  15. body{
  16. padding:200px;
  17. }
  18. article{
  19. width:400px;
  20. height:300px;
  21. overflow:scroll;
  22. border:1px solid pink;
  23. }
  24. section{
  25. margin-bottom:50px;
  26. }
  27. section h2{
  28. color:#fff;
  29. background:pink;
  30. position:sticky;
  31. top:0;
  32. left:0;
  33. }
  34. </style>
  35. </head>
  36. <body>
  37. <main>
  38. <article>
  39. <section>
  40. <h2>
  41. css粘性定位position:sticky1
  42. </h2>
  43. <p>
  44. 1 position:sticky是css定位新增属性;可以说是相对定位relative和固定定位fixed的结合;它主要用在对scroll事件的监听上;简单来说,在滑动过程中,某个元素距离其父元素的距离达到sticky粘性定位的要求时(比如top:100px);position:sticky这时的效果相当于fixed定位,固定到适当位置。
  45. position属性中最有意思的就是sticky了,设置了sticky的元素,在屏幕范围(viewport)时该元素的位置并不受到定位影响(设置是top、left等属性无效),当该元素的位置将要移出偏移范围时,定位又会变成fixed,根据设置的left、top等属性成固定位置的效果。
  46.   可以知道sticky属性有以下几个特点:
  47. 该元素并不脱离文档流,仍然保留元素原本在文档流中的位置。
  48. 当元素在容器中被滚动超过指定的偏移值时,元素在容器内固定在指定位置。亦即如果你设置了top: 50px,那么在sticky元素到达距离相对定位的元素顶部50px的位置时固定,不再向上移动。
  49. 元素固定的相对偏移是相对于离它最近的具有滚动框的祖先元素,如果祖先元素都不可以滚动,那么是相对于viewport来计算元素的偏移量
  50.   比较蛋疼的是这个属性的兼容性还不是很好,目前仍是一个试验性的属性,并不是W3C推荐的标准。它之所以会出现,也是因为监听scroll事件来实现粘性布局使浏览器进入慢滚动的模式,这与浏览器想要通过硬件加速来提升滚动的体验是相悖的。
  51. </p>
  52. </section>
  53. <section>
  54. <h2>
  55. css粘性定位position:sticky2
  56. </h2>
  57. <p>
  58. 2 position:sticky是css定位新增属性;可以说是相对定位relative和固定定位fixed的结合;它主要用在对scroll事件的监听上;简单来说,在滑动过程中,某个元素距离其父元素的距离达到sticky粘性定位的要求时(比如top:100px);position:sticky这时的效果相当于fixed定位,固定到适当位置。
  59. position属性中最有意思的就是sticky了,设置了sticky的元素,在屏幕范围(viewport)时该元素的位置并不受到定位影响(设置是top、left等属性无效),当该元素的位置将要移出偏移范围时,定位又会变成fixed,根据设置的left、top等属性成固定位置的效果。
  60.   可以知道sticky属性有以下几个特点:
  61. 该元素并不脱离文档流,仍然保留元素原本在文档流中的位置。
  62. 当元素在容器中被滚动超过指定的偏移值时,元素在容器内固定在指定位置。亦即如果你设置了top: 50px,那么在sticky元素到达距离相对定位的元素顶部50px的位置时固定,不再向上移动。
  63. 元素固定的相对偏移是相对于离它最近的具有滚动框的祖先元素,如果祖先元素都不可以滚动,那么是相对于viewport来计算元素的偏移量
  64.   比较蛋疼的是这个属性的兼容性还不是很好,目前仍是一个试验性的属性,并不是W3C推荐的标准。它之所以会出现,也是因为监听scroll事件来实现粘性布局使浏览器进入慢滚动的模式,这与浏览器想要通过硬件加速来提升滚动的体验是相悖的。
  65. </p>
  66. </section>
  67. </article>
  68. </main>
  69. </body>
  70. </html>

 

 能够自动吸附住标题

 

粘性同级定位的特性:

如果是同级元素,会叠加;

如果不是同级元素,后面的会把前面的顶走;

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta name="keywords" content="关键词1,关键词2,关键词3">
  7. <meta name="description" content="网站描述bla bla">
  8. <title>网站标题</title>
  9. <link rel="stylesheet" href="style.css">
  10. <style>
  11. *{
  12. margin:0;
  13. padding:0;
  14. }
  15. body{
  16. padding:200px;
  17. }
  18.  
  19. /* 同级元素,后面的把前面的顶走 */
  20. .main1 article{
  21. width:400px;
  22. height:300px;
  23. overflow:scroll;
  24. border:1px solid pink;
  25. }
  26. .main1 section{
  27. margin-bottom:50px;
  28. }
  29. .main1 section h2{
  30. color:#fff;
  31. background:pink;
  32. position:sticky;
  33. top:0;
  34. left:0;
  35. }
  36.  
  37. /* 非同级元素,后面的会覆盖住前面的 */
  38. .main2 article{
  39. width:400px;
  40. height:300px;
  41. overflow:scroll;
  42. border:1px solid pink;
  43. }
  44. .main2 p{
  45. margin-bottom:50px;
  46. }
  47. .main2 h2{
  48. color:#fff;
  49. background:pink;
  50. position:sticky;
  51. top:0;
  52. left:0;
  53. }
  54. </style>
  55. </head>
  56. <body>
  57. <main class="main1">
  58. <article>
  59. <section>
  60. <h2>
  61. css粘性定位position:sticky1
  62. </h2>
  63. <p>
  64. 1 position:sticky是css定位新增属性;可以说是相对定位relative和固定定位fixed的结合;它主要用在对scroll事件的监听上;简单来说,在滑动过程中,某个元素距离其父元素的距离达到sticky粘性定位的要求时(比如top:100px);position:sticky这时的效果相当于fixed定位,固定到适当位置。
  65. position属性中最有意思的就是sticky了,设置了sticky的元素,在屏幕范围(viewport)时该元素的位置并不受到定位影响(设置是top、left等属性无效),当该元素的位置将要移出偏移范围时,定位又会变成fixed,根据设置的left、top等属性成固定位置的效果。
  66.   可以知道sticky属性有以下几个特点:
  67. 该元素并不脱离文档流,仍然保留元素原本在文档流中的位置。
  68. 当元素在容器中被滚动超过指定的偏移值时,元素在容器内固定在指定位置。亦即如果你设置了top: 50px,那么在sticky元素到达距离相对定位的元素顶部50px的位置时固定,不再向上移动。
  69. 元素固定的相对偏移是相对于离它最近的具有滚动框的祖先元素,如果祖先元素都不可以滚动,那么是相对于viewport来计算元素的偏移量
  70.   比较蛋疼的是这个属性的兼容性还不是很好,目前仍是一个试验性的属性,并不是W3C推荐的标准。它之所以会出现,也是因为监听scroll事件来实现粘性布局使浏览器进入慢滚动的模式,这与浏览器想要通过硬件加速来提升滚动的体验是相悖的。
  71. </p>
  72. </section>
  73. <section>
  74. <h2>
  75. css粘性定位position:sticky2
  76. </h2>
  77. <p>
  78. 2 position:sticky是css定位新增属性;可以说是相对定位relative和固定定位fixed的结合;它主要用在对scroll事件的监听上;简单来说,在滑动过程中,某个元素距离其父元素的距离达到sticky粘性定位的要求时(比如top:100px);position:sticky这时的效果相当于fixed定位,固定到适当位置。
  79. position属性中最有意思的就是sticky了,设置了sticky的元素,在屏幕范围(viewport)时该元素的位置并不受到定位影响(设置是top、left等属性无效),当该元素的位置将要移出偏移范围时,定位又会变成fixed,根据设置的left、top等属性成固定位置的效果。
  80.   可以知道sticky属性有以下几个特点:
  81. 该元素并不脱离文档流,仍然保留元素原本在文档流中的位置。
  82. 当元素在容器中被滚动超过指定的偏移值时,元素在容器内固定在指定位置。亦即如果你设置了top: 50px,那么在sticky元素到达距离相对定位的元素顶部50px的位置时固定,不再向上移动。
  83. 元素固定的相对偏移是相对于离它最近的具有滚动框的祖先元素,如果祖先元素都不可以滚动,那么是相对于viewport来计算元素的偏移量
  84.   比较蛋疼的是这个属性的兼容性还不是很好,目前仍是一个试验性的属性,并不是W3C推荐的标准。它之所以会出现,也是因为监听scroll事件来实现粘性布局使浏览器进入慢滚动的模式,这与浏览器想要通过硬件加速来提升滚动的体验是相悖的。
  85. </p>
  86. </section>
  87. </article>
  88. </main>
  89.  
  90. <main class="main2">
  91. <article>
  92. <h2>
  93. css粘性定位position:sticky1
  94. </h2>
  95. <p>
  96. 1 position:sticky是css定位新增属性;可以说是相对定位relative和固定定位fixed的结合;它主要用在对scroll事件的监听上;简单来说,在滑动过程中,某个元素距离其父元素的距离达到sticky粘性定位的要求时(比如top:100px);position:sticky这时的效果相当于fixed定位,固定到适当位置。
  97. position属性中最有意思的就是sticky了,设置了sticky的元素,在屏幕范围(viewport)时该元素的位置并不受到定位影响(设置是top、left等属性无效),当该元素的位置将要移出偏移范围时,定位又会变成fixed,根据设置的left、top等属性成固定位置的效果。
  98.   可以知道sticky属性有以下几个特点:
  99. 该元素并不脱离文档流,仍然保留元素原本在文档流中的位置。
  100. 当元素在容器中被滚动超过指定的偏移值时,元素在容器内固定在指定位置。亦即如果你设置了top: 50px,那么在sticky元素到达距离相对定位的元素顶部50px的位置时固定,不再向上移动。
  101. 元素固定的相对偏移是相对于离它最近的具有滚动框的祖先元素,如果祖先元素都不可以滚动,那么是相对于viewport来计算元素的偏移量
  102.   比较蛋疼的是这个属性的兼容性还不是很好,目前仍是一个试验性的属性,并不是W3C推荐的标准。它之所以会出现,也是因为监听scroll事件来实现粘性布局使浏览器进入慢滚动的模式,这与浏览器想要通过硬件加速来提升滚动的体验是相悖的。
  103. </p>
  104. <h2>
  105. css粘性定位position:sticky2
  106. </h2>
  107. <p>
  108. 2 position:sticky是css定位新增属性;可以说是相对定位relative和固定定位fixed的结合;它主要用在对scroll事件的监听上;简单来说,在滑动过程中,某个元素距离其父元素的距离达到sticky粘性定位的要求时(比如top:100px);position:sticky这时的效果相当于fixed定位,固定到适当位置。
  109. position属性中最有意思的就是sticky了,设置了sticky的元素,在屏幕范围(viewport)时该元素的位置并不受到定位影响(设置是top、left等属性无效),当该元素的位置将要移出偏移范围时,定位又会变成fixed,根据设置的left、top等属性成固定位置的效果。
  110.   可以知道sticky属性有以下几个特点:
  111. 该元素并不脱离文档流,仍然保留元素原本在文档流中的位置。
  112. 当元素在容器中被滚动超过指定的偏移值时,元素在容器内固定在指定位置。亦即如果你设置了top: 50px,那么在sticky元素到达距离相对定位的元素顶部50px的位置时固定,不再向上移动。
  113. 元素固定的相对偏移是相对于离它最近的具有滚动框的祖先元素,如果祖先元素都不可以滚动,那么是相对于viewport来计算元素的偏移量
  114.   比较蛋疼的是这个属性的兼容性还不是很好,目前仍是一个试验性的属性,并不是W3C推荐的标准。它之所以会出现,也是因为监听scroll事件来实现粘性布局使浏览器进入慢滚动的模式,这与浏览器想要通过硬件加速来提升滚动的体验是相悖的。
  115. </p>
  116. </article>
  117. </main>
  118. </body>
  119. </html>

 

原文链接:http://www.cnblogs.com/chenyingying0/p/13945647.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号