博客列表 >默认布局与元素类型及定位原理与演示

默认布局与元素类型及定位原理与演示

OC的PHP大牛之路
OC的PHP大牛之路原创
2022年07月14日 14:00:13289浏览

默认布局与元素类型

文档布局流

position:static静态定位

默认情况下,元素布局的完整流程
1.把内容(匿名文本)放到一个独立的盒模型,如div
2.为盒子添加上padding/border/margin
3.盒子的种类
3.1 块级
属性:display:block/table/table-cell,td/list-item/form/p/h1-h6
宽度:占据父级全部宽度
高低:由内容决定且可自定义
排列:垂直

3.2 内联(行内)
用来描述块级元素内部的内容(文本)
宽度:内容宽度
高度:内容高度
排列:水平排不下则换行显示
宽高不能自定义,padding有效,margin只有左右有效
使用displau:block可将内联元素转为快级,使宽高生效,但后面的其他元素
如果不想让后面的其他元素换行显示,可使用display:inline-block将它转为

3.3 内联块(行内块):即像内联一样水平排列,又像块级一样可设置宽高

定位原理与演示

1.静态定位:position:static

2.相对定位:position:relative
需设置top:/left:/right:/bottom:
参照物:相对元素自身在文档流张的原始位置进行偏移

3.绝对定位:position:absolute
隐藏:visibility:hidden源码有,页面有但看不见
删除:display:none源码有,页面没有
参照物:具有定位属性的包含块(定位父级)
初始包含块:根元素的父级

4.固定定位:position:fixed
永远相对于视口定位,是绝对定位的一个特例,

5.粘性定位:position:sticky
粘性定位=静态定位+固定定位

案例:

  1. <body>
  2. <div class="box parent">
  3. <div class="box child one">相对定位</div>
  4. <div class="box child two">绝对定位</div>
  5. <div class="box child three">固定定位</div>
  6. </div>
  7. <style>
  8. .box{
  9. border: 1px solid ;
  10. }
  11. .box.parent{
  12. width: 400px;
  13. height:400px;
  14. background-color:lightcyan;
  15. }
  16. .box.child{
  17. padding: 20px;
  18. }
  19. .box.child.one{
  20. background-color:aqua;
  21. }
  22. .box.child.two{
  23. background-color:aquamarine;
  24. }
  25. .box.child.three{
  26. background-color: cadetblue;
  27. }
  28. .box.child.one{
  29. display: none;
  30. }

相对于.box.parent定位

  1. .box.parent{
  2. position:relative;
  3. }
  4. .box.child.two{
  5. right: 0;
  6. bottom: 0;
  7. }

相对于body定位

  1. body{
  2. height: 500px;
  3. width: 500px;
  4. border: 2px solid blue;
  5. }
  6. .box.parent{
  7. position: static;
  8. }
  9. body{
  10. position: relative;
  11. }

相对于html定位

  1. html{
  2. width: 700px;
  3. height: 700px;
  4. border: 2px dashed red;
  5. }
  6. body{
  7. position: static;
  8. }
  9. html{
  10. position: relative;
  11. }

注:当绝对定位找不到任何定位父级的时候,它会把“初始包含块”作为定位父级且只出现在视口第一屏,而固定定位是永远对视口定位,所有屏都有效!

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议