There're plenty of different CSS shapes over at CSS Tricks - Shapes of CSS and I'm particularly puzzled with a triangle:
#triangle-up {
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom: 100px solid red;
}
<div id="triangle-up"></div>
How and why does it work?
OK, this triangle will get created because of the way that borders of the elements work together in HTML and CSS...
As we usually use 1 or 2px borders, we never notice that borders make a 45° angles to each others with the same width and if the width changes, the angle degree get changed as well, run the CSS code I created below:
Then in the next step, we don't have any width or height, something like this:
And now we make the left and right borders invisible to make our desirable triangle as below:
If you not willing to run the snippet to see the steps, I've created an image sequence to have a look at all steps in one image:
Here is an animation in JSFiddle I created for demonstration.
Also see snippet below.
This is an Animated GIF made from a Screencast
Random version
All at once version
Taking it one step further, using css based on this I added arrows to my back and next buttons (yes I know its not 100% cross-browser, but slick none the less).
The borders use an angled edge where they intersect (45° angle with equal width borders, but changing the border widths can skew the angle).
jsFiddle.
By hiding certain borders, you can get the triangle effect (as you can see above by making the different portions different colours).
transparent
is often used as an edge colour to achieve the triangle shape.Others have already explained this well. Let me give you an animation which will explain this quickly: http://codepen.io/chriscoyier/pen/lotjh
Here is some code for you to play with and learn the concepts.
HTML:
CSS:
Play with this and see what happens. Set height and width to zero. Then remove top border and make left and right transparent, or just look at the code below to make a css triangle: