When I change the stroke width of a rectangle the width and and height change also. How can I prevent this from happening?
In my opinion, "width of a rectangle" in your question can mean two things, leading to two different answers; it is not clear for me which one you actually ask for. Which one do you want to have constant:
- The visual size of the rectangle, i.e. from the outer line edge left to the outer line edge right.
- The nominal (geometric) size of the rectangle, i.e., from the center of the left line to the center of the right line.
You might rather care for (1) if you do graphic design and for (2) if you do technical drawings. Inkscape by default seems to show (1) as the width of an object (e.g., in the "W:" field in the toolbar). The answers already given here indicate on how to keep this visual size (1) constant.
However, if you are actually interested in (2), Inkscape can also to show this as the width. For this, go to Preferences -> Tools and set "Bounding box to use" to "Geometric bounding box" instead of "Visual bounding box". Now the geometric size (2) will be indicated by Inkscape. You can now change the line width as you like, and the indicated rectangle width is not affected.
Assuming that you are talking about a relatively small change:
The reason for this is that a rectangle with a stroke is based on a set of infinetely small lines (red), which are then equipped with a stroke (black) such that those lines are in the middle of the stroke.
If you increase the size of the stroke, the position of the lines (red) is fixed and thus you also slightly increase the size of the total object (by half the increase of the stroke width).
I am not aware of a way to directly prevent this, but you can manually reset the size of the rectangle after changing the stroke width. The easiest way to do this is as follows:
- Select the rectangle.
- Copy it (Ctrl + C).
- Change the stroke width.
- Paste → Paste Size (if you care about preserving the size to the last digit, you may have to repeat this once or twice).
In Inkscape while in
Scaling and transform objects (F1) mode there are four toggle buttons on the top toolbar after a label
Affect:. The first one has tooltip stating
When scaling objects, scale the stroke width by the same proportion. Turn it off and then you can resize the rectangle without changing the stroke width.
It sounds like what you are asking for is similar to
box-sizing:border-box in CSS. There is no analog for this property in SVG.
Inkscape does have a built-in work around for this problem. The work around attempts to re-size your object so that result is as though the stroke moved inward. This is a one-time only function so you'd have to do it again any time you size your object. It also requires you to plan ahead your sizes.
- Draw a rectangle with a stroke width of 5px.
- Duplicate it with Ctrl+D and move it off to the size.
- In your duplicate, change your border to 10px.
- With the duplicate selected, click
Path > Inset.
If you compare the size of the two rectangles, you'll see that it didn't work! The reason is for the imaginary border box. So when you change to inset for a 10px border, it reducing the object's size by
(w / 2) + 1 px.
In other words, to expand the border of a 5px box, could change it to 9px and use inset once, change it to 13px and use inset twice, change it to 17px and use inset 3 times, etc. You can't just pick an arbitrary value, you have to grow the border based on the current border size.
This is obviously really awkward to use. A more reliable technique is described in Wrzlprmft's answer.
In the stroke panel you can adjust whether the stroke aligns to the center, outside or inside of the path. Create the perfect size rectangle and then Align the stroke to the inside. As you make the stroke bigger, it will grow towards the inside thus maintaining the size of the rectangle. Note you can only adjust the path stroke alignment on closed paths - so it will work for the rectangle but something to know if you try to apply it to an open path it won't let you.