Canddi

CSS: Beyond Basic Shapes

CSS can create circles, ellipses, triangles and compound shapes without image files. The examples below use rounded corners, coloured borders and pseudo-elements. They are decorative shapes: keep meaningful labels in HTML and use real buttons or links for interactive controls.

Each example includes matching HTML and CSS. Add the CSS to your stylesheet, then place the HTML where you want the shape. These fixed pixel sizes are useful for learning; test the surrounding layout on narrow screens before using them in a design.

Browser-rendered CSS circle, ellipse, three triangles, trapezoid, six-point star and hexagon.
Rendered from the examples below. Open the image for a larger view.

Start with a predictable box model

Use this shared rule with every example. It keeps the dimensions consistent even if your project applies border-box globally.

.shape, .shape::before, .shape::after {
  box-sizing: content-box;
}

Circle

Equal width and height with a 50% corner radius produce a circle. Unprefixed border-radius is sufficient for modern browsers.

<div class="shape circle" aria-hidden="true"></div>
.circle {
  width: 100px;
  height: 100px;
  background: lightgreen;
  border-radius: 50%;
}

Ellipse

Different width and height produce an ellipse. The 50% radius follows both dimensions.

<div class="shape ellipse" aria-hidden="true"></div>
.ellipse {
  width: 220px;
  height: 120px;
  background: lightgreen;
  border-radius: 50%;
}

Up triangle

A zero-sized content box leaves the borders visible. Transparent side borders and a coloured bottom border create the upward triangle.

<div class="shape triangle-up" aria-hidden="true"></div>
.triangle-up {
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-bottom: 100px solid lightgreen;
}

Down triangle

Changing the coloured border to the top reverses the direction.

<div class="shape triangle-down" aria-hidden="true"></div>
.triangle-down {
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-top: 100px solid lightgreen;
}

Right-angled triangle

One coloured border and one transparent border produce a right-angled triangle.

<div class="shape triangle-right" aria-hidden="true"></div>
.triangle-right {
  width: 0;
  height: 0;
  border-bottom: 100px solid lightgreen;
  border-right: 100px solid transparent;
}

Trapezoid

Unlike the triangles, this shape has a non-zero content width. The visible result is 200px wide at its base: 100px of content plus two 50px side borders.

<div class="shape trapezoid" aria-hidden="true"></div>
.trapezoid {
  width: 100px;
  height: 0;
  border-bottom: 100px solid lightgreen;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
}

Six-point star

Two overlapping triangles create a six-point star, rather than a five-point star. The empty content value makes the pseudo-element render.

<div class="shape star" aria-hidden="true"></div>
.star {
  position: relative;
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-bottom: 100px solid gold;
}
.star::after {
  content: "";
  position: absolute;
  top: 30px;
  left: -50px;
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-top: 100px solid gold;
}

Hexagon

A rectangle and two triangular pseudo-elements form this hexagon. The top and bottom tips extend 25px beyond the rectangle, so leave room around it.

<div class="shape hexagon" aria-hidden="true"></div>
.hexagon {
  position: relative;
  width: 100px;
  height: 60px;
  background: lightgreen;
}
.hexagon::before, .hexagon::after {
  content: "";
  position: absolute;
  left: 0;
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
}
.hexagon::before {
  top: -25px;
  border-bottom: 25px solid lightgreen;
}
.hexagon::after {
  bottom: -25px;
  border-top: 25px solid lightgreen;
}

Check the result in your page

  • Keep the class names in the HTML and CSS identical.
  • Leave space for the star and hexagon pseudo-elements; they extend outside the base box and do not reserve extra space in normal flow.
  • If a tip is cut off, check whether a parent clips overflow.
  • The aria-hidden attribute is appropriate here because these empty shapes are decorative. Put any essential meaning in visible text.
  • Compare the output with the reference image, then test at your intended desktop and mobile sizes.

For the underlying properties, see MDN on border-radius and the ::after pseudo-element.

Build This Skill

Practise styling and page layout with our HTML5 and CSS3 courses.