博主信息
博文 8
粉丝 0
评论 0
访问量 4798
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
实例演示绝对定位与固定定位,并描述联系与区别和flex必会的容器与项目属性
alexcy的学习博客
原创
1135人浏览过

1.实例演示绝对定位与固定定位,并描述联系与区别

a.绝对定位

1、盒子2相对于大盒子box进行了绝对定位
2、盒子1的定位属性是static,默认文档流

  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>Document</title>
  8. <style>
  9. *{
  10. padding: 0;
  11. margin: 0;
  12. }
  13. .box {
  14. position: relative;
  15. width: 1000px;
  16. height: 500px;
  17. border: 5px solid green;
  18. }
  19. .box1 {
  20. width: 100px;
  21. height: 100px;
  22. background-color: aqua;
  23. text-align: center;
  24. line-height: 100px;
  25. }
  26. .box2 {
  27. text-align: center;
  28. line-height: 100px;
  29. top: 150px;
  30. right: 10px;
  31. position: absolute;
  32. width: 100px;
  33. height: 100px;
  34. background-color: coral;
  35. }
  36. </style>
  37. </head>
  38. <body>
  39. <div class="box">
  40. <div class="box1">盒子1</div>
  41. <div class="box2">盒子2</div>
  42. </div>
  43. </body>
  44. </html>

上述代码结果图如下:

b.固定定位

  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>Document</title>
  8. <link rel="stylesheet" href="//at.alicdn.com/t/c/font_3884917_h8g1mdk02fe.css">
  9. <style>
  10. *{
  11. padding: 0;
  12. margin: 0;
  13. }
  14. ul li {
  15. list-style: none;
  16. }
  17. body {
  18. min-height: 2000px;
  19. }
  20. .kefu {
  21. top: 200px;
  22. right: 10px;
  23. position: fixed;
  24. border: 2px solid pink;
  25. width: 200px;
  26. background-color: pink;
  27. }
  28. .kefu h2 {
  29. text-align: center;
  30. border-bottom: 3px solid #fff;
  31. }
  32. .kefu li{
  33. width: 100%;
  34. height: 50px;
  35. border-bottom: 1px solid #fff;
  36. text-align: center;
  37. line-height: 50px;
  38. }
  39. .kefu li:last-child {
  40. border: 0;
  41. }
  42. .kefu a {
  43. text-decoration: none;
  44. color: #fff;
  45. }
  46. .kefu i{
  47. font-size: 30px;
  48. vertical-align: middle;
  49. margin-right: 5px;
  50. }
  51. </style>
  52. </head>
  53. <body>
  54. <div class="kefu">
  55. <h2>在线客服</h2>
  56. <ul>
  57. <li><a href="#"><i class="iconfont icon-QQ-circle-fill"></i>1111</a></li>
  58. <li><a href="#"><i class="iconfont icon-QQ-circle-fill"></i>2222</a></li>
  59. <li><a href="#"><i class="iconfont icon-QQ-circle-fill"></i>3333</a></li>
  60. <li><a href="#"><i class="iconfont icon-QQ-circle-fill"></i>4444</a></li>
  61. </ul>
  62. </div>
  63. </body>
  64. </html>

固定定位效果图如下:

c.绝对定位和固定定位的联系与区别

联系:两者都不会占据空间 区别:绝对定位会受父亲的情况影响,固定定位只针对视口,不会受父级的影响

2.实例演示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>Document</title>
  8. </head>
  9. <body>
  10. <div class="container">
  11. <div class="box">box1</div>
  12. <div class="box">box2</div>
  13. <div class="box">box3</div>
  14. </div>
  15. <style>
  16. .container {
  17. width: 500px;
  18. height: 100px;
  19. line-height: 100px;
  20. display: flex;
  21. background-color: coral;
  22. /* 主轴方向默认水平方向row */
  23. /* flex-direction: row;
  24. flex-wrap: nowrap; */
  25. flex-flow: row nowrap;
  26. /* 默认对其方式 沿着起始边布局,在主轴上的排列*/
  27. place-content: start;
  28. /* 沿着右边 */
  29. /* place-content: end; */
  30. /* 两端对齐 */
  31. /* place-content: space-between; */
  32. /* 分散 */
  33. /* place-content: space-around; */
  34. /* 平均 */
  35. /* place-content: space-evenly; */
  36. /* 在交叉轴上排列 默认*/
  37. /* place-items: stretch; */
  38. /* 顶部对齐 */
  39. /* place-items: start; */
  40. /* 中间对齐 */
  41. /* place-items: center; */
  42. }
  43. .container .box {
  44. background-color: pink;
  45. /* height: 50px; */
  46. /* flex:属性,放大比例,是否允许收缩 占据主轴空间 */
  47. flex: 1 0 auto;
  48. }
  49. .container .box:first-child,
  50. .container .box:last-child{
  51. background-color: blue;
  52. flex: 1;
  53. }
  54. .container .box:first-child + *{
  55. flex: 3;
  56. place-self: start;
  57. }
  58. </style>
  59. </body>
  60. </html>

详细图如下:





批改老师:PHPzPHPz

批改状态:合格

老师批语:完成的很好,没什么问题,继续加油
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学