Advertisement
  1. Web Design
  2. HTML/CSS
  3. CSS

Taking CSS Shapes to the Next Level

Scroll to top

In the previous tutorial, we took a glimpse at creating CSS Shapes, allowing us to define the true shape of an element. We touched on the basics; creating a circle and wrapping a few lines of text around it. Let’s now explore more complex shapes, making our page layouts richer and less boxy.

Creating a Polygon

Beyond simple shapes like circles and rectangles we can also create polygons; shapes consisting of multiple (typically five or more) sides, such as a pentagon or a hexagon. To create one, we use the CSS polygon() function which takes multiple comma-separated value defining the shape coordinates:

1
.shape {
2
   float: left;
3
   background-size: contain;
4
   background-repeat: no-repeat;
5
   margin: 30px;
6
   width: 265px;
7
   height: 265px;
8
   shape-margin: 20px;
9
}
10
.pentagon {
11
   background-image: url('../img/pentagon.svg');
12
   shape-outside: polygon(32px 126px, 163px 32px, 293px 127px, 244px 282px, 82px 281px);
13
}

With the above code, we have added five coordinates, thereby generating a pentagon shape. Adding another coordinate to the function will turn the shape into a hexagon, one more for a heptagon, another for an octagon, and so on. 

Creating a polygon shape manually is not as easy as creating a circle or an ellipse, especially if the polygon is complex and involves dozens of coordinates. Luckily, there is a handy tool which makes creating CSS Shapes more intuitive.

Using CSS Shapes Editor

CSS Shapes Editor is a Chrome extension. Once you have installed it, it adds a new tab named Shapes in DevTools under the Elements tab, inline with the other sub-tabs (Styles, Computer, etc.)

The Shapes tab contains a shape-outside property paired with a plus + sign to help us add CSS Shape functions. Let’s select the polygon() and the element on the page, then we can start drawing the shape right in the browser just like a graphics application.

As we draw, the browser extension will update our polygon function with the respective coordinates of the shape drawn, simultaneously adding it as an inline style to the element. Once done, copy the generated code and paste it into your stylesheet. 

generated polygon coordinatesgenerated polygon coordinatesgenerated polygon coordinates
Generated polygon coordinates

Using this same technique we can easily draw and apply any form or shape on our webpage, for instance:

Image from How to Add Drama to a Rainy Scene With Adobe Photoshop

Extracting the Image Shape

Another possible value we can use in the shape-outside propertyis the url() function; the same function that we use elsewhere in CSS to add a background image. In this case the url() function enables the shape-outside property to define an element shape based on the image.

You might choose to use the url() function instead of polygon() when you have a particularly complex graphic, with many curves and corners and you want your content to wrap around it smoothly. Or, when dealing with user-generated content in which defining the shape could be tricky.

We’re going to use this macaw parrot from Unsplash. First, you will need to create a version with transparency, for instance:

the shape of our macawthe shape of our macawthe shape of our macaw
The shape of our macaw parrot

The original parrot image is added to the container as a background image:

1
.container {
2
   background-repeat: no-repeat;
3
   background-image: url('../img/parrot.png');
4
}

Transparency Threshold

For the shape itself, we’ll add that to another element within the container, and this time we’ll use another CSS Shape property called shape-image-threshold.

1
.shape {
2
   shape-margin: 15px;
3
   shape-outside: url('../img/parrot-shape.png');
4
   shape-image-threshold: 0;
5
}

This particular property sets the threshold of minimum transparency that defines the image shape; it takes a number between 0 to 1 as the value. So if you have slightly transparent image you may want to set it at 0.8 or 0.5. We’ve set our value to 0 since the area surrounding the parrot shape is completely transparent.

The outcome should look something like:

Providing Fallback

Browser support for CSS Shapes is decent; though don’t expect much from Firefox, Internet, Explorer or Edge. Also, responding to Stephen’s question in the previous tutorial, we have several options.

“This is cool, but genuine question: what is the fallback for IE/FF? It seems like going all-in with a shape layout would end up in some weird places for a lot of users.” – Stephen Bateman

First, we can use Modernizr. Modernizr is a reliable JavaScript library for browser feature testing. Ensure you have included the CSS Shapes and the Add CSS classes option when downloading the library. Then you can specify the layout styles when CSS Shape is not supported, as follows:

1
/**

2
 * When the browser does not support "CSS Shape".

3
 */
4
.no-shapes .shape {
5
   shape-outside: polygon();
6
}
7
8
/**

9
 * Styles when the browser supports "CSS Shape".

10
 */
11
.shapes .shape {
12
13
}

If loading Modernizr seems too much, since you only need to test whether the browser has CSS enabled, you can add the following JavaScript lines.

1
if ('undefined' === typeof document.querySelector('.shape').style.shapteOutside) {
2
   document.body.classList.add('no-css-shape');
3
}

Each property in CSS comes with its equivalent under the JavaScript Style object, which allows us to add style dynamically trough JavaScript. This code detects whether the browser has the shapeOutside method, then where that’s not the case we add the no-css-shape class which we can utilize to add the fallback styles.

1
.shape {
2
   shape-outside: polygon();
3
}
4
.no-shapes .shape {
5
6
}

Lastly, we can use the CSS @support syntax in the stylesheet, for example.

1
@supports (shape-outside: circle()) {
2
   .shape {
3
      shape-outside: circle();
4
   }
5
}

It’s up to you to decide which approach best fits your project.

Wrapping Up

In this tutorial, we discovered how to use the polygon() function in CSS Shape and learned how we can draw polygons easily using a Chrome extension. We also learned how to leverage an image to draw shapes for us; particularly useful with overly complex shapes. Lastly, despite the CSS Shapes reasonable browser support, we talked about using graceful alternatives for unsupported browsers.

Demos

Useful Links

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.