@规则
- link 导入的样式会在页面加载时同时加载,@import 导入的样式需等页面加载完成后再加载;
实现垂直居中
<div class="container">
<div class="item" style="width: 100px; height: 100px; background: #999;">
块状元素
</div>
</div>
<div class="container">
<div class="item">不定高宽的块状元素</div>
</div>
<div class="container">
<span class="item">行内元素</span>
</div>
.item {
padding: 1rem;
border: 1px solid #999;
}
.container {
height: 12rem;
background: #ccc;
margin: 1rem;
}
绝对定位
.container {
position: relative;
}
.item {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
flex
.container {
display: flex;
justify-content: center;
align-items: center;
}
grid
.container {
display: grid;
justify-content: center;
align-content: center;
<!-- place-items: center; 或者 place-content: center; 也可以 -->
}
flex 奇妙的方法
.container {
display: flex;
}
.item {
margin: auto;
}
实现左边固定 300px,右边自适应
浮动
.container {
padding-left: 300px;
height:300px;
}
.left,
.main {
float: left;
height:300px;
}
.left {
width: 300px;
margin-left: -300px;
background-color: red;
}
.main {
width: 100%;
background-color: blue;
}
.aside {
float: left;
width: 100px;
background: red;
}
.main {
overflow: auto;
background: blue;
}
flex
.container {
display: flex;
}
.left {
flex-basis: 300px;
flex-shrink: 0;
}
.main {
flex-grow: 1;
}
grid
.container {
display: grid;
grid-template-columns: 300px 1fr;
}
实现 loading 动画
.loading {
display: inline-block;
width: 5em;
height: 5em;
stroke-width: 0;
stroke: currentColor;
fill: currentColor;
animation: rotate 2s linear infinite;
}
@keyframes rotate {
from {
transform: rotateZ(0deg);
}
to {
transform: rotateZ(360deg);
}
}