博主信息
博文 56
粉丝 0
评论 4
访问量 48413
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
CSS:定位与定位元素-复习
异乡客
原创
832人浏览过

  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>定位与定位元素</title>
  8. </head>
  9. <body>
  10. <!-- 文档流:显示顺序和书写顺序一致 -->
  11. <!-- 文档流默认position:static -->
  12. <div class="box parent">
  13. <div class="box child first"></div>
  14. <div class="box child"></div>
  15. </div>
  16. </body>
  17. </html>
  18. <style>
  19. body{
  20. border: 1px solid #000;
  21. }
  22. .box {
  23. border: 1px solid #000;
  24. }
  25. .box.parent{
  26. width: 300px;
  27. height: 400px;
  28. background-color: lightcyan;
  29. }
  30. .box.child{
  31. width:150px;
  32. height: 150px;
  33. background-color: yellow;
  34. }
  35. .box.child.first{
  36. width:150px;
  37. height: 150px;
  38. background-color: lightgreen;
  39. /* 1 相对定位 */
  40. position:relative;
  41. /* 相对于自身在文档流中的位置,元素仍然在文档流中国,原占空间不释放,
  42. 只有相对原位置进行了偏移 */
  43. top:30px;
  44. left:30px;
  45. /* 2 绝对定位 */
  46. /* 相对于他的包含快,看他的元素结构了
  47. 能充当绝对定位包含快的元素,必须是定位元素
  48. 不能是static就可以了
  49. 如果绝对定位元素向上一直找不到可定位的父级,那就相对于Body/html*/
  50. position:absolute;
  51. }
  52. .box.parent{
  53. /* 设定定位参考父级,包含快 */
  54. position: relative;
  55. }
  56. /* 相对定位:相对自身,在文档流中
  57. 绝对定位:相对包含快,不在文档流中 */
  58. /* 3 固定定位 */
  59. /* 永远不动了,是绝对定位的一个子集,就是绝对定位
  60. ,只是包含快是固定,永远是Body/html
  61. */
  62. .box.child.first{
  63. position: fixed;
  64. }
  65. </style>
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系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+教程免费学