博客列表 >【CSS学习】盒模型与元素位置大小、定位元素的水平与垂直对齐技术 20200617作业

【CSS学习】盒模型与元素位置大小、定位元素的水平与垂直对齐技术 20200617作业

Lucas
Lucas原创
2020年06月18日 14:28:19628浏览

盒模型与元素位置大小、定位元素的水平与垂直对齐技术


札记

position 属性

序号 参数 说明
1 absolute 生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位。元素的位置通过 “left”, “top”, “right” 以及 “bottom” 属性进行规定。
2 fixed 生成绝对定位的元素,相对于浏览器窗口进行定位。元素的位置通过 “left”, “top”, “right” 以及 “bottom” 属性进行规定。
3 static 默认值。没有定位,元素出现在正常的流中(忽略 top, bottom, left, right 或者 z-index 声明)。
4 inherit 规定应该从父元素继承 position 属性的值
5 relative 生成相对定位的元素,相对于其正常位置进行定位。因此,”left:20” 会向元素的 LEFT 位置添加 20 像素。

box-sizing

  1. box-sizing: content-box|border-box|inherit;
说明
content-box 指定元素的宽度和高度(最小/最大属性)适用于 box 的宽度和高度。元素的填充和边框布局和绘制指定宽度和高度除外
border-box 指定宽度和高度(最小/最大属性)确定元素边框。也就是说,对元素指定宽度和高度包括了 padding 和 border 。通过从已设定的宽度和高度分别减去边框和内边距才能得到内容的宽度和高度

常用块级元素

address - 地址
blockquote - 块引用
center - 举中对齐块
dir - 目录列表
div - 常用块级容易,也是 css layout 的主要标签
dl - 定义列表
fieldset - form 控制组
form - 交互表单
h1 - 大标题
h2 - 副标题
h3 - 3 级标题
h4 - 4 级标题
h5 - 5 级标题
h6 - 6 级标题
hr - 水平分隔线
isindex - input prompt
menu - 菜单列表
noframes - frames 可选内容(对于不支持 frame 的浏览器显示此区块内容)
noscript - 可选脚本内容(对于不支持 script 的浏览器显示此内容)
ol - 排序列表
p - 段落
pre - 格式化文本
table - 表格
ul - 非排序列表

常用行内元素

a - 锚点
abbr - 缩写
acronym - 首字
b - 粗体(不推荐)
bdo - bidi override
big - 大字体
br - 换行
cite - 引用
code - 计算机代码(在引用源码的时候需要)
dfn - 定义字段
em - 强调
font - 字体设定(不推荐)
i - 斜体
img - 图片
input - 输入框
kbd - 定义键盘文本
label - 表格标签
q - 短引用
s - 中划线(不推荐)
samp - 定义范例计算机代码
select - 项目选择
small - 小字体文本
span - 常用内联容器,定义文本内区块
strike - 中划线
strong - 粗体强调
sub - 下标
sup - 上标
textarea - 多行文本输入框
tt - 电传文本
u - 下划线


盒模型基础、元素位置

  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. <title>盒模型基础</title>
  7. </head>
  8. <style>
  9. .box {
  10. /* 宽、高 */
  11. width: 200px;
  12. height: 200px;
  13. border: 2px solid black;
  14. }
  15. .box.one {
  16. /* margin: top right bottom left; */
  17. margin: 50px 0 50px 0;
  18. /* margin-top: 20px; */
  19. background-color: rgb(221, 50, 50);
  20. /* background-clip 对背景进行裁切 默认是 border-box (边框级别),content-box(内容区) */
  21. background-clip: content-box;
  22. /* 内边距 */
  23. padding: 20px;
  24. box-sizing: content-box;
  25. }
  26. .box.two {
  27. margin-top: 20px;
  28. background-color: lightblue;
  29. background-clip: border-box;
  30. /* padding: 20px; */
  31. box-sizing: border-box;
  32. }
  33. .box.parent {
  34. background-color: lightpink;
  35. /* 一旦一个元素被添加了position 且值非static 那么它就是定位元素 */
  36. position: relative;
  37. /* 从左边向右移动30px */
  38. /* 相对定位是相对自己做了偏移,这个元素在文档流中的位置不释放 */
  39. left: 30px;
  40. top: 40px;
  41. }
  42. .son {
  43. width: 100px;
  44. height: 100px;
  45. background-color: lime;
  46. /* 绝对定位 */
  47. /* 如果没有定位父级元素,会向上一级找,直到找到,就是body元素 */
  48. /* position: absolute; */
  49. /* 固定定位 会忽略定位父级 总是相对于body定位*/
  50. /* 右下角 返回顶部位置 */
  51. position: fixed;
  52. right: 30px;
  53. bottom: 30px;
  54. }
  55. </style>
  56. <body>
  57. <!-- 块级元素默认垂直排列 -->
  58. <div class="box"><p>box 基础框</p></div>
  59. <div class="box one">box one</div>
  60. <!-- width: content_width + padding_left/rigth + border_left/right -->
  61. <div class="box two">box two</div>
  62. <!-- 嵌套关系 -->
  63. <div class="box parent">
  64. <p>parent</p>
  65. <div class="box son">
  66. <p>son</p>
  67. </div>
  68. </div>
  69. </body>
  70. <script>
  71. const box = document.querySelector(".box");
  72. // 1、内容区大小与位置
  73. // 宽高大小=宽/高 +podding
  74. console.log("宽度-clientWidth", box.clientWidth);
  75. // 高度
  76. console.log("高度-clientHeight", box.clientHeight);
  77. // clientleft/clienttop :就是边框宽度 用得很少。
  78. console.log("边框宽度-clientLeft", box.clientLeft);
  79. console.log("边框宽度-clientLeft", box.clientTop);
  80. // 获取可视区的大小:视口
  81. console.log(
  82. "可视区的大小clientWidth",
  83. document.documentElement.clientWidth
  84. );
  85. console.log(
  86. "可视区的大小clientHeight",
  87. document.documentElement.clientHeight
  88. );
  89. // 2、当前元素的定位偏移量,与定位有关
  90. // 定位父级
  91. console.log("定位父级", box.offsetParent);
  92. // 这个元素现在是一个真正的盒子,包括了内容,podding,border
  93. // 真实宽度要加上border
  94. console.log("box.offsetWidth", box.offsetWidth);
  95. console.log("box.offsetHeight", box.offsetHeight);
  96. console.log("box.offsetLeft", box.offsetLeft);
  97. console.log("box.offsetTop", box.offsetTop);
  98. //3、当前文件的滚动大小
  99. // 视口大小:可视区大小
  100. // 文档大小:当前html大小
  101. // 通常视口大小 > 文档大小,所以要通过滚动条才能看到全部html文档内容
  102. // 获取html元素
  103. const html = document.documentElement;
  104. // 当前文档的高度
  105. console.log("文档高度-scrollHeight", html.scrollHeight);
  106. // 当前可视区的高度
  107. console.log("可视区域高度-clientHeight", html.clientHeight);
  108. // 获取滚动条
  109. console.log("滚动条", html.scrollTop);
  110. </script>
  111. </html>

定位元素的水平与垂直对齐

Tags: 需注意是否存在你同级元素,在写代码时,<DIV>同级建立了<H1> ,使用 top 时挡住了 <H1>

  • 样例代码
  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. <title>元素水平对齐与垂直对齐</title>
  7. </head>
  8. <style>
  9. body {
  10. height: 2000px;
  11. }
  12. .container {
  13. width: 300px;
  14. height: 300px;
  15. background-color: red;
  16. position: relative;
  17. }
  18. /* 让浏览器自动计算左右外边距 */
  19. /* margin-left: auto;
  20. margin-right: auto; */
  21. /* 垂直居中 */
  22. /* 不能用margin-top:auto 和 margin-bottom:auto 来垂直居中 */
  23. .container .item1 {
  24. /* 块级元素默认左对齐 */
  25. width: 100px;
  26. height: 100px;
  27. background-color: violet;
  28. position: absolute;
  29. }
  30. .container .item2 {
  31. /* 居中对齐 */
  32. width: 100px;
  33. height: 100px;
  34. background-color: violet;
  35. position: absolute;
  36. /* top: 0; */
  37. right: 0;
  38. /* bottom: 0; */
  39. left: 0;
  40. margin: auto;
  41. }
  42. .container .item3 {
  43. /* 居中对齐 */
  44. width: 100px;
  45. height: 100px;
  46. background-color: violet;
  47. position: absolute;
  48. /* top: 0; */
  49. right: 0;
  50. /* bottom: 0; */
  51. /* left: 0; */
  52. margin: auto;
  53. }
  54. .container .item4 {
  55. /* 顶对齐 */
  56. width: 100px;
  57. height: 100px;
  58. background-color: violet;
  59. position: absolute;
  60. margin-top: auto;
  61. /* 有兄弟元素时,使用top定位会进行覆盖 需注意 使用margin-top*/
  62. /* top: 0; */
  63. /* right: 0; */
  64. /* bottom: 0; */
  65. /* left: 0; */
  66. /* margin: auto; */
  67. }
  68. .container .item5 {
  69. /* 顶对齐 */
  70. width: 100px;
  71. height: 100px;
  72. background-color: violet;
  73. position: absolute;
  74. /* margin-top: auto; */
  75. /* 有兄弟元素时,使用top定位会进行覆盖 需注意 使用margin-top*/
  76. margin-top: auto;
  77. margin-bottom: auto;
  78. top: 0;
  79. /* right: 0; */
  80. bottom: 0;
  81. /* left: 0; */
  82. margin: auto;
  83. }
  84. .container .item6 {
  85. /* 顶对齐 */
  86. width: 100px;
  87. height: 100px;
  88. background-color: violet;
  89. position: absolute;
  90. /* margin-top: auto; */
  91. /* 有兄弟元素时,使用top定位会进行覆盖 需注意 使用margin-top*/
  92. /* margin-top: auto; */
  93. /* margin-bottom: auto; */
  94. /* top: 0; */
  95. /* right: 0; */
  96. bottom: 0;
  97. /* left: 0; */
  98. margin: auto;
  99. }
  100. .container .item7 {
  101. /* 顶对齐 */
  102. width: 100px;
  103. height: 100px;
  104. background-color: violet;
  105. position: absolute;
  106. /* margin-top: auto; */
  107. /* margin-top: auto; */
  108. /* margin-bottom: auto; */
  109. top: 0;
  110. right: 0;
  111. bottom: 0;
  112. left: 0;
  113. margin: auto;
  114. }
  115. </style>
  116. <body>
  117. <h1>水平对齐</h1>
  118. <div class="container">
  119. <h2>左对齐</h2>
  120. <div class="item1"></div>
  121. </div>
  122. <div class="container">
  123. <h2>居中对齐</h2>
  124. <div class="item2"></div>
  125. </div>
  126. <div class="container">
  127. <h3>右对齐</h3>
  128. <div class="item3"></div>
  129. </div>
  130. <h1>垂直对齐</h1>
  131. <div class="container">
  132. <h3>顶对齐</h3>
  133. <div class="item4"></div>
  134. </div>
  135. <div class="container">
  136. <h3>中间对齐</h3>
  137. <div class="item5"></div>
  138. </div>
  139. <div class="container">
  140. <h3>底对齐</h3>
  141. <div class="item6"></div>
  142. </div>
  143. <h1>垂直水平居中对齐</h1>
  144. <div class="container">
  145. <div class="item7"></div>
  146. </div>
  147. </body>
  148. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议