Skip to content Skip to sidebar Skip to footer

Css Draw a Circle Around Image

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

Earlier nosotros outset. If you want more costless 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 spider web evolution and CSS? Have you ever wondered how those nice shapes are made that yous run across all over the internet? Wonder no more than. Y'all've come up to the right place.

Below I will explain the very basics of creating shapes with CSS. There's a lot to tell you almost 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 "prepare and tricks" than others. Creating shapes with CSS is ordinarily a combination of using width, height, top, right, left, border, bottom, transform and pseudo-elements like :before and :after. We also have more than modern CSS properties to create shapes with like shape-exterior and prune-path. I'll write nearly them beneath also.

CSS Shapes - The bones 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 backdrop. Let's look at a few of them now.

Squares and rectangles

Squares and rectangles are probably the easiest shapes to accomplish. By default, a div is e'er a square or a rectangle.

You set the width and height equally shown in the below lawmaking. Then it'southward just a matter of giving the element a background color. You can have whatsoever other properties you want on the element.

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


Circles

It'due south most as easy to create a circle. To create a circle we tin set the edge-radius on the element. This will create curved corners on the chemical element.

If we prepare it to 50% it will create a circumvolve. If yous prepare a different width and height we volition get an oval instead.

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

Triangles

Triangles are a footling trickier. We take to set the borders on the chemical element to match a triangle. By setting the width and height to zero on the element, the bodily 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 caste diagonals to each other. That'south why this method works to create a triangle. By setting ane of the borders to a solid color and the other borders to transparent it will take the class of a triangle.

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

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

                                  #triangle {      width: 0;      summit: 0;      border-top: 40px solid transparent;      border-right: 80px solid lightblue;      border-bottom: 40px solid transparent;  }              
triangle2
Another CSS Triangle

Alright – that's an intro to basic shapes with CSS. In that location are probably an endless corporeality of shapes you can recall of to create. These are just the fundamentals, but with a little creativity and determination yous tin can achieve a lot with just basic CSS properties.

In some cases, with more advanced shapes, information technology's as well a good thought to use the :after and :before pseudo selectors. This is out of scope of this article though as my intention is to encompass the basics to get you going.

Disadvantage

There is i large 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 permit that. The text will non adapt and flow around your shape. Instead it will flow effectually the div itself (which is a foursquare or a rectangle).

Below is an illustration showing the triangle and how the text will menstruation.

textflow-bad

Luckily we have some modernistic CSS backdrop to utilise instead.

CSS Shapes - The other manner

Nowadays we take a holding called shape-outside to use in CSS. This property lets you lot ascertain a shape that the text will wrap/catamenia around.

Along with this belongings we have some basic shapes:

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

Here's a tip: You can besides use the prune-path property. Yous can create your shape with that in the aforementioned way, but it won't let the text wrap around your shape similar shape-outside does.

The chemical element that we are going to apply the shape to with the shape-outside property to has to be floated. It also has to have a defined width and height. That'due south really important to know!

You tin read more virtually why here. Below is besides 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 below, which ascertain the float expanse for float elements. The float expanse determines the shape effectually which inline content (float elements) wrap.

inset()

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

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

Y'all can utilise other units too to set the offset, for example, percent. The values correspond like this: inset(meridian right lesser left) .

Check out the beneath code example. 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 desire your text to go around your foursquare instead you can just skip using inset() at all. Instead set the background on your div and specify the size every bit usual.

                                  #square {      float: left;      width: 100px;      summit: 100px;      shape-exterior: inset(20px 5px 30px 10px);      background: lightblue;  }              
inset
The text is offset by the specified values. In this instance 20px at pinnacle, 5px to the right, 30px at the bottom and 10 px to the left

Information technology is too possible to give inset() a 2d value that specifies the border-radius of the inset. Similar below:

                                  #foursquare {      float: left;      width: 100px;      height: 100px;      shape-outside: inset(20px 5px 30px 10px circular 50px);      background: lightblue;  }              
inset2
edge-radius fix to 50px on the inset

circumvolve()

In this 1 a circle is created using the shape-outside property. You likewise take to apply a clip-path with the corresponding property for the circle to show up.

The clip-path property can accept the aforementioned value as the shape-outside property then we tin can requite it the standard circle() shape that we used for shape-outside. Also, annotation that I've applied a 20px margin on the chemical element here to give the text some space.

                #circle {     float: left;     width: 300px;     height: 300px;     margin: 20px;     shape-outside: circle();     prune-path: circle();     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 it to be as large as the div is (300px). If you want to specify a different size for the circle yous can do that.

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

In the below example I've fix the radius to 50%. So I have shifted the center of the circle by thirty%. Note that the word "at" has to exist used betwixt the radius and position values.

I've also specified another position value on the prune-path. This will clip the circle in half as I motion the position to zero.

                                  #circle {       float: left;       width: 150px;       height: 150px;       margin: 20px;       shape-exterior: circle(50% at 30%);       clip-path: circle(fifty% at 0%);       groundwork: lightblue;     }              
circle2

ellipse()

Ellipses work the same way equally circles except that they create an oval. You lot can define both the X value and the Y value, like this: ellipse(25px  50px).

The same as a circle, it too takes a position value every bit the final value.

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

polygon()

A polygon is a shape with unlike vertices/coordinates defined. Below I create a "T" shape which is the outset 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-exterior: polygon(         0 0,         100% 0,         100% twenty%,         60% 20%,         sixty% 100%,         twoscore% 100%,         xl% 20%,         0 20%       );       prune-path: polygon(         0 0,         100% 0,         100% 20%,         lx% xx%,         threescore% 100%,         40% 100%,         40% 20%,         0 twenty%       );       groundwork: lightblue;     }              
polygon_t

Images

You can also employ images with transparent backgrounds to create your shape. Similar this round cute moon below.

This is a .png paradigm with a transparent background.

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

And that's it! Thank you for reading.

About the author of this article

My proper noun is Thomas Weibenfalk and I'm a programmer from Sweden. I regularly create free tutorials on my Youtube channel. In that location'southward also a few premium courses out there on React and Gatsby. Feel gratuitous to visit me on these links:

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



Learn to code for free. freeCodeCamp's open source curriculum has helped more twoscore,000 people get jobs as developers. Go started

brinkerhoffhiplace.blogspot.com

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

Enregistrer un commentaire for "Css Draw a Circle Around Image"