Circle
Every circle in CSS starts as a square. Round each corner by half its width with border-radius: 50%, and the curves meet in the middle.
- 1A square
width = height - 2Round the corners
border-radius: 25% - 3All the way
border-radius: 50%
.circle {
width: 120px;
height: 120px;
border-radius: 50%;
background: #F472B6;
}Square
The simplest shape, and the starting point for most of the others: a div with the same width and height, plus a background so you can see it.
- 1An empty div
no size yet - 2Give it a size
width + height - 3Fill it in
background
.square {
width: 100px;
height: 100px;
background: #57BC92;
}Triangle
There’s no triangle property in CSS, so we borrow one from borders. Shrink a box to zero, give it thick borders, and each border becomes a triangle. Then hide the ones you don’t need.
- 1Four thick borders
width: 0; height: 0 - 2Drop the top
no border-top - 3Hide the sides
transparent
- border-top
- border-right
- border-bottom (the triangle)
- border-left
.triangle {
width: 0;
height: 0;
border-left: 60px solid transparent;
border-right: 60px solid transparent;
border-bottom: 100px solid #7F9BE6;
}Pentagon
A pentagon is two shapes stacked: a trapezoid body made from a thick top border, and a triangle roof added with ::before.
- 1A trapezoid body
border-top + clear sides - 2A triangle roof
::before - 3Stack them
position: absolute
- the div: a trapezoid
- ::before: a triangle on top
.pentagon {
position: relative;
width: 54px;
border-style: solid;
border-width: 50px 18px 0;
border-color: #F5BF46 transparent;
}
.pentagon::before {
content: "";
position: absolute;
top: -85px;
left: -18px;
border-style: solid;
border-width: 0 45px 35px;
border-color: transparent transparent #F5BF46;
}Oval
An oval is a stretched circle. Use the same border-radius: 50%, but on a box that’s taller than it is wide.
- 1A tall rectangle
width < height - 2Round the corners
border-radius: 25% - 3All the way
border-radius: 50%
.oval {
width: 70px;
height: 120px;
border-radius: 50%;
background: #FE7043;
}Trapezoid
A trapezoid uses the triangle’s border trick on a box that still has some width. The bottom border keeps a flat top, and the transparent sides slant it in.
- 1Three borders
height: 0 - 2Hide the sides
transparent
- border-bottom (the trapezoid)
- border-left
- border-right
.trapezoid {
width: 80px;
height: 0;
border-bottom: 70px solid #2BA3A0;
border-left: 40px solid transparent;
border-right: 40px solid transparent;
}Octagon
Start with a square, then cover its four corners with small triangles the same color as the background. The corners disappear, leaving eight sides.
- 1A square
the div - 2Paint the corners
::before + ::after - 3Match the background
corners disappear
- the div: a square
- corner wedges, painted the background color
.octagon {
position: relative;
width: 100px;
height: 100px;
background: #B985EB;
}
.octagon::before,
.octagon::after {
content: "";
position: absolute;
left: 0;
width: 42px; /* 100px minus the two 29px borders */
border-left: 29px solid #C4EBD7; /* background color */
border-right: 29px solid #C4EBD7;
}
.octagon::before {
top: 0;
border-bottom: 29px solid #B985EB;
}
.octagon::after {
bottom: 0;
border-top: 29px solid #B985EB;
}Parallelogram
Draw a rectangle, then lean it over with transform: skew(). The top and bottom stay level while the sides tilt.
- 1A rectangle
the div - 2Lean it over
transform: skew(20deg)
.parallelogram {
width: 120px;
height: 70px;
background: #5088EA;
transform: skew(20deg);
}Rectangle
A square with one side stretched: any width and height that don’t match.
- 1A square
width = height - 2Stretch one side
width ≠ height
.rectangle {
width: 70px;
height: 120px;
background: #2D6144;
}Hexagon
A hexagon is a wide rectangle with a triangle on top and another underneath, added with ::before and ::after.
- 1A wide rectangle
the div - 2A triangle on top
::before - 3And one below
::after
- the div: a rectangle
- ::before: triangle on top
- ::after: triangle below
.hexagon {
position: relative;
width: 104px;
height: 60px;
background: #D91F1F;
}
.hexagon::before,
.hexagon::after {
content: "";
position: absolute;
left: 0;
border-left: 52px solid transparent;
border-right: 52px solid transparent;
}
.hexagon::before {
bottom: 100%;
border-bottom: 30px solid #D91F1F;
}
.hexagon::after {
top: 100%;
border-top: 30px solid #D91F1F;
}Star
The classic CSS star is three triangles: one wide triangle, rotated; a mirrored twin from ::after; and a tall point on top from ::before.
- 1One wide triangle
rotate(35deg) - 2A mirror twin
::after - 3A point on top
::before
- the div: a wide triangle
- ::after: its mirror
- ::before: the top point
.star {
position: relative;
width: 0;
height: 0;
margin: 50px 0;
border-left: 100px solid transparent;
border-right: 100px solid transparent;
border-bottom: 70px solid #8B87F5;
transform: rotate(35deg);
}
.star::before {
content: "";
position: absolute;
top: -45px;
left: -65px;
border-left: 30px solid transparent;
border-right: 30px solid transparent;
border-bottom: 80px solid #8B87F5;
transform: rotate(-35deg);
}
.star::after {
content: "";
position: absolute;
top: 3px;
left: -105px;
border-left: 100px solid transparent;
border-right: 100px solid transparent;
border-bottom: 70px solid #8B87F5;
transform: rotate(-70deg);
}Heart
Tilt a square 45 degrees so it points down, then add a circle to each top edge with ::before and ::after.
- 1A tilted square
rotate(-45deg) - 2Add a circle
::before - 3Add another
::after
- the div: a tilted square
- ::before: a circle
- ::after: a circle
.heart {
position: relative;
width: 60px;
height: 60px;
background: #6F1B31;
transform: rotate(-45deg);
}
.heart::before,
.heart::after {
content: "";
position: absolute;
width: 60px;
height: 60px;
border-radius: 50%;
background: #6F1B31;
}
.heart::before {
top: -30px;
left: 0;
}
.heart::after {
top: 0;
left: 30px;
}