我们先来看看边框
<!DOCTYPE html>
<html lang="ch">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>css实现三角形</title>
<style>
.box1 {
width: 200px;
height: 200px;
border: 20px solid;
/* 设置四个边框的颜色不同,方便查看 */
border-color: red blue orange greenyellow;
}
</style>
</head>
<body>
<div class="box1">
</div>
</body>
</html>
效果
可以看到边框交界处是斜的
我们将边框设置大一点实验一下看看
<!DOCTYPE html>
<html lang="ch">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>css实现三角形</title>
<style>
.box1 {
width: 200px;
height: 200px;
background-color: greenyellow;
border-bottom: 150px solid red;
border-right:170px solid orange;
border-left: 170px solid blue;
}
</style>
</head>
<body>
<div class="box1">
</div>
</body>
</html>
发现是这样的
将中间绿色部分宽度缩小会怎样呢?
<!DOCTYPE html>
<html lang="ch">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>css实现三角形</title>
<style>
.box1 {
width: 0px;
height: 200px;
background-color: greenyellow;
border-bottom: 150px solid red;
border-right:170px solid orange;
border-left: 170px solid blue;
}
</style>
</head>
<body>
<div class="box1">
</div>
</body>
</html>
效果
再把高度弄掉会怎样呢
<!DOCTYPE html>
<html lang="ch">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>css实现三角形</title>
<style>
.box1 {
width: 0px;
height: 0px;
background-color: greenyellow;
border-bottom: 150px solid red;
border-right:170px solid orange;
border-left: 170px solid blue;
}
</style>
</head>
<body>
<div class="box1">
</div>
</body>
</html>
效果
之后再把左右两侧边框设置成白色就行了
<!DOCTYPE html>
<html lang="ch">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>css实现三角形</title>
<style>
.box1 {
width: 0px;
height: 0px;
background-color: greenyellow;
border-bottom: 150px solid red;
border-right:170px solid white;
border-left: 170px solid white;
}
</style>
</head>
<body>
<div class="box1">
</div>
</body>
</html>
注意
三级行左右两侧的宽度是跟左右两侧边框的大小相关的
调整两侧边框大小就可以调整三角形的形状