Files
30-seconds-of-code/snippets/triangle.md
2018-03-03 15:05:35 +10:00

94 lines
2.1 KiB
Markdown

### Triangle
Creates a triangle shape with pure CSS.
#### HTML
```html
<div class="triangle"></div>
```
#### CSS
```css
.triangle {
width: 0;
height: 0;
border-top: 20px solid #333;
border-left: 20px solid transparent;
border-right: 20px solid transparent;
}
```
#### Demo
<div class="snippet-demo">
<div class="snippet-demo__triangles">
<div class="snippet-demo__triangle snippet-demo__triangle-1"></div>
<div class="snippet-demo__triangle snippet-demo__triangle-2"></div>
<div class="snippet-demo__triangle snippet-demo__triangle-3"></div>
<div class="snippet-demo__triangle snippet-demo__triangle-4"></div>
<div class="snippet-demo__triangle snippet-demo__triangle-5"></div>
</div>
</div>
<style>
.snippet-demo__triangles {
display: flex;
align-items: center;
}
.snippet-demo__triangle {
display: inline-block;
width: 0;
height: 0;
margin-right: 0.25rem;
}
.snippet-demo__triangle-1 {
border-top: 20px solid #333;
border-left: 20px solid transparent;
border-right: 20px solid transparent;
}
.snippet-demo__triangle-2 {
border-bottom: 20px solid #333;
border-left: 20px solid transparent;
border-right: 20px solid transparent;
}
.snippet-demo__triangle-3 {
border-left: 20px solid #333;
border-top: 20px solid transparent;
border-bottom: 20px solid transparent;
}
.snippet-demo__triangle-4 {
border-right: 20px solid #333;
border-top: 20px solid transparent;
border-bottom: 20px solid transparent;
}
.snippet-demo__triangle-5 {
border-top: 40px solid #333;
border-left: 15px solid transparent;
border-right: 15px solid transparent;
}
</style>
#### Explanation
[View this link for a detailed explanation.](https://stackoverflow.com/q/7073484)
The color of the border is the color of the triangle. The side the triangle tip points
corresponds to the opposite `border-*` property. For example, a color on `border-top`
means the arrow points downward.
Experiment with the `px` values to change the proportion of the triangle.
#### Browser support
<span class="snippet__support-note">✅ No caveats.</span>
<!-- tags: visual -->