Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounded corner only on one side of svg <rect>

Tags:

html

css

svg

I am trying to implement a bar chart like diagram. I have the following html element

<rect x="35" y="-135" width="10" height="51" style="stroke: rgb(255, 255, 255); opacity: 0.8; fill: rgb(255, 122, 0);"></rect> 

I want to give the top corner of the rectangle a rounded shape. How is it possible?
I am not able to apply border-radius property.

like image 949
Dhanya Avatar asked Jan 21 '16 12:01

Dhanya


People also ask

How do you round SVG corners?

To draw a rectangle in HTML SVG, use the SVG <rect> element. For rounded corners, set the rx and ry attribute, which rounds the corners of the rectangle.

How do you round individual corners in Inkscape?

In Inkscape, rounding the corners of a rectangle is easy - you select the object, press F4 (rectangle tool), and drag the circular nodes.


1 Answers

You may use clipPath too. It's kind of a hack but it may be easier to implement.

Assuming the follow:

  • your rect is width="10" and height="51"
  • the top corner will be rx="5" and ry="5"

<svg>     <defs>         <clipPath id="round-corner">             <rect x="0" y="0" width="10" height="56" rx="5" ry="5"/>          </clipPath>      </defs>       <!-- if you want to use x="35" y="-135" put clip-path on a `g` element -->       <rect width="10"             height="51"             clip-path="url(#round-corner)"            style="stroke: rgb(255, 255, 255); opacity: 0.8; fill: rgb(255, 122, 0);"></rect> </svg>

Some Notes:
So the clipPath > rect > width is exactly the same as your rect.

Instead for clipPath > rect > height, you have to consider the corner radius on top and thus the height should be rect.height + desired-corner-radius (in this case 51px + 5px).

In that way you won't touch the bottom part of your rect with the clipPath.

like image 186
borracciaBlu Avatar answered Sep 21 '22 08:09

borracciaBlu