盒子的类型
display: inline-block;宽适应内容,可以设置宽高
display: block;宽独占一行
display: inline;宽高适应内容
box-sizing:border-box;
box-sizing:content-box;
盒子的边框,内外边距,宽
width: %30; 百分比
width: fit-content; 宽适应内容
padding: ; em
margin: ;em
border-width:px,em
font-size: rem
再确定盒子的位置
float: left;浮动到左边
position: relative;相对,相对于自己
position: absolute绝对,最近的祖先
position: fixed固定,相对视口
position: sticky;粘性
文本
text-align: center;
text-align: left;
text-align: right;
vertical-align: top;
vertical-align: middle;
vertical-align: bottom;
块元素
margin: auto;居中
居中图像,请将左右外边距设置为 auto,并将其设置为块元素
display: block;
margin-left: auto;
margin-right: auto;
左和右对齐 - 使用 position
position: absolute;
right: 0px;
左和右对齐 - 使用 float
float: right;
如果一个元素比包含它的元素高,并且它是浮动的,它将溢出其容器。然后我们可以向包含元素添加 overflow: auto;,来解决此问题
overflow: auto;
有很多方法可以在 CSS 中垂直对齐元素。一个简单的解决方案是使用上下内边距:
padding: 7em 0;
如需同时垂直和水平对齐,请使用 padding 和 text-align: center;
padding: 70px 0;
text-align: center;
垂直对齐 - 使用 line-height
.center {
line-height: 200px;
height: 200px;
border: 3px solid green;
text-align: center;
}
/* 如果有多行文本,请添加如下代码:*/
.center p {
line-height: 1.5;
display: inline-block;
vertical-align: middle;
}
垂直对齐 - 使用 position 和 transform
.center {
height: 200px;
position: relative;
border: 3px solid green;
}
.center p {
margin: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
使用 flexbox 将内容居中
.center {
display: flex;
justify-content: center;
align-items: center;
height: 200px;
border: 3px solid green;
}