CSS概念 - 盒模型
示意图:
属性:
box-sizing: content-box | border-box
content-box
border-box
元素宽高是 content + padding + border
CSS概念 - BFC
BFC是一个独立的布局环境,其中的元素布局是不受外界的影响,并且在一个BFC中,块盒与行盒(行盒由一行中所有的内联元素所组成)都会垂直的沿着其父元素的边框排列。
布局规则
- 内部的Box会在垂直方向,一个接一个地放置。
- Box垂直方向的距离由margin决定。属于同一个BFC的两个相邻Box的margin会发生重叠。
- 每个盒子(块盒与行盒)的margin box的左边,与包含块border box的左边相接触(对于从左往右的格式化,否则相反)。即使存在浮动也是如此。
- BFC的区域不会与float box重叠。
- BFC就是页面上的一个隔离的独立容器,容器里面的子元素不会影响到外面的元素。反之也如此。
- 计算BFC的高度时,浮动元素也参与计算。
如何创建BFC
- float的值不是none。
- position的值不是static或者relative。
- display的值是inline-block、table-cell、flex、table-caption或者inline-flex
- overflow的值不是visible
作用
利用BFC避免margin重叠
相关规则:属于同一个bfc的两个相邻box会发生margin重叠。
创建一个新的BFC即可。
清除浮动
相关规则:计算BFC的高度时,浮动元素也参与计算。
父节点未设置高度,子节点设置浮动时,会发生高度塌陷,这时要清除浮动。
给父节点激活BFC,overflow:hidden。
transition 有哪些属性?
transition: property duration timing-function delay;
transition: all 0.5s ease 0
怎么清除浮动?
设置overflow
.parent {overflow: hidden;/* 兼容IE */zoom: 1;}
css伪类,搭配clear
.clear {/* 兼容IE */zoom: 1;}.clear::after {content: '';display: block;clear: both;}
水平居中
- 行内元素:
text-align: center; - 块级元素:
margin: auto; - flex:
display: flex; justify-content: center; - position定位:
position: absolute; left: 50%; transform: translateX(-50%);
垂直居中
- 设置
lien-height等于height - 设置
verticle-align: middle; - position定位:
position: absolute; top: 50%; transform: translateY(-50%); - flex:
display: flex; align-items: center; - table:
display: table;+display: table-cell; verticle-align: center; position: absolute; top: 0; bottom: 0; margin: auto;
CSS单位
px
rem
em
vw / vh
绝对单位
怎么实现栅格布局?
table -> float -> flex -> grid
https://www.zhihu.com/question/28542816
