CSS 主要概览

74 阅读4分钟

BFC

BFC 是块级格式化上下文
下列方式会创建BFC:

  • 浮动元素(元素的 float 不是 none)
  • 绝对定位元素(元素的 position 为 absolute 或 fixed)
  • 行内块 inline block 元素
  • overflow 值不为 visible 的块元素
  • 弹性元素(display为 flex 或 inline-flex元素的直接子元素) 它的优点是:
  1. 清除浮动(为什么不用 .clearfix 呢?)
  2. 防止 margin 合并
  3. 某些古老的布局方式会用到(已过时)

详解可见mdn

如何确定 CSS 选择器优先级

  1. 选择器越具体,其优先级越高(如 id和class,id更具体优先级越高)
  2. 相同优先级,出现在后面的,覆盖前面的
  3. 属性后面加 !important 的优先级最高,但是要少用

如何清除浮动

  1. 方法一: 给父元素加上 .clearfix
.clearfix:after{
     content: '';
     display: block; /*或者 table*/
     clear: both;
 }

  1. 方法二:给父元素加上 overflow:hidden

两种盒模型(box-sizing)的区别

第一种盒模型是 content-box,即 width 指定的是 content 区域宽度,而不是实际宽度,公式为

实际宽度 = width + padding + border

第二种盒模型是 border-box,即 width 指定的是左右边框外侧的距离,公式为

实际宽度 = width

相同点是都是用来指定宽度的,不同点是 border-box 更好用。

如何实现垂直居中

方法一

如果 .parent 的 height 不写,你只需要 padding: 10px 0; 就能将 .child 垂直居中;(但能不写 height 就千万别写 height)

方法二、 table自带功能

<body>
  <table class="parent">
    <tr>
      <td class="child">
      一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字
      </td>
    </tr>
  </table>
</body>
      //以下为css
.parent{
  height: 600px;
}

在css上给.parent添加高度即可

方法三、div 装成 table

<body>
  <div class="table">
      <div class="td">
        <div class="child">
          一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文
        </div>
    </div>
  </div>
</body>
div.table{
  display: table;
  height: 600px;
}
div.td{
  display: table-cell;
  vertical-align: middle;
}

方法四、100% 高度的 afrer before 加上 inline block

<body>
  <div class="parent">
    <div class="child">
      一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字
    </div>
  </div>
</body>
.parent{
  border: 3px solid red;
  height: 600px;
  text-align: center;
}

.child{
  border: 3px solid black;
  display: inline-block;
  width: 300px;
  vertical-align: middle;
}

.parent:before{
  content:'';
  outline: 3px solid red;
  display: inline-block;
  height: 100%;
  vertical-align: middle;
}
.parent:after{
  content:'';
  outline: 3px solid red;
  display: inline-block;
  height: 100%;
  vertical-align: middle;
}

方法五、margin-top -50%

HTML同上

.parent{
  height: 600px;
  border: 1px solid red;
  position: relative;
}
.child{
  border: 1px solid green;
  width: 300px;
  position: absolute;
  top: 50%;
  left: 50%;
  margin-left: -150px;
  height: 100px;
  margin-top: -50px;
}

方法六、translate -50%

HTML同上

.parent{
  height: 600px;
  border: 1px solid red;
  position: relative;
}
.child{
  border: 1px solid green;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
}

方法七、absolute margin auto

HTML同上

.parent{
  height: 600px;
  border: 1px solid red;
  position: relative;
}
.child{
  border: 1px solid green;
  position: absolute;
  width: 300px;
  height: 200px;
  margin: auto;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}

方法八、flex布局

HTML同上

.parent{
  height: 600px;
  border: 3px solid red;
  
  display: flex;
  justify-content: center;
  align-items: center;
}
.child{
  border: 3px solid green;
  width: 300px;
}