Every div is a rectangle you can shape
You already know that a <div> is a box. What you might not know is that a box is basically a blank canvas. Give it a width, a height, and a background-color, and you have a colored rectangle. Round its corners and it can become a circle. Layer a few rectangles and circles on top of each other in the right spots, and you have drawn a picture.
That is the entire secret behind "CSS art." There is no special drawing tool. It is the same three ideas, used over and over:
- Shape —
border-radiusturns square corners into round ones (or a whole circle). - A sneaky shape — a strange trick with
bordercan turn a div into a triangle. - Layers —
positionandz-indexlet you stack shapes exactly where you want, in the right order.
The little smiling sun up in the header? Built from five divs: one circle for the sun, one bigger circle for the face, two small circles for eyes, and one curved rectangle for the smile. By the end of this tutorial, you will build a scene of your own.
Squares, circles, and border-radius
Every rectangle starts with four sharp corners. The CSS property border-radius rounds them. Give it a pixel value and it rounds the corners a little. Give it 50%, and something neat happens: a perfect square becomes a perfect circle, and a rectangle becomes an oval, because 50% of each side curves all the way to the middle.
That last box shows something useful: border-radius can take up to four values, one per corner, going clockwise from the top-left. border-radius: 20px 20px 0 0; rounds only the top two corners, which is perfect for things like a door or a coffee cup.
Rule of thumb: if the box is a perfect square (width equals height) and border-radius is 50%, you get a circle. If the box is a rectangle, 50% gives you an oval instead.
Shape builder
Drag the sliders to change the width, height, and border-radius of a div, and pick any color you like. Watch the shape change live, and watch the CSS underneath update to match. Try the presets, then try making something in between.
width: 140px; height: 140px; border-radius: 0%; background: #fb6340;
Triangles from nothing but borders
Circles come from border-radius, but what about a triangle, like a roof? CSS has no shape: triangle property. Instead, there is a classic trick that uses border against itself.
Give a div width: 0 and height: 0, so the box itself disappears. Then give it thick borders. Borders meet at an angle at each corner, so if you make the left and right borders transparent and only color the bottom border, all that is left visible is a wedge, and that wedge is a triangle:
.triangle { width: 0; height: 0; border-left: 40px solid transparent; border-right: 40px solid transparent; border-bottom: 70px solid #fb923c; }
Why it works: a 0×0 box has no room for a border rectangle, only four tapered wedges meeting at the center. Making two of those wedges transparent leaves the third one standing alone, shaped like a triangle. Change which side is colored (top, bottom, left, or right) to point the triangle in any direction.
Stacking shapes with position and z-index
A picture needs shapes to overlap on purpose, like eyes sitting on top of a face, or a roof sitting on top of a wall. Two CSS properties make that possible:
position: relativeon a container turns it into an "anchor." It does not move the container, it just gives its children something to measure from.position: absoluteon a child pulls it out of the normal flow and lets you place it precisely withtop,left,bottom, andright, measured from that anchor.z-indexdecides which shape wins when two shapes land on the same spot. A higherz-indexmeans "closer to the viewer," so it gets drawn on top.
Try it below. All three circles overlap in the same spots, but only one can be on top at a time. Click any circle to bring it to the front:
Click a circle to raise its z-index above the others.
Good to know: if you never set z-index at all, shapes still stack, later elements in your HTML simply draw on top of earlier ones. You only need z-index when you want to override that order, like making an eye stay on top even though it was written earlier in the file.
Build a house, step by step
Time to combine everything: a square body, a triangle roof, a rectangle door, and a small square window. Each step adds one more div. Press Run on each one to watch the house grow.
Step 1: the body
Start with a plain rectangle sitting on a strip of grass. Nothing fancy yet, just a positioned box.
<div class="scene">
<div class="ground"></div>
<div class="house-body"></div>
</div>
.scene { position: relative; height: 220px; }
.ground { position: absolute; bottom: 0; left: 0; right: 0; height: 46px; background: #8bc34a; }
.house-body { position: absolute; bottom: 46px; left: 50%; transform: translateX(-50%);
width: 150px; height: 100px; background: #f2c98d; border: 3px solid #caa06a; }
Step 2: add the roof
Now stack the triangle trick from earlier right on top of the body, using position: absolute to line it up perfectly.
<div class="scene">
<div class="ground"></div>
<div class="house-body"></div>
<div class="house-roof"></div>
</div>
.house-roof { position: absolute; bottom: 146px; left: 50%; transform: translateX(-50%);
width: 0; height: 0;
border-left: 85px solid transparent;
border-right: 85px solid transparent;
border-bottom: 60px solid #c0392b; }
Step 3: add a door
A door is just a skinny rectangle with rounded top corners, positioned at the bottom center of the body.
<div class="house-door"></div>
.house-door { position: absolute; bottom: 46px; left: 50%; transform: translateX(-50%);
width: 34px; height: 54px; background: #7a4a26;
border-radius: 8px 8px 0 0; }
Step 4: add a window and a sun
Last touch: a small square window with two thin divs crossing it like windowpanes, plus a sun in the sky to finish the scene.
<div class="house-window">
<div class="window-bar-h"></div>
<div class="window-bar-v"></div>
</div>
<div class="sun"></div>
.house-window { position: absolute; bottom: 96px; left: calc(50% - 55px);
width: 32px; height: 32px; background: #bfe3f5;
border: 2px solid #fff; border-radius: 4px; }
.window-bar-h { position: absolute; top: 50%; left: 0; right: 0; height: 2px; background: #fff; }
.window-bar-v { position: absolute; left: 50%; top: 0; bottom: 0; width: 2px; background: #fff; }
.sun { position: absolute; top: 16px; right: 24px; width: 46px; height: 46px;
border-radius: 50%; background: #ffd54a; }
Check yourself
1. What border-radius value turns a square div into a perfect circle?
Sample answer: border-radius: 50%; — as long as the div's width and height are equal, 50% rounds every corner all the way to the center, which makes a perfect circle.
2. Write the CSS for a small orange triangle that points to the right, using the border trick.
Sample answer: to point right, color the left border and make top and bottom transparent, so the wedge points away from the colored side.
.triangle-right { width: 0; height: 0; border-top: 30px solid transparent; border-bottom: 30px solid transparent; border-left: 50px solid orange; }
A full CSS art example
Here is the complete house scene as one self-contained file: a body, a roof from the border trick, a door, a window, a sun, and a ground strip, all positioned with position: absolute inside one position: relative container.
And here is exactly what that file draws, rendered live with real divs, not a screenshot:
5 shapes, 1 position: relative container, 0 images
Quick quiz
1. What border-radius value turns a square div into a circle?
2. What is the idea behind the CSS triangle trick?
3. When two shapes overlap, what mainly controls which one appears on top?
Keep learning
You just drew a picture using nothing but divs, borders, and a little math. Next, learn how to arrange whole pages the same way, with real layout tools like grids and spacing.