一、布局

1、盒子模型

1.1 盒子模型宽度计算

  • offsetWidth = (内容宽度 + 内边距 + 边框),无外边距
  • box-sizing:border-box(宽度包括内容宽度 + 内边距 + 边框)、content-box(宽度为内容宽度)

1.2 Margin 纵向重叠问题

  • 相邻元素的 margin-top 和 margin-bottom 会发生重叠
  • 空白内容,如

    也会重叠

1.3 Margin 负值问题

  • margin-top 和 margin-left 负值,元素向上、向左移动
  • margin-right 负值,右侧元素左移,自身不受影响
  • margin-bottom 负值,下方元素上移,自身不受影响

2、BFC 理解与应用

  • Block format context,块级格式化上下文
  • 一块独立渲染区域,内部元素的渲染不会影响边界以外的元素

形成BFC的常见条件

  • body 根元素
  • 浮动元素:float 不是 none
  • 绝对定位元素:position 是 absolute 或 fixed
  • overflow 不是 visible
  • display 是 flex、inline-block、table-cells 等

常见应用:清除浮动、解决 margin 纵向合并问题
参考:https://zhuanlan.zhihu.com/p/25321647

3、Float 布局

圣杯布局和双飞翼布局介绍

  • 三栏布局,中间一栏最先加载和渲染
  • 两侧内容固定,中间内容随着宽度自适应
  • 一般用于 PC 网页
  1. 圣杯布局

    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>圣杯布局</title>
    8. <style type="text/css">
    9. body {
    10. min-width: 550px;
    11. }
    12. #header {
    13. text-align: center;
    14. background-color: #f1f1f1;
    15. }
    16. #container {
    17. padding-left: 200px;
    18. padding-right: 150px;
    19. }
    20. #container .column {
    21. float: left;
    22. }
    23. #center {
    24. background-color: #ccc;
    25. width: 100%;
    26. }
    27. #left {
    28. position: relative;
    29. background-color: yellow;
    30. width: 200px;
    31. margin-left: -100%;
    32. right: 200px;
    33. }
    34. #right {
    35. background-color: red;
    36. width: 150px;
    37. margin-right: -150px;
    38. }
    39. #footer {
    40. text-align: center;
    41. background-color: #f1f1f1;
    42. }
    43. /* 手写 clearfix */
    44. .clearfix:after {
    45. content: '';
    46. display: table;
    47. clear: both;
    48. }
    49. </style>
    50. </head>
    51. <body>
    52. <div id="header">this is header</div>
    53. <div id="container" class="clearfix">
    54. <div id="center" class="column">this is center</div>
    55. <div id="left" class="column">this is left</div>
    56. <div id="right" class="column">this is right</div>
    57. </div>
    58. <div id="footer">this is footer</div>
    59. </body>
    60. </html>

    实现方案一

  • 使用 float 布局
  • 两侧使用 margin 负值,以便和中间内容横行重叠;
  • 防止中间内容被两侧覆盖,使用 padding 预留位置

其他方案

  • flex
  • 中间内容宽度使用 calc 或 flex-grow: 1
  1. 双飞翼布局

    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>双飞翼布局</title>
    8. <style type="text/css">
    9. body {
    10. min-width: 550px;
    11. }
    12. .col {
    13. float: left;
    14. }
    15. #main {
    16. width: 100%;
    17. height: 200px;
    18. background-color: #ccc;
    19. }
    20. #main-wrap {
    21. margin: 0 190px 0 190px;
    22. }
    23. #left {
    24. width: 190px;
    25. height: 200px;
    26. background-color: #0000FF;
    27. margin-left: -100%;
    28. }
    29. #right {
    30. width: 190px;
    31. height: 200px;
    32. background-color: #FF0000;
    33. margin-left: -190px;
    34. }
    35. </style>
    36. </head>
    37. <body>
    38. <div id="main" class="col">
    39. <div id="main-wrap">
    40. this is main
    41. </div>
    42. </div>
    43. <div id="left" class="col">
    44. this is left
    45. </div>
    46. <div id="right" class="col">
    47. this is right
    48. </div>
    49. </body>
    50. </html>

    实现方案一同圣杯布局,使用 margin 预留位置,也可使用上述其他方案

  2. 清除浮动

  • clear: both
  • clearfix 伪类 ```css .clearfix:after { content: ‘’; display: table; clear: both; }

.clearfix { zoom: 1; / 兼容IE低版本 */ }

  1. <a name="VpSRW"></a>
  2. ### 4、Flex 布局
  3. 文档:[http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html](http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html)<br />Flex 布局的基本结构是 Flex 容器(display: flex)中放置多个 Flex 项目(子元素)<br />Flex 容器属性如下:
  4. - flex-direction:主轴方向
  5. - flex-wrap:是否换行
  6. - flex-flow:flex-direction、flex-wrap 的简写
  7. - justify-content:主轴对齐方式
  8. - align-item:交叉轴对齐方式
  9. - align-self:子元素在交叉轴对齐方式(可与其他子元素不同)
  10. Flex 项目属性如下:
  11. - order:项目的排列顺序
  12. - flex-grow:空间有多余时,项目的放大比例
  13. - flex-shrink:空间不足时,项目的缩小比例
  14. - flex-basis:定义分配多余空间之前,项目占据的主轴空间
  15. - flex:flex-grow、flex-shrink、flex-basis 的简写
  16. - align-content:项目单独设置对齐方式
  17. <a name="PCglQ"></a>
  18. ## 二、定位
  19. <a name="HcZTs"></a>
  20. ### 1、absolute 和 relative 定位
  21. - relative 依据自身定位
  22. - absolute 依据最近一层的定位元素定位(absolute、relative、fixed),若没有则依据 body 定位
  23. <a name="u2KqZ"></a>
  24. ### 2、居中对齐的实现方式
  25. 1. 水平居中
  26. - inline 元素:text-align: center
  27. - block 元素:margin: auto
  28. - absolute 元素:left: 50% + margin-left 负值
  29. 2. 垂直居中
  30. - inline 元素:line-height 的值等于 height 值
  31. - flex 布局
  32. - absolute 元素:top: 50% + margin-top 负值
  33. 3. 水平垂直居中
  34. - top: 50%, left: 50%, transform: translate(-50%, -50%)
  35. - top, left, bottom, right: 0 + margin: auto
  36. <a name="9L4Fj"></a>
  37. ## 三、图文样式
  38. <a name="FJF63"></a>
  39. ### 1、line-height 继承
  40. - 写具体数值,如 30px,则继承该值
  41. - 写比例,如 2 或 1.5,则继承该比例
  42. - 写百分比,如 200%,则继承父元素根据百分比计算后出来的值
  43. <a name="mxqqx"></a>
  44. ## 四、响应式
  45. <a name="Apltm"></a>
  46. ### 1、rem
  47. rem 是一个长度单位
  48. - px:绝对长度单位,常用
  49. - em:相对长度单位,相对于父元素,不常用
  50. - rem:相对长度单位,相对于根元素 html,常用于响应式布局
  51. <a name="8kUL4"></a>
  52. ### 2、媒体查询
  53. - 媒体查询(media-query),根据不同的屏幕宽度设置根元素 font-size
  54. - rem:基于根元素的相对单位
  55. ```css
  56. @media only screen and (max-width: 374px) {
  57. html {
  58. font-size: 86px;
  59. }
  60. }
  61. @media only screen and (min-width: 375px) and (max-width: 413px) {
  62. html {
  63. font-size: 100px;
  64. }
  65. }
  66. @media only screen and (min-width: 414px) {
  67. html {
  68. font-size: 110px;
  69. }
  70. }

3、vw/vh

媒体查询 + rem 存在 “阶梯” 性的弊端,面对不同分辨率不够丝滑

  • window.screen.height // 屏幕高度
  • window.innerHeight // 网页视口高度
  • document.body.clientHeight // body 高度

其中

  • vh 是网页视口高度的 1/100
  • vw 是网页视口宽度的 1/100
  • vmax 取二者最大值;vmin 取两者最小值