Just realized I have yet to see this.
But can not believe it isn't possible.
I'm looking to draw a triangle in pure CSS/HTML. An equilateral if possible.
Clarification:
I don't wish to use an image to achieve this.
You would need to be able to put content inside the div.
One Solution
Diagonals are not easy. One solution is to overlay pseudo-elements to create the border, assuming you are dealing with solid background colors. Then you have to position the content to make it look nice. You could even do some text wrapping.
Here is a basic example using this code:
CSS & HTML Respectively
Here are a few different approaches for creating the equilateral triangle shape using CSS. Creation of diagonals is still not any easier but now the shape can at-least have a transparent background even when the body has a gradient (or) an image as its background.
Option 1: Using Pseudo-elements and Skew Transforms
In this method we use a couple of pseudo-elements and skew them in opposite directions (inward) to create the diagonal lines whereas the line at the bottom is produced using a
border-bottom
on the parent. We can also produce trapezoids using this approach.Cons: This approach would not work if the
body
background andshape
background are different and thebody
background is not a solid color.Here is a variation of Option 1 which would work when the background of the
body
and that of the shape are different and thebody
background is a solid color.Here is another variation of Option 1 which supports gradient background for both inside and outside the triangle shape.
Screenshot:
Triangles with different angles can be easily created by modifying the
skew
angle and the height of the parentdiv
. But, as we are usingskew
the borders tend to become thinner as the skew angle approaches 90deg (or -90deg) but that shouldn't be too big a problem because with such high angles you can barely have fit any text inside.Option 2: Using Linear Gradients
In this method, we use a couple of angled
linear-gradient
backgrounds (each of which are 50% width of the container) and slant them in opposite directions to produce the diagonal lines.Cons: Angled gradients are known for producing jagged lines.