How to Set the Shape of an element in CSS

CSS Shapes is a feature in CSS that allows you to create non-rectangular shapes for elements on a web page.

This can add visual interest and create more dynamic layouts.

In this CSS tutorial, we’ll explore how to set the shape of an element using CSS Shapes.


CSS Shapes properties

There are several properties in CSS that can be used to set the shape of an element.

These include:

  • shape-outside: defines the shape that an element should take outside of its regular rectangular bounds.
  • shape-margin: adds space around the shape defined by shape-outside.
  • shape-image-threshold: sets the transparency threshold for images used as shapes.

Using shape-outside

The shape-outside property is used to define the shape that an element should take outside of its regular rectangular bounds.

It can be set to one of several values, including:

  • none: the default value, which preserves the element’s rectangular shape.
  • url(image-url): an image file that should be used as the shape.
  • polygon(x1 y1, x2 y2, …): a polygon shape defined by a set of coordinates.
  • circle(x y r): a circular shape defined by a center point and a radius.
  • ellipse(x y rx ry): an elliptical shape defined by a center point and two radii.

Example:

Let’s say we have an image element with the class “shape” and we want to create a circular shape around it. We would use the following CSS:

.shape {
  shape-outside: circle(50% 50% 50%);
  shape-margin: 10px;
}

This creates a circle with a radius of 50% of the element’s width and height, with a margin of 10px around it.


Conclusion

CSS Shapes offers an exciting opportunity to create more dynamic and visually interesting layouts on a web page.

With the shape-outside property, you can create a variety of non-rectangular shapes for elements, including circles, polygons, and more.

It can be a powerful tool to make your website more distinctive and engaging.