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?
Lets say we have the following div:
Now Edit the CSS step-by-step, so you will get clear idea what is happening around
STEP 1: JSfiddle Link:
This is a simple div. With a very simple CSS. So a layman can understand. Div has dimensions 150 x 150 pixels with the border 50 pixels. The image is attached:
STEP 2: JSfiddle Link:
Now I just changed the border-color of all 4 sides. The image is attached.
STEP:3 JSfiddle Link:
Now I just changed the height & width of div from 150 pixels to zero. The image is attached
STEP 4: JSfiddle:
Now I have made all the borders transparent apart from the bottom border. The image is attached below.
STEP 5: JSfiddle Link:
Now I just changed the background color to white. The image is attached.
Hence we got the triangle we needed.
Start with a basic square and borders. Each border will be given a different color so we can tell them apart:
which gives you this:
But there's no need for the top border, so set its width to
0px
. Now our border-bottom of200px
will make our triangle 200px tall.and we will get this:
Then to hide the two side triangles, set the border-color to transparent. Since the top-border has been effectively deleted, we can set the border-top-color to transparent as well.
finally we get this:
Almost all the answers focus on the triangle built using border so I am going to elaborate the
linear-gradient
method (as started in the answer of @lima_fil).Using a degree value like
45°
will force us to respect a specific ratio ofheight/width
in order to obtain the triangle we want and this won't be responsive:Instead of doing this we should consider predefined values of direction like
to bottom
,to top
, etc. In this case we can obtain any kind of triangle shape while keeping it responsive.1) Rectangle triangle
To obtain such triangle we need one linear-gradient and a diagonal direction like
to bottom right
,to top left
,to bottom left
, etc2) isosceles triangle
For this one we will need 2 linear-gradient like above and each one will take half the width (or the height). It's like we create a mirror image of the first triangle.
3) equilateral triangle
This one is a bit tricky to handle as we need to keep a relation between the height and width of the gradient. We will have the same triangle as above but we will make the calculation more complex in order to transform the isosceles triangle to an equilateral one.
To make it easy, we will consider that the width of our div is known and the height is big enough to be able to draw our triangle inside (
height >= width
).We have our two gradient
g1
andg2
, the blue line is the width of thediv
w
and each gradient will have 50% of it (w/2
) and each side of the triangle sould be equal tow
. The green line is the height of both gradienthg
and we can easily obtain the formula below:(w/2)² + hg² = w²
--->hg = (sqrt(3)/2) * w
--->hg = 0.866 * w
We can rely on
calc()
in order to do our calculation and to obtain the needed result:Another way is to control the height of div and keep the syntax of gradient easy:
4) Random triangle
To obtain a random triangle, it's easy as we simply need to remove the condition of 50% of each one BUT we should keep two condition (both should have the same height and the sum of both width should be 100%).
But what if we want to define a value for each side? We simply need to do calculation again!
Let's define
hg1
andhg2
as the height of our gradient (both are equal to the red line) thenwg1
andwg2
as the width of our gradient (wg1 + wg2 = a
). I will not going to detail the calculation but at then end we will have:Now we have reached the limit of CSS as even with
calc()
we won't be able to implement this so we simply need to gather the final result manually and use them as fixed size:Bonus
We should not forget that we can also apply rotation and/or skew and we have more option to obtain more triangle:
And of course we should keep in mind the SVG solution which can be more suitable in some situation:
And now something completely different...
Instead of using css tricks don't forget about solutions as simple as html entities:
Result:
▲
See: What are the HTML entities for up and down triangles?
CSS
clip-path
This is something I feel this question has missed;
clip-path
clip-path
will use the element itself rather than its borders to cut the shape you specify in its parameters. It uses a super simple percentage based co-ordinate system which makes editing it very easy and means you can pick it up and create weird and wonderful shapes in a matter of minutes.Triangle Shape Example
Downside
It does have a major downside at the moment, one being it's major lack of support, only really being covered within
-webkit-
browsers and having no support on IE and only very partial in FireFox.Resources
Here are some useful resources and material to help better understand
clip-path
and also start creating your own.clip-path
generatorclip-path
documentationclip-path
Browser Supporthere is another fiddle:
https://jsfiddle.net/qdhvdb17/