实现一个矩形,居中,宽高2:1
复习position:
- static:默认的布局;即元素在正常文档流的位置。
- relative:相对定位;不脱离文档流,移动参照点是定位前的位置。
- absolute:绝对定位;脱离文档流,参照点是最近的非static的祖先元素。
- fixed:绝对定位;脱离文档流,参照点是屏幕视口。
- sticky:粘性定位;不脱离文档流,参照点是最近的滚动父元素。
具体效果请查看MDN
.box{
position: absolute;
top: 0;
right: 0;
left: 0;
bottom: 0;
margin: auto;
/* position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto; */
background-color: cadetblue;
width: 20%;
height: 0;
padding-top: 10%;
}
border实现三角形
利用before、和after结合border实现的正方形还可实现六边形。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>border实现三角形</title>
<style>
.box{
/* =========水平垂直居中========== */
position: absolute;
right: 0;
left: 0;
top: 0;
bottom: 0;
margin: auto;
/* ==========等腰三角形============ */
width: 0;
height: 0;
/* border: 50px solid goldenrod; */
/* 向上的三角形 */
/* border-top: 50px solid red; */
border-bottom: 50px solid green;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>