How do I create a shape like this to display on a webpage?
I don't want to use images since they would get blurry on scaling
I tried with CSS:
.tear {
display: inline-block;
transform: rotate(-30deg);
border: 5px solid green;
width: 50px;
height: 100px;
border-top-left-radius: 50%;
border-bottom-left-radius: 50%;
border-bottom-right-radius: 50%;
}
<div class="tear">
</div>
That turned out really screwed.
And then I tried with SVG:
<svg viewBox="0 100 100">
<polygon points="50,0 100,70 50,100 0,70"/>
</svg>
It did get the shape, but the bottom part wasn't curved.
Is there a way to create this shape so it can be used in an HTML page?
I'd personally use an SVG for this. You can create SVGs in most vector graphics software. I'd recommend:
I have made one below that is a tracing of your shape in Illustrator.
HTML Canvas
This is an option uncovered in this thread so far. The commands used for Canvas drawings are very similar to SVG (and web-tiki deserves the credits for the base idea used in this answer).
The shape in question can be created either using canvas' own curve commands (Quadratic or Bezier) or the Path API. The answer contains examples for all three methods.
The browser support for Canvas is quite good.
Using Quadratic Curves
Below is an advanced version with gradient fill and shadows. I have also included a
hover
effect on the shape to illustrate one drawback of Canvas when compared to SVG. Canvas is raster (pixel) based and hence would look blurred/pixelated when scaled beyond a certain point. The only solution to that would be to repaint the shape on every browser resize which is an overhead.Using Bezier Curves
Using Path API
Note: As mentioned in my answere here, the Path API is not yet supported by IE and Safari.
Further reading:
IMO this shape requires smooth curve-to beziers to ensure continuity of the curve.
The Drop in question :
For the drop in question,
Note: Red and blue curves are two different quadratic curves.
stroke-linejoin="miter"
, for the pointed top part.AS this shape only uses successive
c
commands, we can omit it.Here's the final snippet:
TBH though, accepted answer's curves are not quite continuous.
For IE 5-8 (VML)
Only works in IE 5-8. VML uses different commands than SVG. Eg. it uses v for relative cubic beziers.
Note: This snippet won't run in IE 5-8 too. You need to create an html file and run it directly in the browser.
Basic Border-Radius
You can do this within CSS relatively easily using border-radius' and transforms. Your CSS was just a little bit out.
Advanced Border-Radius
This will be very similar to above but gives it a bit more shape.
It is quite easy to do this with SVG by just using an image conversion resource such as http://image.online-convert.com/convert-to-svg, which was used to create the following:
SVG approach:
You can achieve the double curve easily with an inline SVG and the
<path/>
element instead of the<polygon/>
element which doesn't allow curved shapes.The following example uses the
<path/>
element with:Q
)A
)SVG is a great tool to make this kind of shapes with double curves. You can check this post about double curves with an SVG/CSS comparison. Some of the advantages of using SVG in this case are:
Browser support for inline SVG goes back to Internet Explorer 9. See canIuse for more information.