博主信息
博文 47
粉丝 0
评论 0
访问量 31361
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
CSS定位
P粉036614676
原创
522人浏览过

1.块级元素

1.1常用的块级元素

  1. div p table ul lo li h1-h6 dl dt

特点:

  • 块级元素独占一行
  • 默认的宽度占满父级元素,行内元素不会换行
  • 可以设置宽高

    1.2行级元素

    1. a img span b strong input select section
    特点:
  • 不可以设置宽高

    1.3内联元素

    特点:
    高度: 内容高度
  • 宽度: 内容宽度
  • 宽高不能自定义
  • padding有效,margin只有左右有效
  • 排列: 水平, 排不下,则换行显示
  • display: inline

    2.块级元素定位

    2.1 static

    静态布局是浏览器默认布局,由上到下布局,先写的盒子在上面,后写的盒子在下面
    样式
    `

    2.2 absolute

    绝对定位:定位根据body元素和absolute元素和relative,不会由于html排版而移动
    就是如果父级元素是定位是absolute元素和relative的盒子,就相对于父盒子,否则相对于body盒子
    样式

    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>Document</title>
    8. <style>
    9. div{
    10. border: 1px dotted red;
    11. }
    12. .father{
    13. background-color: aqua;
    14. position: absolute;
    15. top: 20px;
    16. left: 20px;
    17. }
    18. .child{
    19. background-color: bisque;
    20. }
    21. .sty1{
    22. background-color: blue;
    23. height: 120px;
    24. border: 1px solid black;
    25. /* position: relative; */
    26. </style>
    27. </head>
    28. <body>
    29. </body>
    30. <div class="sty1">
    31. <div class="father">父亲</div>
    32. <div class="child">孩子</div>
    33. </div>
    34. </html>

    2.3 relative

    和staticd定位相比可以使用top,bottom等,就这一点不同
    样式

    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>Document</title>
    8. <style>
    9. div{
    10. border: 1px dotted red;
    11. }
    12. .father{
    13. background-color: aqua;
    14. position: absolute;
    15. top: 20px;
    16. left: 20px;
    17. position: relative;
    18. }
    19. .child{
    20. background-color: bisque;
    21. }
    22. .sty1{
    23. background-color: blue;
    24. height: 120px;
    25. border: 1px solid black;
    26. }
    27. </style>
    28. </head>
    29. <body>
    30. </body>
    31. <div class="sty1">
    32. <div class="child">孩子</div>
    33. </div><div class="father">父亲</div>
    34. </html>

    2.4 fixed

    固定定位:不会由于卷轴而移动,根据body去定位
    样式

    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>Document</title>
    8. <style>
    9. div{
    10. border: 1px dotted red;
    11. }
    12. .father{
    13. background-color: aqua;
    14. position: absolute;
    15. top: 20px;
    16. left: 20px;
    17. position: fixed;
    18. }
    19. .child{
    20. background-color: bisque;
    21. }
    22. .sty1{
    23. background-color: blue;
    24. height: 120px;
    25. border: 1px solid black;
    26. }
    27. </style>
    28. </head>
    29. <body>
    30. </body>
    31. <div class="sty1">
    32. <div class="child">孩子</div>
    33. </div><div class="father">父亲</div>
    34. <div class="sty1"></div>
    35. <div class="sty1"></div>
    36. </html>

    2.5 sticky

    没有到达顶部前会随卷轴而移动,到达顶端后会一直在顶端
    没有top,left等
    样式

    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>Document</title>
    8. <style>
    9. div{
    10. border: 1px dotted red;
    11. }
    12. .father{
    13. background-color: aqua;
    14. /* position: absolute; */
    15. top: 20px;
    16. left: 20px;
    17. position: sticky;
    18. }
    19. .child{
    20. background-color: bisque;
    21. }
    22. .sty1{
    23. background-color: blue;
    24. height: 120px;
    25. border: 1px solid black;
    26. }
    27. </style>
    28. </head>
    29. <body>
    30. </body>
    31. <div class="sty1">
    32. <div class="child">孩子</div>
    33. </div><div class="father">父亲</div>
    34. <div class="sty1"></div>
    35. <div class="sty1"></div>
    36. </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+教程免费学