“`css
position: absolute;
top: 50%;
transform: translateY(-50%);
2. 使用伪元素
“`css
div {
position: relative;
width: 100px;
height: 100px;
div::before {
position: absolute;
top: 0;
left: 50%;
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom: 50px solid green;
3. 使用 transform
“`css
position: absolute;
top: 50%;
transform: translateY(-50%);
4. 使用transform-origin
“`css
position: absolute;
top: 50%;
transform: translateY(-50%);
transform-origin: 0 100%;