博客列表 >熟悉盒模型常用属性、 理解 box-sizing 的用法、理解定位元素的水平与垂直对齐技术

熟悉盒模型常用属性、 理解 box-sizing 的用法、理解定位元素的水平与垂直对齐技术

忠于原味
忠于原味原创
2020年06月19日 01:35:13822浏览

一、熟悉盒模型常用属性

概述:所有 HTML 元素可以看作盒子,CSS 盒模型本质上是一个盒子,封装周围的 HTML 元素,它包括:边距,边框,填充,和实际内容。盒模型允许我们在其它元素和周围元素边框之间的空间放置元素。

下面的图片说明了盒子模型:

  • Margin(外边距) - 清除边框外的区域,外边距是透明的。
  • Border(边框) - 围绕在内边距和内容外的边框。
  • Padding(内边距) - 清除内容周围的区域,内边距是透明的。
  • Content(内容) - 盒子的内容,显示文本和图像。
  • 轮廓 Outline: 位于 border 与 margin 之间,因为不占空间, 可暂时忽略

    针对 padding,margin,border 的每一条边都可以单独设置属性
    pading 和 margin 是背景透明的,所以只能设置宽度例如:

  • padding-top,padding-right,padding-bottom,padding-left
  • margin-top,margin-right,margin-bottom,margin-left

    border 比较特殊, 除了可以设置宽度, 还可以设置样式和颜色,所以有更多的属性例如:

  • border-top: 1px solid black

  • border-right: 1px solid green

  • border-bottom: 1px solid gray

  • border-left: 1px solid skyblue

    具体示例代码:

  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. <style>
  8. .box {
  9. /* 宽、高:内容区 */
  10. width: 200px;
  11. height: 200px;
  12. }
  13. .one {
  14. padding: 10px;
  15. border: 2px solid red;
  16. background-clip: content-box;
  17. background-color: lightgreen;
  18. /* margin: top right bottom left ; */
  19. /* margin: 0 0 20px 0; */
  20. margin-bottom: 20px;
  21. }
  22. .two {
  23. padding: 10px;
  24. border: 2px solid red;
  25. /* 规定背景的绘制区域,默认border-box */
  26. background-clip: content-box;
  27. background-color: blue;
  28. /* 当二个盒子在垂直方向上,外边距会产生折叠 */
  29. margin-top: 30px;
  30. }
  31. .box.parent {
  32. background-color: lightblue;
  33. /* 一旦一个元素被添加了position,且值非static,那么它就是定位元素 */
  34. /* 相对定位是相对自己做了偏移,这个元素在文档流中的位置不释放 */
  35. position: relative;
  36. left: 50px;
  37. }
  38. .son {
  39. width: 100px;
  40. height: 100px;
  41. background-color: violet;
  42. /* 绝对定位:相对于它的定位父级进行定位 */
  43. position: absolute;
  44. /* 固定定位:忽略你的定位父级,总是相对于<body>定位 */
  45. /* position: fixed; */
  46. left: 50px;
  47. top: 30px;
  48. }
  49. </style>
  50. </head>
  51. <body>
  52. <!-- 块级元素默认垂直排列 -->
  53. <div class="box one"></div>
  54. <div class="box two"></div>
  55. <div class="box parent">
  56. <div class="son"></div>
  57. </div>
  58. </body>
  59. </html>

效果预览:

二、理解 box-sizing 的用法

box-sizing 属性允许你以某种方式定义某些元素,以适应指定区域,说白了就是重新计算盒大小

属性值 说明
content-box 默认值,以内容区为准
border-box 指定宽度和高度(最小/最大属性)确定元素边框。也就是说,对元素指定宽度和高度包括了 padding 和 border 。通过从已设定的宽度和高度分别减去边框和内边距才能得到内容的宽度和高度。
inherit 指定 box-sizing 属性的值,应该从父元素继承

具体实例代码:

  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. <style>
  8. * {
  9. margin: 0;
  10. padding: 0;
  11. box-sizing: border-box;
  12. }
  13. .box {
  14. width: 200px;
  15. height: 180px;
  16. border: 2px solid #000;
  17. background-color: violet;
  18. padding: 40px;
  19. background-clip: content-box;
  20. /* box-sizing: 重新计算盒大小 */
  21. /* content-box: 默认值,以内容区为准 */
  22. /* box-sizing: content-box; */
  23. box-sizing: border-box;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div class="box"></div>
  29. <!-- width: content-width + padding-left/right + border-left/right
  30. height: 206 -->
  31. </body>
  32. </html>

当 box-sizing: border-box;效果预览:

当 box-sizing: content-box;效果预览:

综上可知:盒子实际 width= content-width + padding-left/right + border-left/right ;

三、理解定位元素的水平与垂直对齐技术

具体步骤看代码注释:

  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>margin:auto: 块元素元素的垂直居中</title>
  7. <style>
  8. .container {
  9. width: 300px;
  10. height: 300px;
  11. background-color: lightgreen;
  12. /* 弹性布局 */
  13. /* display: flex;
  14. justify-content: center;
  15. align-items: center; */
  16. /* 转为定位元素 */
  17. position: relative;
  18. }
  19. .container .item {
  20. width: 100px;
  21. height: 100px;
  22. background-color: violet;
  23. /* 水平居中 */
  24. /* text-align: center; */
  25. /* 让浏览器自动计算左右外边距 */
  26. /* margin-left: auto;
  27. margin-right: auto; */
  28. /* 垂直居中,默认margin-top/bottom: 0 */
  29. /* margin-top: auto;
  30. margin-bottom: auto; */
  31. /* 通过绝对定位来实现垂直居中 */
  32. position: absolute;
  33. /* 让当前元素绝对定位的上下文充满整个父级容器 */
  34. /* 左上角开始 */
  35. top: 0;
  36. left: 0;
  37. /* 右下角结束 */
  38. right: 0;
  39. bottom: 0;
  40. /* 水平垂直居中 */
  41. margin: auto;
  42. }
  43. </style>
  44. </head>
  45. <body>
  46. <div class="container">
  47. <div class="item"></div>
  48. </div>
  49. </body>
  50. </html>

效果预览:

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