CSS Shapes Explained: How to Draw a Circle, Triangle, and More Using Pure CSS

SOURCES : https://www.freecodecamp.org/

[feature] css


Cover photo by Delila Ziebart on Unsplash
Are you new to web development and CSS? Have you ever wondered how those nice shapes are made that you see all over the Internet? Wonder no more ... you've come to the right place.
Below I will explain the very basics of creating shapes with CSS. There's a lot to tell you about this topic! Therefore I will not cover all (far from all) tools and shapes but this should give you a basic idea of how shapes are created with CSS.
Some shapes require more "fix and tricks" than others. Creating shapes with CSS is usually a combination of using width, height, top, right, left, border, bottom, transform and pseudo-elements like :before and :after. We also have more modern CSS properties to create shapes with like shape-outside and clip-path. I'll write about them below also.

CSS Shapes - The basic way

By using a few tricks in CSS we've always been able to create basic shapes like squares, circles, and triangles with regular CSS properties.

Squares and rectangles

Squares and rectangles are probably the easiest shapes to achieve. By default, a div is always a square or a rectangle.
You set the width and height as shown in the below code. Then it's just a matter of giving the element a background color. You can have whatever other properties you want on the element.
#square {
    background: lightblue;
    width: 100px;
    height: 100px;
}
A CSS square


Circles

It's almost as elementary to create a circle. To create a circle we can set the border-radius on the element. This will create curved corners on the element.
If we set it to 50% it will create a circle. If you have different with and height we will get an oval instead.
#circle {
    background: lightblue;
    border-radius: 50%;
    width: 100px;
    height: 100px;
}
A CSS Circle

Triangles

Triangles are a little trickier. We have to set the borders on the element to match a triangle. By setting the width and height to zero on the element the actual width of the element is going to be the width of the border.
Keep in mind that the border edges on an element are 45 degrees diagonals to each other. That's why this method works to create a triangle. By setting one of the borders to a solid color and the other borders to transparent it will take the form of a triangle.
CSS Borders have angled edges
#triangle {
    width: 0;
    height: 0;
    border-left: 40px solid transparent;
    border-right: 40px solid transparent;
    border-bottom: 80px solid lightblue;
}
A CSS Triangle
If you want to have a triangle/arrow pointing in another direction You can change the border values corresponding to what side you want to be visible. Or you can rotate the element with the transform property if you want to be really fancy.
 #triangle {
     width: 0;
     height: 0;
     border-top: 40px solid transparent;
     border-right: 80px solid lightblue;
     border-bottom: 40px solid transparent;
 }
Another CSS Triangle
Alright ... that's basic shapes with CSS. There's probably an endless amount of shapes you can think of to create. These are just the fundamentals, but with a little creativity and determination you can achieve a lot with just basic CSS properties.
In some cases, with more advanced shapes, it's also a good idea to use the :after and :before pseudo selectors.

There is one big disadvantage with this approach though - if you, for example, want your text to flow around and wrap your shape. This is just regular HTML elements that we style and the text will not adapt and flow around your shape.
Below is an illustration showing the triangle and how the text will flow.
Luckily we have some modern CSS properties to use instead.

CSS Shapes - The other way

Nowadays we have a property called shape-outside to use in CSS. This property lets you define a shape that the text will wrap/flow around. To come along with this property we have some basic shapes:

inset()
circle()
ellipse()
polygon()
Tip!
You can also use the clip-path property. You can create your shape with that in the same way, but it won't let the text wrap around your shape like shape-outside does.
The element that we are going to apply the shape to has to be floated and it has to have a defined width and height. That's really important to know!

inset()

The inset() type can be used to create a rectangle/square with an offset for the wrapping text. It allows you to specify how much you want your wrapping text to overlap the shape.
You can specify the offset to be the same for all four directions like this: inset(20px). Or it can be individually set for each direction: inset(20px 5px 30px 10px).
You can use other units also to set the offset, for example, percent. The values correspond like this: inset(top right bottom left).
Check out the below code example:
 #square {
     float: left;
     width: 100px;
     height: 100px;
     shape-outside: inset(20px 5px 30px 10px);
     background: lightblue;
 }
The text is offset by the specified values. In this case 20px at top, 5px to the right, 30px at the bottom and 10 px to the left
It is also possible to give inset() a second value that specifies the border-radius of the inset. Like below:
 #square {
     float: left;
     width: 100px;
     height: 100px;
     shape-outside: inset(20px 5px 30px 10px round 50px);
     background: lightblue;
 }
border-radius set to 50px on the inset

circle()

In this one a circle is created using the shape-outside property. You also have to apply a clip-path with the corresponding property for the circle to show up.
The clip-path property can take the same value as the shape-outside property so we can give it the standard circle() shape that we used for shape-outside. Also, note that I've applied a 20px margin on the element here to give the text some space.
#circle {
    float: left;
    width: 300px;
    height: 300px;
    margin: 20px;
    shape-outside: circle();
    clip-path: circle();
    background: lightblue;
}
Text flows around the shape!
In the above example, I don't specify the radius of the circle. This is because I want it to be as big as the div is (300px). If you want to specify your own size of the circle you can do that.
The circle() takes two values. The first value is the radius and the second value is the position. These values will specify the center of the circle.
In the below example I've set the radius to 50%. Then I have shifted the center of the circle by 30%. Note that the word "at" has to be used between the radius and position values.
I've also specified another position value on the clip-path. This will clip the circle in half as I move the position to zero.
 #circle {
      float: left;
      width: 150px;
      height: 150px;
      margin: 20px;
      shape-outside: circle(50% at 30%);
      clip-path: circle(50% at 0%);
      background: lightblue;
    }

ellipse()

Ellipses work the same way as circles except that they create an oval. You can define both the X value and the Y value, like this: ellipse(25px  50px).
The same as a circle, it also takes a position value as the last value.
   #ellipse {
      float: left;
      width: 150px;
      height: 150px;
      margin: 20px;
      shape-outside: ellipse(20% 50%);
      clip-path: ellipse(20% 50%);
      background: lightblue;
    }

polygon()

A polygon is a shape with different vertices/coordinates defined. Below I create a "T" shape which is the first letter in my name. I start from the coordinates 0,0 and move from left to right to create the "T" shape.
#polygon {
      float: left;
      width: 150px;
      height: 150px;
      margin: 0 20px;
      shape-outside: polygon(
        0 0,
        100% 0,
        100% 20%,
        60% 20%,
        60% 100%,
        40% 100%,
        40% 20%,
        0 20%
      );
      clip-path: polygon(
        0 0,
        100% 0,
        100% 20%,
        60% 20%,
        60% 100%,
        40% 100%,
        40% 20%,
        0 20%
      );
      background: lightblue;
    }

Images

You can also use images with transparent backgrounds to create your shape. Like this round beautiful moon below.
This is a .png image with a transparent background.
<img src="src/moon.png" id="moon" />
#moon {
      float: left;
      width: 150px;
      height: 150px;
      shape-outside: url("./src/moon.png");
    }
And that's it! Thank you for reading.

About the author of this article

My name is Thomas Weibenfalk and I'm a developer from Sweden. I regularly create free tutorials on my Youtube channel. There's also a few premium courses out there on React and Gatsby. Feel free to visit me on these links:
Twitter — @weibenfalk,
Weibenfalk on Youtube,
Weibenfalk Courses Website.
Name

Ayurvedic,1,Banner,8,cinema,70,Panchangam,9,spiritual,77,sports,19,Technology,12,Tourism,5,இன்றைய ராசிபலன்,64,கவிதைகள்,32,குருப்பெயர்ச்சி,5,திருக்கதைகள்,10,நிகழ்வுகள்,250,வேலைவாய்ப்புக்கள்,11,
ltr
item
Free Tech Daily: CSS Shapes Explained: How to Draw a Circle, Triangle, and More Using Pure CSS
CSS Shapes Explained: How to Draw a Circle, Triangle, and More Using Pure CSS
Are you new to web development and CSS? Have you ever wondered how those nice shapes are made that you see all over the Internet? Wonder no more ... you've come to the right place.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhBhbesm6uT_G50X5nGTh_kyT7NG-MY3r_I471lzWym39IfwUseZLp3O3EmT9VP51KWIm4hYkajPm7El3w2wbzv-l6wwxG4YcQJKvZjVl9oEADV6CZTtagXDJncxOMhdEWLwugFyvmzqo_x/s640/delila-ziebart-b0GSCFJ-Gzg-unsplash.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhBhbesm6uT_G50X5nGTh_kyT7NG-MY3r_I471lzWym39IfwUseZLp3O3EmT9VP51KWIm4hYkajPm7El3w2wbzv-l6wwxG4YcQJKvZjVl9oEADV6CZTtagXDJncxOMhdEWLwugFyvmzqo_x/s72-c/delila-ziebart-b0GSCFJ-Gzg-unsplash.png
Free Tech Daily
https://freetechdaily.blogspot.com/2020/02/css-shapes-explained-how-to-draw-circle.html
https://freetechdaily.blogspot.com/
http://freetechdaily.blogspot.com/
http://freetechdaily.blogspot.com/2020/02/css-shapes-explained-how-to-draw-circle.html
true
3500669192308647346
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy