Draw a Circle Around Image

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

Earlier we start. If you lot want more than free content just 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 ever wondered how those nice shapes are made that you run across all over the internet? Wonder no more. You've come to the right identify.

Below I volition explain the very basics of creating shapes with CSS. In that location's a lot to tell you near this topic! Therefore I will not 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 "fix and tricks" than others. Creating shapes with CSS is ordinarily a combination of using width, height, elevation, right, left, border, lesser, transform and pseudo-elements similar :before and :after. We too accept more modernistic CSS properties to create shapes with similar shape-outside and clip-path. I'll write most them below also.

CSS Shapes - The basic way

By using a few tricks in CSS we've e'er been able to create basic shapes like squares, circles, and triangles with regular CSS properties. Permit'due south expect at a few of them now.

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 equally shown in the below code. And so it's just a thing of giving the element a background color. You can accept any other properties you want on the element.

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


Circles

Information technology'due south well-nigh every bit easy to create a circle. To create a circle we tin gear up the border-radius on the element. This will create curved corners on the element.

If nosotros set it to 50% information technology will create a circle. If you set a different width and acme nosotros will get an oval instead.

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

Triangles

Triangles are a footling trickier. Nosotros have to set the borders on the element to match a triangle. Past setting the width and top to null on the chemical element, the actual width of the element is going to exist the width of the border.

Proceed in heed that the border edges on an element are 45 degree diagonals to each other. That's why this method works to create a triangle. Past setting 1 of the borders to a solid color and the other borders to transparent it will take the form of a triangle.

borders
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; }              
triangle
A CSS Triangle

If you desire to take a triangle/arrow pointing in another direction You tin can change the border values corresponding to what side y'all want to exist visible. Or yous can rotate the element with the transform property if you want to exist actually fancy.

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

Alright – that's an intro to basic shapes with CSS. In that location are probably an endless amount of shapes you can think of to create. These are just the fundamentals, just with a fiddling creativity and determination you tin can attain a lot with just basic CSS properties.

In some cases, with more than advanced shapes, it's also a good idea to use the :later and :earlier pseudo selectors. This is out of scope of this article though as my intention is to comprehend the basics to get you going.

Disadvantage

There is ane big disadvantage with the above approach. For example, if you want your text to flow effectually and wrap your shape. A regular HTML div with groundwork and borders to make up the shape won't allow that. The text will not adapt and menstruum around your shape. Instead it will flow around the div itself (which is a square or a rectangle).

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

textflow-bad

Luckily we have some modernistic CSS properties to use instead.

CSS Shapes - The other style

Nowadays we accept a holding chosen shape-exterior to use in CSS. This property lets you define a shape that the text volition wrap/menstruum effectually.

Along with this property we have some basic shapes:

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

Here'due south a tip: You can also use the clip-path property. Y'all can create your shape with that in the aforementioned way, but it won't let the text wrap effectually your shape like shape-outside does.

The element that we are going to apply the shape to with the shape-outside property to has to exist floated. It likewise has to have a defined width and summit. That's really important to know!

Yous can read more about why hither. Below is besides a text that I've taken from the provided link to developer.mozilla.org.

The shape-exterior holding is specified using the values from the list below, which ascertain the float area for float elements. The float area determines the shape effectually which inline content (float elements) wrap.

inset()

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

You can specify the beginning to be the same for all four directions like this: inset(20px). Or information technology can be individually gear up for each management: inset(20px 5px 30px 10px).

You tin utilize other units also to gear up the showtime, for example, pct. The values stand for similar this: inset(top right bottom left) .

Cheque out the below lawmaking case. I've specified the inset values to be 20px at the superlative, 5px to the right, 30px at the bottom and 10px to the left. If you lot want your text to go around your square instead you lot tin 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 get-go past 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 besides possible to give inset() a second value that specifies the border-radius of the inset. Like below:

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

circle()

In this ane a circle is created using the shape-outside property. Y'all too take to apply a prune-path with the corresponding property for the circle to bear witness up.

The clip-path holding can take the aforementioned value as the shape-outside property so nosotros can give information technology the standard circumvolve() shape that we used for shape-exterior. 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();     prune-path: circle();     background: lightblue; }              
circle-shape-margin-1
Text flows around the shape!

In the above instance, 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 yous desire to specify a different size for the circle you can do that.

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

In the below case I've set the radius to 50%. And so I have shifted the heart of the circumvolve past 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 one-half as I move the position to nil.

                                  #circle {       float: left;       width: 150px;       height: 150px;       margin: 20px;       shape-outside: circle(l% at xxx%);       clip-path: circle(l% at 0%);       background: lightblue;     }              
circle2

ellipse()

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

The same equally a circle, it besides 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%);       groundwork: lightblue;     }              
ellipse

polygon()

A polygon is a shape with different vertices/coordinates divers. Below I create a "T" shape which is the first letter in my name. I commencement from the coordinates 0,0 and motion from left to correct to create the "T" shape.

                #polygon {       float: left;       width: 150px;       superlative: 150px;       margin: 0 20px;       shape-outside: polygon(         0 0,         100% 0,         100% 20%,         lx% 20%,         60% 100%,         40% 100%,         twoscore% 20%,         0 20%       );       prune-path: polygon(         0 0,         100% 0,         100% 20%,         60% 20%,         sixty% 100%,         twoscore% 100%,         40% 20%,         0 xx%       );       background: lightblue;     }              
polygon_t

Images

You can too use images with transparent backgrounds to create your shape. Like this round beautiful moon beneath.

This is a .png paradigm with a transparent background.

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

And that's it! Cheers for reading.

About the writer of this article

My name is Thomas Weibenfalk and I'chiliad a developer from Sweden. I regularly create free tutorials on my Youtube channel. There's besides a few premium courses out there on React and Gatsby. Experience gratuitous to visit me on these links:

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



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

anthonyitoponed.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 "Draw a Circle Around Image"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel