You’ve seen triangles everywhere: on tooltips, dropdowns and even on loading animations. Like it or not, these triangle shapes are quite important when trying to build some relationships between various UI elements.
Everything is a rectangle when working with HTML. But there are different ways to build triangle shapes in HTML, SVG & CSS for your designs and in this article, you’ll see how you can actually do that.
Assuming you already created your custom triangle image and you want to save a valuable HTTP request, then converting it into a Base64 string is the best solution.
- You can design it as you wish using shadows, gradients and strokes and then just encode it.
- You’ll need a tool like Photoshop or Gimp to edit it.
- For larger images, the resulting Base64 string can be quite huge.
- Older browsers like IE6/IE7 do not support this type of encoding.
- Easy to update color and size by adjusting some CSS values.
- This is a cross-browser solution.
- This technique consists of using borders, so you can’t add shadows, gradients and other CSS3 awesomeness.
- Keep in mind that IE6 doesn’t support transparent borders - in case you care about this matter anymore.
- If you’re using Firefox, be aware that CSS’s
transparentmay not be that transparent, especially on those diagonal borders.
- It’s a cross-browser technique.
- You can add a shadow using CSS
- Again, you can’t play too much with CSS3 here, excepting the use of
- It’s quite impossible to achieve pixel perfection across all browsers.
Here’s the official list of character entities: WHATWG’s named character references.
Besides using an entity, if you’re already using an icon font in your project, then you can simply use an arrow symbol from the available ones (in case there is one).
CSS rotated square
Basically, for this technique to work, you’ll need two blocks. But, it’s not mandatory to use two elements, so you can use an element with a pseudo-element for example.
Create a box. e.g.
100x100px- this one will contain the rotated block.
Rotate the contained block by 45deg to obtain the diamond shape.
Shift the diamond shape to the top then set
overflow: hiddento the wrapping block to show just the part we’re interested in.
There you go!
- This solution is not cross-browser due to using the CSS
Having the following
canvas element in your HTML file:
SVG (Scalable Vector Graphics)
This is how you can define an inline SVG triangle in your markup:
Then, just add some styles:
I must admit I haven’t played too much with the last two described in this article:
SVG. This is something I must improve in the future as they are very powerful. Anyways, the truth is that they were made for things greater than just trivial triangles :)
Let me know your thoughts about these ways to build triangles, what is the method you often use?