Currently building a browser-based SVG application. Within this app, various shapes can be styled and positioned by the user, including rectangles.
When I apply a stroke-width
to an SVG rect
element of say 1px
, the stroke is applied to the rect
’s offset and inset in different ways by different browsers. This is proving to be troublesome, especially when I try to calculate the outer width and visual position of a rectangle and position it next to other elements.
For example:
- Firefox adds 1px inset (bottom and left), and 1px offset (top and right)
- Chrome adds 1px inset (top and left), and 1px offset (bottom and right)
My only solution so far would be to draw the actual borders myself (probably with the path
tool) and position the borders behind the stroked element. But this solution is an unpleasant workaround, and I’d prefer not to go down this road if possible.
So my question is, can you control how an SVG’s stroke-width
is drawn on elements?
A (dirty) possible solution is by using patterns,
here is an example with an inside stroked triangle :
https://jsfiddle.net/qr3p7php/5/
Here's a function that will calculate how many pixels you need to add - using the given stroke - to the top, right, bottom and left, all based on the browser:
I don’t know how helpful will that be but in my case I just created another circle with border only and placed it “inside” the other shape.
I found an easy way, which has a few restrictions, but worked for me:
Here a working example:
UPDATE: The
stroke-alignment
attribute was on April 1st moved to a completely new spec called SVG Strokes.As of the SVG 2.0 Editor’s Draft of February 26th, 2015 (and possibly since February 13th),
thewith the valuesstroke-alignment
property is presentinner
,center
(default) andouter
.It seems to work the same way as the
stroke-location
property proposed by @Phrogz and the laterstroke-position
suggestion. This property has been planned since at least 2011, but apart from an annotation that said, it has never been detailed in the spec as it was deferred - until now, it seems.
No browser support this property, or, as far as I know, any of the new SVG 2 features, yet, but hopefully they will soon as the spec matures. This has been a property I personally have been urging to have, and I'm really happy that it's finally there in the spec.
There seems to be some issues as to how to the property should behave on open paths as well as loops. These issues will, most probably, prolong implementations across browsers. However, I will update this answer with new information as browsers begin to support this property.
Here is a work around for inner bordered
rect
usingsymbol
anduse
.Example: https://jsbin.com/yopemiwame/edit?html,output
SVG:
Note: Make sure to replace the
?
inuse
with real values.Background: The reason why this works is because symbol establishes a new viewport by replacing
symbol
withsvg
and creating an element in the shadow DOM. Thissvg
of the shadow DOM is then linked into your currentSVG
element. Note thatsvg
s can be nested and everysvg
creates a new viewport, which clips everything that overlaps, including the overlapping border. For a much more detailed overview of whats going on read this fantastic article by Sara Soueidan.