Cutting rectangle from circle Can the following code snippet be optimized or am I doing it right?

&lt;svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"&gt;
&lt;mask id="a"&gt;
&lt;circle cx="50" cy="50" r="50" fill="#fff"/&gt;
&lt;rect x="50" width="5" height="100%"/&gt;
&lt;/mask&gt;
&lt;rect width="100" height="100" fill="gray" mask="url(#a)" /&gt;
&lt;/svg&gt;


http://jsfiddle.net/5z4npr5L/6/

I don't want to use a path because I want it readable.
While I don't entirely agree with not using path, I will try to keep my answer unbiased. This is because using a path is much more developer-friendly, and opens up more options to do what you ask.

Keep your code styled, with--that's right--tabs

Using tabs in your code to indent it makes it a lot more readable and allows you to see the structure much more easily. You can see the difference in the following two code examples:

Before:

&lt;svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"&gt;
&lt;mask id="a"&gt;
&lt;circle cx="50" cy="50" r="50" fill="#fff"/&gt;
&lt;rect x="50" width="5" height="100%"/&gt;
&lt;/mask&gt;
&lt;rect width="100" height="100" fill="gray" mask="url(#a)" /&gt;
&lt;/svg&gt;


After:

&lt;svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"&gt;
    &lt;mask id="a"&gt;
        &lt;circle cx="50" cy="50" r="50" fill="#fff"/&gt;
        &lt;rect x="50" width="5" height="100%"/&gt;
    &lt;/mask&gt;
    &lt;rect width="100" height="100" fill="gray" mask="url(#a)" /&gt;
&lt;/svg&gt;


XMLNS isn't needed

Unless you're using XHTML, XML, or any other kind of page that requires an XML parser, then XMLNS is not needed. Since inline SVG is a recent development in HTML5, this is never really needed. I'm also going to add that you added the tags HTML and CSS to this, so I'll assume you don't want XMLNS.

Your final code should look something like this:



&lt;svg width="100" height="100"&gt;
    &lt;mask id="a"&gt;
        &lt;circle cx="50" cy="50" r="50" fill="#fff" /&gt;
        &lt;rect x="50" width="5" height="100%" /&gt;
    &lt;/mask&gt;
    &lt;rect width="100" height="100" fill="gray" mask="url(#a)" /&gt;
&lt;/svg&gt;




There isn't much more you could optimize without using path. Using path would allow you to use things like fill-rule="evenodd", and many other options.

EDIT:

@somethinghere asked me to expound a bit on how to use the extra features available with path that would allow you to do shape cutouts much more easily.


fill-rule="evenodd"


This can be used on a &lt;path&gt; element and have the &lt;path&gt; element have multiple shapes, if the shape number is even in the path call, it will define an outside point. If it's odd, it'll define an inside point. This allows you to define two path segments, your circle and your rectangle, and have the rect mark the inside point. This is an implementation using fill-rule="evenodd" in your scenario:



&lt;svg width="100" height="100"&gt;
    &lt;path d="M50 0 h 5 v 100 h -5 z M0 50 a 50 50 0 1 1 0 0.0001 z" fill-rule="evenodd" fill="gray" /&gt;
&lt;/svg&gt;





Use a &lt;clipPath&gt;. This works similar to the &lt;mask&gt; tag you used and doesn't need a &lt;path&gt; element either:




&lt;svg width="100" height="100"&gt;
    &lt;clipPath id="myClip"&gt;
        &lt;rect x="0" y="0" width="50" height="100%" /&gt;
        &lt;rect x="55" y="0" width="50" height="100%" /&gt;
    &lt;/clipPath&gt;
    &lt;circle cx="50" cy="50" r="50" fill="gray" clip-path="url(#myClip)" /&gt;
&lt;/svg&gt;





You could use a &lt;path&gt; element and define the entire shape if needed, but that seems a bit counter intuitive unless you're using a tool to make the SVG instead of drawing it by hand.