Different Ways to Draw a Circle in a Webpage

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

Before nosotros start. If you lot want more complimentary content but in video format. Don't miss out on my Youtube channel where I publish weekly videos on FrontEnd coding.

https://www.youtube.com/user/Weibenfalk

----------

Are you new to web development and CSS? Have you lot always wondered how those nice shapes are made that you run into all over the internet? Wonder no more. Yous've come up to the right place.

Below I volition explain the very basics of creating shapes with CSS. There's a lot to tell you lot about this topic! Therefore I will non embrace all (far from all) tools and shapes but this should give yous a basic idea of how shapes are created with CSS.

Some shapes require more "gear up and tricks" than others. Creating shapes with CSS is normally a combination of using width, acme, meridian, right, left, border, bottom, transform and pseudo-elements similar :before and :later. Nosotros also have more modern CSS properties to create shapes with like shape-exterior and clip-path. I'll write nearly them beneath also.

CSS Shapes - The basic mode

By using a few tricks in CSS nosotros've e'er been able to create basic shapes like squares, circles, and triangles with regular CSS backdrop. Let's look at a few of them now.

Squares and rectangles

Squares and rectangles are probably the easiest shapes to attain. By default, a div is ever a square or a rectangle.

You lot set up the width and height equally shown in the beneath code. And then information technology's but a matter of giving the element a background color. You can have whatsoever other properties y'all want on the element.

                #square {     groundwork: lightblue;     width: 100px;     height: 100px; }              
square
A CSS square


Circles

Information technology's almost as easy to create a circumvolve. To create a circle we tin can set the border-radius on the element. This volition create curved corners on the element.

If we gear up it to 50% it will create a circle. If you lot set a different width and height we will get an oval instead.

                #circle {     groundwork: lightblue;     border-radius: 50%;     width: 100px;     height: 100px; }              
circle
A CSS Circle

Triangles

Triangles are a little trickier. We take to fix the borders on the chemical element to match a triangle. By setting the width and meridian to goose egg on the element, the actual width of the element is going to be the width of the border.

Keep in mind that the edge edges on an element are 45 degree diagonals to each other. That's why this method works to create a triangle. Past setting one of the borders to a solid color and the other borders to transparent it volition take the form of a triangle.

borders
CSS Borders have angled edges
                #triangle {     width: 0;     height: 0;     border-left: 40px solid transparent;     edge-right: 40px solid transparent;     border-bottom: 80px solid lightblue; }              
triangle
A CSS Triangle

If you want to accept a triangle/arrow pointing in another direction You tin change the edge values corresponding to what side yous want to be visible. Or you can rotate the element with the transform property if you lot want to be really fancy.

                                  #triangle {      width: 0;      superlative: 0;      border-peak: 40px solid transparent;      border-right: 80px solid lightblue;      border-bottom: 40px solid transparent;  }              
triangle2
Some other CSS Triangle

Alright – that'south an intro to bones shapes with CSS. There are probably an endless corporeality of shapes you tin think of to create. These are only the fundamentals, but with a little creativity and determination you lot tin achieve a lot with simply basic CSS properties.

In some cases, with more advanced shapes, it's likewise a expert idea to use the :later on and :before pseudo selectors. This is out of scope of this article though every bit my intention is to cover the basics to get you going.

Disadvantage

There is one big disadvantage with the above approach. For example, if you want your text to flow around and wrap your shape. A regular HTML div with background and borders to make up the shape won't allow that. The text will non adjust and catamenia effectually your shape. Instead information technology will menstruation around the div itself (which is a square or a rectangle).

Beneath is an analogy showing the triangle and how the text will flow.

textflow-bad

Luckily we have some modern CSS properties to use instead.

CSS Shapes - The other style

Nowadays nosotros take a belongings called shape-exterior to use in CSS. This property lets y'all ascertain a shape that the text will wrap/flow effectually.

Along with this property nosotros have some bones shapes:

inset()
circle()
ellipse()
polygon()

Hither's a tip: You can also utilize the clip-path property. You tin can create your shape with that in the same way, but it won't allow the text wrap around your shape like shape-outside does.

The element that we are going to apply the shape to with the shape-outside holding to has to be floated. Information technology also has to accept a defined width and height. That's really of import to know!

You can read more than about why here. Below is likewise a text that I've taken from the provided link to developer.mozilla.org.

The shape-outside property is specified using the values from the list beneath, which define the float area for float elements. The float area determines the shape around which inline content (float elements) wrap.

inset()

The inset() blazon can exist used to create a rectangle/foursquare with an optional offset for the wrapping text. It allows yous to provide values on how much you want your wrapping text to overlap the shape.

You tin specify the offset to be the same for all four directions like this: inset(20px). Or it tin exist individually set for each direction: inset(20px 5px 30px 10px).

You can utilize other units also to set the offset, for example, percent. The values correspond like this: inset(acme correct bottom left) .

Bank check out the below code instance. I've specified the inset values to be 20px at the top, 5px to the correct, 30px at the bottom and 10px to the left. If you lot desire your text to get around your foursquare instead you can just skip using inset() at all. Instead set the background on your div and specify the size as usual.

                                  #square {      float: left;      width: 100px;      height: 100px;      shape-outside: inset(20px 5px 30px 10px);      background: lightblue;  }              
inset
The text is start past the specified values. In this example 20px at top, 5px to the right, 30px at the bottom and 10 px to the left

It is also possible to requite inset() a 2nd value that specifies the border-radius of the inset. Like beneath:

                                  #square {      float: left;      width: 100px;      meridian: 100px;      shape-outside: inset(20px 5px 30px 10px round 50px);      background: lightblue;  }              
inset2
border-radius prepare to 50px on the inset

circumvolve()

In this i a circle is created using the shape-outside belongings. You also have to apply a clip-path with the corresponding property for the circle to testify up.

The clip-path property tin can have the same value as the shape-exterior property then nosotros can give information technology 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: circumvolve();     clip-path: circumvolve();     background: lightblue; }              
circle-shape-margin-1
Text flows around the shape!

In the above example, I don't specify the radius of the circle. This is because I want information technology to be as big as the div is (300px). If you desire to specify a unlike size for the circle yous can practice that.

The circle() takes two values. The first value is the radius and the 2d value is the position. These values will specify the centre of the circle.

In the beneath instance I've gear up the radius to 50%. Then I have shifted the center of the circle by thirty%. Note that the discussion "at" has to be used between the radius and position values.

I've too 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;       top: 150px;       margin: 20px;       shape-exterior: circumvolve(fifty% at 30%);       clip-path: circle(50% at 0%);       groundwork: lightblue;     }              
circle2

ellipse()

Ellipses work the same way as circles except that they create an oval. Yous tin define both the X value and the Y value, similar this: ellipse(25px  50px).

The same as a circle, it also takes a position value equally the last value.

                                  #ellipse {       float: left;       width: 150px;       peak: 150px;       margin: 20px;       shape-outside: ellipse(20% 50%);       clip-path: ellipse(20% 50%);       background: lightblue;     }              
ellipse

polygon()

A polygon is a shape with different vertices/coordinates divers. Below I create a "T" shape which is the start alphabetic character 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% xx%,         60% twenty%,         60% 100%,         40% 100%,         twoscore% twenty%,         0 twenty%       );       prune-path: polygon(         0 0,         100% 0,         100% 20%,         threescore% twenty%,         sixty% 100%,         40% 100%,         forty% 20%,         0 twenty%       );       background: lightblue;     }              
polygon_t

Images

Y'all tin can too use images with transparent backgrounds to create your shape. Like this round beautiful moon below.

This is a .png prototype with a transparent background.

moon
                <img src="src/moon.png" id="moon" />              
                #moon {       float: left;       width: 150px;       meridian: 150px;       shape-outside: url("./src/moon.png");     }              
moon2

And that's it! Thank you for reading.

Well-nigh 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 aqueduct. There's also a few premium courses out at that place on React and Gatsby. Experience free to visit me on these links:

Twitter — @weibenfalk,
Weibenfalk on Youtube,
Weibenfalk Courses Website.



Larn to code for complimentary. freeCodeCamp's open source curriculum has helped more than than 40,000 people get jobs as developers. Get started

petriehicest.blogspot.com

Source: https://www.freecodecamp.org/news/css-shapes-explained-how-to-draw-a-circle-triangle-and-more-using-pure-css/

0 Response to "Different Ways to Draw a Circle in a Webpage"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel