使用CSS绘制Triangle三角形


像上面图片中的几种三角形,不需要使用图片,使用 CSS 即可方便实现,甚至还能更改尺寸。

下面的 css 样式定义了上图中的各种样式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<style>
div {
float: left;
margin-right: 8px;
background: #b4b472;
}
#triangle-up {
width: 0;
height: 0;
border-left: 25px solid transparent;
border-right: 25px solid transparent;
border-bottom: 50px solid red;
}
#triangle-down {
width: 0;
height: 0;
border-left: 25px solid transparent;
border-right: 25px solid transparent;
border-top: 50px solid red;
}
#triangle-left {
width: 0;
height: 0;
border-bottom: 25px solid transparent;
border-top: 25px solid transparent;
border-right: 50px solid red;
}
#triangle-right {
width: 0;
height: 0;
border-bottom: 25px solid transparent;
border-top: 25px solid transparent;
border-left: 50px solid red;
}
#triangle-topleft {
width: 0;
height: 0;
border-top: 50px solid red;
border-right: 50px solid transparent;
}
#triangle-topright {
width: 0;
height: 0;
border-top: 50px solid red;
border-left: 50px solid transparent;
}
#triangle-bottomleft {
width: 0;
height: 0;
border-bottom: 50px solid red;
border-right: 50px solid transparent;
}
#triangle-bottomright {
width: 0;
height: 0;
border-bottom: 50px solid red;
border-left: 50px solid transparent;
}
#triangle-up-down {
width: 0;
height: 0;
border-left: 25px solid transparent;
border-right: 25px solid transparent;
border-bottom: 25px solid red;
border-top: 25px solid red;
}
#triangle-left-right {
width: 0;
height: 0;
border-top: 25px solid transparent;
border-bottom: 25px solid transparent;
border-left: 25px solid red;
border-right: 25px solid red;
}
</style>