1、当一个元素的宽、高都为0,但存在边框时,得到如下效果:
<title>用CSS画三角形</title>
<style>
.triangle {
width: 0;
height: 0;
border-top: 100px solid #f00;
border-right: 100px solid #0f0;
border-bottom: 100px solid #00f;
border-left: 100px solid #ff0;
}
</style>
</head>
<body>
<div class="triangle"></div>
</body>
给border三角形其中三个边框的颜色设置transparent,即可分别实现任一方向的三角形。比如:
上三角形:
<title>用CSS画三角形</title>
<style>
.triangle {
width: 0;
height: 0;
border-top: 100px solid transparent;
border-right: 100px solid transparent;
border-bottom: 100px solid #00f;
border-left: 100px solid transparent;
}
</style>
</head>
<body>
<div class="triangle"></div>
</body>
左三角形:
<title>用CSS画三角形</title>
<style>
.triangle {
width: 0;
height: 0;
border-top: 100px solid transparent;
border-right: 100px solid #0f0;
border-bottom: 100px solid transparent;
border-left: 100px solid transparent;
}
</style>
</head>
<body>
<div class="triangle"></div>
</body>