Please look at eaExperiment. I want to make a definition which takes StartArrow definition and rotates it by 180 degrees.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" width="450" height="400" version="1.1">
<style type="text/css">
<![CDATA[
rect {fill:white; stroke: black; stroke-width: 1;}
text {fill: black; font-family: sans-serif; font-size: 10pt}
line {stroke:black; stroke-width:2}
]]>
</style>
<defs>
<marker orient="auto" refY="0.0" refX="0.0" id="StartArrow" style="overflow:visible;">
<path style="fill-rule:evenodd;stroke-width:0.625;stroke-linejoin:round;stroke:#000000;stroke-opacity:1;fill:#000000;fill-opacity:1" d="M 8.7185878,4.0337352 L -2.2072895,0.016013256 L 8.7185884,-4.0017078 C 6.9730900,-1.6296469 6.9831476,1.6157441 8.7185878,4.0337352 z " />
</marker>
<use id="eaExperiment" href="#StartArrow" transform="rotate(180)" />
<marker orient="auto" refY="0.0" refX="0.0" id="EndArrow" style="overflow:visible;">
<path style="fill-rule:evenodd;stroke-width:0.625;stroke-linejoin:round;stroke:#000000;stroke-opacity:1;fill:#000000;fill-opacity:1" d="M 8.7185878,4.0337352 L -2.2072895,0.016013256 L 8.7185884,-4.0017078 C 6.9730900,-1.6296469 6.9831476,1.6157441 8.7185878,4.0337352 z " transform="rotate(180)" />
</marker>
<svg id="Box">
<rect width="100" height="85" x="1" y="1" />
<text x="5" y="20">The box</text>
<svg x="10" y="25">
<rect width="70" height="50" x="1" y="1" />
<text x="5" y="20">Box</text>
<text x="5" y="40">Contents</text>
</svg>
</svg>
</defs>
<svg>
<svg x="10" y="120">
<rect width="100" height="50" x="1" y="1" />
<text x="5" y="20">Data</text>
<text x="5" y="40">source</text>
</svg>
<svg x="150">
<use href="#Box" y="1" />
<use href="#Box" y="100" />
<use href="#Box" y="200" />
</svg>
<svg x="300" y="120">
<rect width="100" height="50" x="1" y="1" />
<text x="5" y="20">Database</text>
<text x="5" y="40">server</text>
</svg>
<line x1="100" y1="120" x2="148" y2="40" style="marker-end:url(#EndArrow)" />
<line x1="110" y1="150" x2="147" y2="150" style="marker-end:url(#EndArrow)" />
<line x1="100" y1="170" x2="148" y2="240" style="marker-end:url(#EndArrow)" />
<line x1="254" y1="40" x2="297" y2="120" style="marker-start:url(#StartArrow); marker-end:url(#EndArrow)" />
<line x1="250" y1="150" x2="297" y2="150" style="marker-start:url(#eaExperiment); marker-end:url(#EndArrow)" />
<line x1="250" y1="240" x2="297" y2="170" style="marker-end:url(#EndArrow)" />
</svg>
</svg>
I am definitely doing it wrong, but what is the right way?
You need separate <marker> elements, but their content can be reused with <use> elements. For example like this:
<defs>
<path id="arrow" style="fill-rule:evenodd;stroke-width:0.625;stroke-linejoin:round;stroke:#000000;stroke-opacity:1;fill:#000000;fill-opacity:1" d="M 8.7185878,4.0337352 L -2.2072895,0.016013256 L 8.7185884,-4.0017078 C 6.9730900,-1.6296469 6.9831476,1.6157441 8.7185878,4.0337352 z " />
<marker orient="auto" refY="0.0" refX="0.0" id="StartArrow" style="overflow:visible;">
<use xlink:href="#arrow" />
</marker>
<marker orient="auto" refY="0.0" refX="0.0" id="EndArrow" style="overflow:visible;">
<use xlink:href="#arrow" transform="rotate(180)" />
</marker>
</defs>
(While the use of the xlink namespace with href is deprecated and also from a practical perspective no longer needed in current browsers, I tend to still use it for the sake of other renderers, Inkscape for example.)
Related
Is it possible to vertically center align all the rects inside a tag without having to adjust the y attributes on <rect>? (see snippet for example)
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3/org/1999/xlink" viewBox="0 0 225 38" preserveAspectRatio="none" width="100%" height="100%">
<g fill="black">
<rect x="10" y="1" width="6" height="5" />
<rect x="20" y="1" width="6" height="10" />
<rect x="30" y="1" width="6" height="20" />
<rect x="40" y="1" width="6" height="5" />
<rect x="50" y="1" width="6" height="15" />
</g>
</svg>
<h3>desired result</h3>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3/org/1999/xlink" viewBox="0 0 225 38" preserveAspectRatio="none" width="100%" height="100%">
<g fill="black">
<rect x="10" y="8" width="6" height="5" />
<rect x="20" y="6" width="6" height="10" />
<rect x="30" y="1" width="6" height="20" />
<rect x="40" y="8" width="6" height="5" />
<rect x="50" y="4" width="6" height="15" />
</g>
</svg>
No, it's not possible to center align <rect> elements.
But it is possible to center-align <line> elements and give them a stroke-width (note the viewBox is vertically centered around 0):
<svg viewBox="0 -19 225 38" width="100%" height="100%">
<g stroke="black">
<line x1="10" x2="16" stroke-width="5" />
<line x1="20" x2="26" stroke-width="10" />
<line x1="30" x2="36" stroke-width="20" />
<line x1="40" x2="46" stroke-width="5" />
<line x1="50" x2="56" stroke-width="15" />
</g>
</svg>
You could also achieve vertically centered <rect> elements by setting a transform: translate(0, -50%) css rule.
This approach also requires a transform-box: fill-box; (or content-box) property.
All <rect>elements get a y="50%" attribute to start at the vertical center of the svg viewBox.
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3/org/1999/xlink" viewBox="0 0 225 38" width="100%" height="100%" style="border:1px solid #ccc">
<style>
rect {
transform: translate(0, -50%);
transform-origin: center;
transform-box: fill-box;
}
</style>
<g fill="black" >
<rect x="10" y="50%" width="6" height="5" />
<rect x="20" y="50%" width="6" height="10" />
<rect x="30" y="50%" width="6" height="20" />
<rect x="40" y="50%" width="6" height="5" />
<rect x="50" y="50%" width="6" height="15" />
</g>
</svg>
Browser support for transform-box is decent. (See caniuse).
However, if you need legacy browser (e.g. ie11) support, #ccprog's answer is a more robust solution.
I have text in my SVGs that is offset by 1 or sometimes 2 from its assigned x. The text is set at x="6" however, the text is at 7 or even 8.
Here is an SVG with all the letters of the alphabet and numerals 0 through 9 to show the offset. I just can not figure out why there is an offset. It gets worse when the text is in italics for some characters. Should I just subtract 1 from x to get the text where I want it? Is this normal behavior for the arial font in SVG?
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
viewBox="0 0 111 1088"
width="111"
height="1088"
id="Alphabet_boxes"
>
<title id="Alphabet_boxes_title">Alphabet boxes</title>
<metadata id="Alphabet_boxes_metadata">
<rdf:RDF>
<cc:Work rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title>Alphabet boxes</dc:title>
<dc:date>2020-05-19</dc:date>
<dc:creator>
<cc:Agent>
<dc:title>Lady Aleena</dc:title>
</cc:Agent>
</dc:creator>
<dc:language>en-US</dc:language>
</cc:Work>
</rdf:RDF>
</metadata>
<style id="Crossover_styles">
path { fill:none; stroke: #666666; stroke-width: 2; stroke-opacity: 1; stroke-linejoin: round; stroke-linecap: round; }
rect { paint-order: stroke; stroke: #666666; stroke-width: 4; stroke-opacity: 1; stroke-linejoin: round; fill-opacity: 1; fill: #ffffff; }
rect.inner { stroke: none; fill: #ccc; }
text { fill: #000000; font-family: arial, roboto, sans-serif; font-size: 16px; text-anchor: start; dominant-baseline: middle; }
g.normal_text { font-style: normal; }
g.italic_text { font-style: italic; }
g.bold_text { font-weight: bold; }
tspan { font-size: 90%; font-style: normal; }
</style>
<g id="Alphabet_boxes_chart" class="graph" transform="translate(5,5)">
<title id="Alphabet_boxes_chart_title">Alphabet boxes</title>
<g id="normal" class="normal_text">
<g id="normal_A" transform="translate(0,0)">
<title id="normal_A_title">A</title>
<rect id="normal_A_box_1" x="0" y="0" width="24" height="28" />
<rect id="normal_A_box_2" x="4" y="4" width="16" height="20" class="inner" />
<text id="normal_A_text" x="6" y="14">A</text>
</g>
<g id="normal_B" transform="translate(0,30)">
<title id="normal_B_title">B</title>
<rect id="normal_B_box_1" x="0" y="0" width="21" height="28" />
<rect id="normal_B_box_2" x="4" y="4" width="13" height="20" class="inner" />
<text id="normal_B_text" x="6" y="14">B</text>
</g>
<g id="normal_C" transform="translate(0,60)">
<title id="normal_C_title">C</title>
<rect id="normal_C_box_1" x="0" y="0" width="23" height="28" />
<rect id="normal_C_box_2" x="4" y="4" width="15" height="20" class="inner" />
<text id="normal_C_text" x="6" y="14">C</text>
</g>
<g id="normal_D" transform="translate(0,90)">
<title id="normal_D_title">D</title>
<rect id="normal_D_box_1" x="0" y="0" width="22" height="28" />
<rect id="normal_D_box_2" x="4" y="4" width="14" height="20" class="inner" />
<text id="normal_D_text" x="6" y="14">D</text>
</g>
<g id="normal_E" transform="translate(0,120)">
<title id="normal_E_title">E</title>
<rect id="normal_E_box_1" x="0" y="0" width="21" height="28" />
<rect id="normal_E_box_2" x="4" y="4" width="13" height="20" class="inner" />
<text id="normal_E_text" x="6" y="14">E</text>
</g>
<g id="normal_F" transform="translate(0,150)">
<title id="normal_F_title">F</title>
<rect id="normal_F_box_1" x="0" y="0" width="21" height="28" />
<rect id="normal_F_box_2" x="4" y="4" width="13" height="20" class="inner" />
<text id="normal_F_text" x="6" y="14">F</text>
</g>
<g id="normal_G" transform="translate(0,180)">
<title id="normal_G_title">G</title>
<rect id="normal_G_box_1" x="0" y="0" width="24" height="28" />
<rect id="normal_G_box_2" x="4" y="4" width="16" height="20" class="inner" />
<text id="normal_G_text" x="6" y="14">G</text>
</g>
<g id="normal_H" transform="translate(0,210)">
<title id="normal_H_title">H</title>
<rect id="normal_H_box_1" x="0" y="0" width="22" height="28" />
<rect id="normal_H_box_2" x="4" y="4" width="14" height="20" class="inner" />
<text id="normal_H_text" x="6" y="14">H</text>
</g>
<g id="normal_I" transform="translate(0,240)">
<title id="normal_I_title">I</title>
<rect id="normal_I_box_1" x="0" y="0" width="15" height="28" />
<rect id="normal_I_box_2" x="4" y="4" width="7" height="20" class="inner" />
<text id="normal_I_text" x="6" y="14">I</text>
</g>
<g id="normal_J" transform="translate(0,270)">
<title id="normal_J_title">J</title>
<rect id="normal_J_box_1" x="0" y="0" width="19" height="28" />
<rect id="normal_J_box_2" x="4" y="4" width="11" height="20" class="inner" />
<text id="normal_J_text" x="6" y="14">J</text>
</g>
<g id="normal_K" transform="translate(0,300)">
<title id="normal_K_title">K</title>
<rect id="normal_K_box_1" x="0" y="0" width="22" height="28" />
<rect id="normal_K_box_2" x="4" y="4" width="14" height="20" class="inner" />
<text id="normal_K_text" x="6" y="14">K</text>
</g>
<g id="normal_L" transform="translate(0,330)">
<title id="normal_L_title">L</title>
<rect id="normal_L_box_1" x="0" y="0" width="20" height="28" />
<rect id="normal_L_box_2" x="4" y="4" width="12" height="20" class="inner" />
<text id="normal_L_text" x="6" y="14">L</text>
</g>
<g id="normal_M" transform="translate(0,360)">
<title id="normal_M_title">M</title>
<rect id="normal_M_box_1" x="0" y="0" width="24" height="28" />
<rect id="normal_M_box_2" x="4" y="4" width="16" height="20" class="inner" />
<text id="normal_M_text" x="6" y="14">M</text>
</g>
<g id="normal_N" transform="translate(0,390)">
<title id="normal_N_title">N</title>
<rect id="normal_N_box_1" x="0" y="0" width="22" height="28" />
<rect id="normal_N_box_2" x="4" y="4" width="14" height="20" class="inner" />
<text id="normal_N_text" x="6" y="14">N</text>
</g>
<g id="normal_O" transform="translate(0,420)">
<title id="normal_O_title">O</title>
<rect id="normal_O_box_1" x="0" y="0" width="24" height="28" />
<rect id="normal_O_box_2" x="4" y="4" width="16" height="20" class="inner" />
<text id="normal_O_text" x="6" y="14">O</text>
</g>
<g id="normal_P" transform="translate(0,450)">
<title id="normal_P_title">P</title>
<rect id="normal_P_box_1" x="0" y="0" width="21" height="28" />
<rect id="normal_P_box_2" x="4" y="4" width="13" height="20" class="inner" />
<text id="normal_P_text" x="6" y="14">P</text>
</g>
<g id="normal_Q" transform="translate(0,480)">
<title id="normal_Q_title">Q</title>
<rect id="normal_Q_box_1" x="0" y="0" width="24" height="28" />
<rect id="normal_Q_box_2" x="4" y="4" width="16" height="20" class="inner" />
<text id="normal_Q_text" x="6" y="14">Q</text>
</g>
<g id="normal_R" transform="translate(0,510)">
<title id="normal_R_title">R</title>
<rect id="normal_R_box_1" x="0" y="0" width="23" height="28" />
<rect id="normal_R_box_2" x="4" y="4" width="15" height="20" class="inner" />
<text id="normal_R_text" x="6" y="14">R</text>
</g>
<g id="normal_S" transform="translate(0,540)">
<title id="normal_S_title">S</title>
<rect id="normal_S_box_1" x="0" y="0" width="22" height="28" />
<rect id="normal_S_box_2" x="4" y="4" width="14" height="20" class="inner" />
<text id="normal_S_text" x="6" y="14">S</text>
</g>
<g id="normal_T" transform="translate(0,570)">
<title id="normal_T_title">T</title>
<rect id="normal_T_box_1" x="0" y="0" width="22" height="28" />
<rect id="normal_T_box_2" x="4" y="4" width="14" height="20" class="inner" />
<text id="normal_T_text" x="6" y="14">T</text>
</g>
<g id="normal_U" transform="translate(0,600)">
<title id="normal_U_title">U</title>
<rect id="normal_U_box_1" x="0" y="0" width="22" height="28" />
<rect id="normal_U_box_2" x="4" y="4" width="14" height="20" class="inner" />
<text id="normal_U_text" x="6" y="14">U</text>
</g>
<g id="normal_V" transform="translate(0,630)">
<title id="normal_V_title">V</title>
<rect id="normal_V_box_1" x="0" y="0" width="23" height="28" />
<rect id="normal_V_box_2" x="4" y="4" width="15" height="20" class="inner" />
<text id="normal_V_text" x="6" y="14">V</text>
</g>
<g id="normal_W" transform="translate(0,660)">
<title id="normal_W_title">W</title>
<rect id="normal_W_box_1" x="0" y="0" width="27" height="28" />
<rect id="normal_W_box_2" x="4" y="4" width="19" height="20" class="inner" />
<text id="normal_W_text" x="6" y="14">W</text>
</g>
<g id="normal_X" transform="translate(0,690)">
<title id="normal_X_title">X</title>
<rect id="normal_X_box_1" x="0" y="0" width="23" height="28" />
<rect id="normal_X_box_2" x="4" y="4" width="15" height="20" class="inner" />
<text id="normal_X_text" x="6" y="14">X</text>
</g>
<g id="normal_Y" transform="translate(0,720)">
<title id="normal_Y_title">Y</title>
<rect id="normal_Y_box_1" x="0" y="0" width="23" height="28" />
<rect id="normal_Y_box_2" x="4" y="4" width="15" height="20" class="inner" />
<text id="normal_Y_text" x="6" y="14">Y</text>
</g>
<g id="normal_Z" transform="translate(0,750)">
<title id="normal_Z_title">Z</title>
<rect id="normal_Z_box_1" x="0" y="0" width="22" height="28" />
<rect id="normal_Z_box_2" x="4" y="4" width="14" height="20" class="inner" />
<text id="normal_Z_text" x="6" y="14">Z</text>
</g>
<g id="normal_zero" transform="translate(0,780)">
<title id="normal_zero_title">0</title>
<rect id="normal_zero_box_1" x="0" y="0" width="21" height="28" />
<rect id="normal_zero_box_2" x="4" y="4" width="13" height="20" class="inner" />
<text id="normal_zero_text" x="6" y="14">0</text>
</g>
<g id="normal_one" transform="translate(0,810)">
<title id="normal_one_title">1</title>
<rect id="normal_one_box_1" x="0" y="0" width="17" height="28" />
<rect id="normal_one_box_2" x="4" y="4" width="9" height="20" class="inner" />
<text id="normal_one_text" x="6" y="14">1</text>
</g>
<g id="normal_two" transform="translate(0,840)">
<title id="normal_two_title">2</title>
<rect id="normal_two_box_1" x="0" y="0" width="21" height="28" />
<rect id="normal_two_box_2" x="4" y="4" width="13" height="20" class="inner" />
<text id="normal_two_text" x="6" y="14">2</text>
</g>
<g id="normal_three" transform="translate(0,870)">
<title id="normal_three_title">3</title>
<rect id="normal_three_box_1" x="0" y="0" width="21" height="28" />
<rect id="normal_three_box_2" x="4" y="4" width="13" height="20" class="inner" />
<text id="normal_three_text" x="6" y="14">3</text>
</g>
<g id="normal_four" transform="translate(0,900)">
<title id="normal_four_title">4</title>
<rect id="normal_four_box_1" x="0" y="0" width="21" height="28" />
<rect id="normal_four_box_2" x="4" y="4" width="13" height="20" class="inner" />
<text id="normal_four_text" x="6" y="14">4</text>
</g>
<g id="normal_five" transform="translate(0,930)">
<title id="normal_five_title">5</title>
<rect id="normal_five_box_2" x="0" y="0" width="21" height="28" />
<rect id="normal_five_box_1" x="4" y="4" width="13" height="20" class="inner" />
<text id="normal_five_text" x="6" y="14">5</text>
</g>
<g id="normal_six" transform="translate(0,960)">
<title id="normal_six_title">6</title>
<rect id="normal_six_box_1" x="0" y="0" width="21" height="28" />
<rect id="normal_six_box_2" x="4" y="4" width="13" height="20" class="inner" />
<text id="normal_six_text" x="6" y="14">6</text>
</g>
<g id="normal_seven" transform="translate(0,990)">
<title id="normal_seven_title">7</title>
<rect id="normal_seven_box_1" x="0" y="0" width="21" height="28" />
<rect id="normal_seven_box_2" x="4" y="4" width="13" height="20" class="inner" />
<text id="normal_seven_text" x="6" y="14">7</text>
</g>
<g id="normal_eight" transform="translate(0,1020)">
<title id="normal_eight_title">8</title>
<rect id="normal_eight_box_1" x="0" y="0" width="21" height="28" />
<rect id="normal_eight_box_2" x="4" y="4" width="13" height="20" class="inner" />
<text id="normal_eight_text" x="6" y="14">8</text>
</g>
<g id="normal_nine" transform="translate(0,1050)">
<title id="normal_nine_title">9</title>
<rect id="normal_nine_box_1" x="0" y="0" width="21" height="28" />
<rect id="normal_nine_box_2" x="4" y="4" width="13" height="20" class="inner" />
<text id="normal_nine_text" x="6" y="14">9</text>
</g>
</g>
<g id="italic" transform="translate(37)" class="italic_text">
<g id="italic_A" transform="translate(0,0)">
<title id="italic_A_title">A</title>
<rect id="italic_A_box_1" x="0" y="0" width="23" height="28" />
<rect id="italic_A_box_2" x="4" y="4" width="15" height="20" class="inner" />
<text id="italic_A_text" x="6" y="14">A</text>
</g>
<g id="italic_B" transform="translate(0,30)">
<title id="italic_B_title">B</title>
<rect id="italic_B_box_1" x="0" y="0" width="23" height="28" />
<rect id="italic_B_box_2" x="4" y="4" width="15" height="20" class="inner" />
<text id="italic_B_text" x="6" y="14">B</text>
</g>
<g id="italic_C" transform="translate(0,60)">
<title id="italic_C_title">C</title>
<rect id="italic_C_box_1" x="0" y="0" width="23" height="28" />
<rect id="italic_C_box_2" x="4" y="4" width="15" height="20" class="inner" />
<text id="italic_C_text" x="6" y="14">C</text>
</g>
<g id="italic_D" transform="translate(0,90)">
<title id="italic_D_title">D</title>
<rect id="italic_D_box_1" x="0" y="0" width="24" height="28" />
<rect id="italic_D_box_2" x="4" y="4" width="16" height="20" class="inner" />
<text id="italic_D_text" x="6" y="14">D</text>
</g>
<g id="italic_E" transform="translate(0,120)">
<title id="italic_E_title">E</title>
<rect id="italic_E_box_1" x="0" y="0" width="24" height="28" />
<rect id="italic_E_box_2" x="4" y="4" width="16" height="20" class="inner" />
<text id="italic_E_text" x="6" y="14">E</text>
</g>
<g id="italic_F" transform="translate(0,150)">
<title id="italic_F_title">F</title>
<rect id="italic_F_box_1" x="0" y="0" width="23" height="28" />
<rect id="italic_F_box_2" x="4" y="4" width="15" height="20" class="inner" />
<text id="italic_F_text" x="6" y="14">F</text>
</g>
<g id="italic_G" transform="translate(0,180)">
<title id="italic_G_title">G</title>
<rect id="italic_G_box_1" x="0" y="0" width="24" height="28" />
<rect id="italic_G_box_2" x="4" y="4" width="16" height="20" class="inner" />
<text id="italic_G_text" x="6" y="14">G</text>
</g>
<g id="italic_H" transform="translate(0,210)">
<title id="italic_H_title">H</title>
<rect id="italic_H_box_1" x="0" y="0" width="25" height="28" />
<rect id="italic_H_box_2" x="4" y="4" width="17" height="20" class="inner" />
<text id="italic_H_text" x="6" y="14">H</text>
</g>
<g id="italic_I" transform="translate(0,240)">
<title id="italic_I_title">I</title>
<rect id="italic_I_box_1" x="0" y="0" width="17" height="28" />
<rect id="italic_I_box_2" x="4" y="4" width="9" height="20" class="inner" />
<text id="italic_I_text" x="6" y="14">I</text>
</g>
<g id="italic_J" transform="translate(0,270)">
<title id="italic_J_title">J</title>
<rect id="italic_J_box_1" x="0" y="0" width="21" height="28" />
<rect id="italic_J_box_2" x="4" y="4" width="13" height="20" class="inner" />
<text id="italic_J_text" x="6" y="14">J</text>
</g>
<g id="italic_K" transform="translate(0,300)">
<title id="italic_K_title">K</title>
<rect id="italic_K_box_1" x="0" y="0" width="24" height="28" />
<rect id="italic_K_box_2" x="4" y="4" width="16" height="20" class="inner" />
<text id="italic_K_text" x="6" y="14">K</text>
</g>
<g id="italic_L" transform="translate(0,330)">
<title id="italic_L_title">L</title>
<rect id="italic_L_box_1" x="0" y="0" width="21" height="28" />
<rect id="italic_L_box_2" x="4" y="4" width="13" height="20" class="inner" />
<text id="italic_L_text" x="6" y="14">L</text>
</g>
<g id="italic_M" transform="translate(0,360)">
<title id="italic_M_title">M</title>
<rect id="italic_M_box_1" x="0" y="0" width="26" height="28" />
<rect id="italic_M_box_2" x="4" y="4" width="18" height="20" class="inner" />
<text id="italic_M_text" x="6" y="14">M</text>
</g>
<g id="italic_N" transform="translate(0,390)">
<title id="italic_N_title">N</title>
<rect id="italic_N_box_1" x="0" y="0" width="25" height="28" />
<rect id="italic_N_box_2" x="4" y="4" width="17" height="20" class="inner" />
<text id="italic_N_text" x="6" y="14">N</text>
</g>
<g id="italic_O" transform="translate(0,420)">
<title id="italic_O_title">O</title>
<rect id="italic_O_box_1" x="0" y="0" width="24" height="28" />
<rect id="italic_O_box_2" x="4" y="4" width="16" height="20" class="inner" />
<text id="italic_O_text" x="6" y="14">O</text>
</g>
<g id="italic_P" transform="translate(0,450)">
<title id="italic_P_title">P</title>
<rect id="italic_P_box_1" x="0" y="0" width="24" height="28" />
<rect id="italic_P_box_2" x="4" y="4" width="16" height="20" class="inner" />
<text id="italic_P_text" x="6" y="14">P</text>
</g>
<g id="italic_Q" transform="translate(0,480)">
<title id="italic_Q_title">Q</title>
<rect id="italic_Q_box_1" x="0" y="0" width="24" height="28" />
<rect id="italic_Q_box_2" x="4" y="4" width="16" height="20" class="inner" />
<text id="italic_Q_text" x="6" y="14">Q</text>
</g>
<g id="italic_R" transform="translate(0,510)">
<title id="italic_R_title">R</title>
<rect id="italic_R_box_1" x="0" y="0" width="24" height="28" />
<rect id="italic_R_box_2" x="4" y="4" width="16" height="20" class="inner" />
<text id="italic_R_text" x="6" y="14">R</text>
</g>
<g id="italic_S" transform="translate(0,540)">
<title id="italic_S_title">S</title>
<rect id="italic_S_box_1" x="0" y="0" width="22" height="28" />
<rect id="italic_S_box_2" x="4" y="4" width="14" height="20" class="inner" />
<text id="italic_S_text" x="6" y="14">S</text>
</g>
<g id="italic_T" transform="translate(0,570)">
<title id="italic_T_title">T</title>
<rect id="italic_T_box_1" x="0" y="0" width="22" height="28" />
<rect id="italic_T_box_2" x="4" y="4" width="14" height="20" class="inner" />
<text id="italic_T_text" x="6" y="14">T</text>
</g>
<g id="italic_U" transform="translate(0,600)">
<title id="italic_U_title">U</title>
<rect id="italic_U_box_1" x="0" y="0" width="24" height="28" />
<rect id="italic_U_box_2" x="4" y="4" width="16" height="20" class="inner" />
<text id="italic_U_text" x="6" y="14">U</text>
</g>
<g id="italic_V" transform="translate(0,630)">
<title id="italic_V_title">V</title>
<rect id="italic_V_box_1" x="0" y="0" width="24" height="28" />
<rect id="italic_V_box_2" x="4" y="4" width="16" height="20" class="inner" />
<text id="italic_V_text" x="6" y="14">V</text>
</g>
<g id="italic_W" transform="translate(0,660)">
<title id="italic_W_title">W</title>
<rect id="italic_W_box_1" x="0" y="0" width="27" height="28" />
<rect id="italic_W_box_2" x="4" y="4" width="19" height="20" class="inner" />
<text id="italic_W_text" x="6" y="14">W</text>
</g>
<g id="italic_X" transform="translate(0,690)">
<title id="italic_X_title">X</title>
<rect id="italic_X_box_1" x="0" y="0" width="26" height="28" />
<rect id="italic_X_box_2" x="4" y="4" width="18" height="20" class="inner" />
<text id="italic_X_text" x="6" y="14">X</text>
</g>
<g id="italic_Y" transform="translate(0,720)">
<title id="italic_Y_title">Y</title>
<rect id="italic_Y_box_1" x="0" y="0" width="24" height="28" />
<rect id="italic_Y_box_2" x="4" y="4" width="16" height="20" class="inner" />
<text id="italic_Y_text" x="6" y="14">Y</text>
</g>
<g id="italic_Z" transform="translate(0,750)">
<title id="italic_Z_title">Z</title>
<rect id="italic_Z_box_1" x="0" y="0" width="23" height="28" />
<rect id="italic_Z_box_2" x="4" y="4" width="15" height="20" class="inner" />
<text id="italic_Z_text" x="6" y="14">Z</text>
</g>
<g id="italic_zero" transform="translate(0,780)">
<title id="italic_zero_title">0</title>
<rect id="italic_zero_box_1" x="0" y="0" width="21" height="28" />
<rect id="italic_zero_box_2" x="4" y="4" width="13" height="20" class="inner" />
<text id="italic_zero_text" x="6" y="14">0</text>
</g>
<g id="italic_one" transform="translate(0,810)">
<title id="italic_one_title">1</title>
<rect id="italic_one_box_1" x="0" y="0" width="18" height="28" />
<rect id="italic_one_box_2" x="4" y="4" width="10" height="20" class="inner" />
<text id="italic_one_text" x="6" y="14">1</text>
</g>
<g id="italic_two" transform="translate(0,840)">
<title id="italic_two_title">2</title>
<rect id="italic_two_box_1" x="0" y="0" width="21" height="28" />
<rect id="italic_two_box_2" x="4" y="4" width="13" height="20" class="inner" />
<text id="italic_two_text" x="6" y="14">2</text>
</g>
<g id="italic_three" transform="translate(0,870)">
<title id="italic_three_title">3</title>
<rect id="italic_three_box_1" x="0" y="0" width="21" height="28" />
<rect id="italic_three_box_2" x="4" y="4" width="13" height="20" class="inner" />
<text id="italic_three_text" x="6" y="14">3</text>
</g>
<g id="italic_four" transform="translate(0,900)">
<title id="italic_four_title">4</title>
<rect id="italic_four_box_1" x="0" y="0" width="21" height="28" />
<rect id="italic_four_box_2" x="4" y="4" width="13" height="20" class="inner" />
<text id="italic_four_text" x="6" y="14">4</text>
</g>
<g id="italic_five" transform="translate(0,930)">
<title id="italic_five_title">5</title>
<rect id="italic_five_box_1" x="0" y="0" width="21" height="28" />
<rect id="italic_five_box_2" x="4" y="4" width="13" height="20" class="inner" />
<text id="italic_five_text" x="6" y="14">5</text>
</g>
<g id="italic_six" transform="translate(0,960)">
<title id="italic_six_title">6</title>
<rect id="italic_six_box_1" x="0" y="0" width="21" height="28" />
<rect id="italic_six_box_2" x="4" y="4" width="13" height="20" class="inner" />
<text id="italic_six_text" x="6" y="14">6</text>
</g>
<g id="italic_seven" transform="translate(0,990)">
<title id="italic_seven_title">7</title>
<rect id="italic_seven_box_1" x="0" y="0" width="21" height="28" />
<rect id="italic_seven_box_2" x="4" y="4" width="13" height="20" class="inner" />
<text id="italic_seven_text" x="6" y="14">7</text>
</g>
<g id="italic_eight" transform="translate(0,1020)">
<title id="italic_eight_title">8</title>
<rect id="italic_eight_box_1" x="0" y="0" width="21" height="28" />
<rect id="italic_eight_box_2" x="4" y="4" width="13" height="20" class="inner" />
<text id="italic_eight_text" x="6" y="14">8</text>
</g>
<g id="italic_nine" transform="translate(0,1050)">
<title id="italic_nine_title">9</title>
<rect id="italic_nine_box_1" x="0" y="0" width="20" height="28" />
<rect id="italic_nine_box_2" x="4" y="4" width="12" height="20" class="inner" />
<text id="italic_nine_text" x="6" y="14">9</text>
</g>
</g>
</g>
</svg>
Change the attributes in the svg tag and add the sodipodi:namedview lines of the SVG to the following if you want to open it in Inkscape. The grid and guidelines are added so you can see the offset.
<svg
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
version="1.1"
sodipodi:docname="Alphabet_boxes_ink.svg"
inkscape:version="0.92.4 (5da689c313, 2019-01-14)"
viewBox="0 0 111 1088"
width="111"
height="1088"
id="Alphabet_boxes"
>
<sodipodi:namedview
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1"
objecttolerance="10"
gridtolerance="10"
guidetolerance="10"
inkscape:pageopacity="0"
inkscape:pageshadow="2"
inkscape:window-width="1280"
inkscape:window-height="979"
id="namedview5209"
showgrid="true"
showguides="true"
inkscape:guide-bbox="true"
inkscape:zoom="1"
inkscape:cx="52"
inkscape:cy="1.1e+03"
inkscape:window-x="0"
inkscape:window-y="0"
inkscape:window-maximized="1"
inkscape:current-layer="Alphabet_boxes"
>
<inkscape:grid type="xygrid" id="grid5213" />
<sodipodi:guide position="11,1088" orientation="1,0" id="guide5215" inkscape:locked="false" />
<sodipodi:guide position="48,1088" orientation="1,0" id="guide5217" inkscape:locked="false" />
<sodipodi:guide position="85,1088" orientation="1,0" id="guide5219" inkscape:locked="false" />
</sodipodi:namedview>
Is it possible to use CSS variables (analogue to e.g. opacity in style="opacity:var(--QQQ_SOMETHING_AAA, 1)" and .ZZZ_RECTANGLES{--QQQ_SOMETHING_AAA: 0.7;} to tweak the SVGS's TITLE (description on hover <title></title>) contents?
I'm looking for something that will use only SVG1.1 features and SMIL/CSS, so no other plug-ins that might worsen cross-browser compatibility.
Here's a MWE code snippet:
.HIDDEN_LAYER{
visibility:hidden;
opacity:0;
}
.CLICKME_TICKBOX{
fill:white;
stroke:black;
}
.CLICKED_TICKBOX{
fill:white;
stroke:black;
}
.CLICKME_TEXTS{
font-family:sans-serif;
font-size:16px;
}
.CLICKED_TEXTS{
font-family:sans-serif;
font-size:16px;
stroke:black;
stroke-width:0.5;
}
.BOTTOM-LAYER_RECTANGLES{
fill:white;
stroke:black;
}
.ZZZ_RECTANGLES{
visibility:hidden;
cursor:help;
fill:yellow;
stroke:black;
--QQQ_SOMETHING_AAA: 0.7;
--QQQ_SOMETHING_BBB: 0.2;
}
.FFF_RECTANGLES{
visibility:hidden;
cursor:help;
fill:red;
stroke:black;
--PPP_SOMETHING_AAA: 0.2;
--PPP_SOMETHING_BBB: 0.7;
}
.LEGENDBOX_ZZZ{
fill:yellow;
stroke:black;
}
.LEGENDBOX_FFF{
fill:red;
stroke:black;
}
.LEGENDS{
cursor:pointer;
}
<svg id="SVG"
version="1.1"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
width="1320"
height="1125"
viewBox="-15 -45 1350 1155">
<symbol id="SOMETHING_AAA">
<use
xlink:href="#RECTANGLE"
transform="translate(637.5, 25)"
style="
opacity:var(--QQQ_SOMETHING_AAA, 1);
fill-opacity:var(--PPP_SOMETHING_AAA, 1)
">
</use>
</symbol>
<symbol id="SOMETHING_BBB">
<use
xlink:href="#RECTANGLE"
transform="translate(15, 215)"
style="
opacity:var(--QQQ_SOMETHING_BBB, 1);
fill-opacity:var(--PPP_SOMETHING_BBB, 1)
">
</use>
</symbol>
<g class="BOTTOM-LAYER_RECTANGLES">
<use xlink:href="#SOMETHING_AAA" />
<use xlink:href="#SOMETHING_BBB" />
</g>
<g class="ZZZ_RECTANGLES">
<use xlink:href="#SOMETHING_AAA">
<title>Something here 111.</title>
</use>
<use xlink:href="#SOMETHING_BBB">
<title>Something here 222.</title>
</use>
<set
to="visible"
attributeType="CSS"
attributeName="visibility"
end="CLICKED_ZZZ.click;bg.click"
begin="CLICKME_ZZZ.click" />
<set
to="1"
attributeType="CSS"
attributeName="opacity"
end="CLICKED_ZZZ.click;bg.click"
begin="CLICKME_ZZZ.click" />
</g>
<g class="FFF_RECTANGLES">
<use xlink:href="#SOMETHING_AAA">
<title>Something here 333.</title>
</use>
<use xlink:href="#SOMETHING_BBB">
<title>Something here 444.</title>
</use>
<set
to="visible"
attributeType="CSS"
attributeName="visibility"
end="CLICKED_DDD.click;bg.click"
begin="CLICKME_DDD.click" />
<set
to="1"
attributeType="CSS"
attributeName="opacity"
end="CLICKED_DDD.click;bg.click"
begin="CLICKME_DDD.click" />
</g>
<g id="LEGENDS"
class="LEGENDS"
transform="translate(15, -15)">
<g id="ZZZ">
<g id="CLICKME_ZZZ">
<rect id="CLICKME_TICKBOX_ZZZ"
class="CLICKME_TICKBOX"
x="0"
y="0"
width="15"
height="15">
</rect>
<use id="CLICKME_LEGENDBOX_ZZZ"
class="LEGENDBOX_ZZZ"
x="30"
y="-5"
xlink:href="#LEGENDBOX">
</use>
<text id="CLICKME_TEXT_ZZZ"
class="CLICKME_TEXTS"
x="65"
y="12.5">
Click me
</text>
</g>
<g id="CLICKED_ZZZ"
class="HIDDEN_LAYER">
<use id="CLICKED_TICKBOX_ZZZ"
x="0"
y="0"
xlink:href="#CLICKED_TICKBOX">
</use>
<use id="CLICKED_LEGENDBOX_ZZZ"
class="LEGENDBOX_ZZZ"
x="30"
y="-5"
xlink:href="#LEGENDBOX">
</use>
<text id="CLICKED_TEXT_ZZZ"
class="CLICKED_TEXTS"
x="65"
y="12.5">
Click me
</text>
<set
to="visible"
attributeType="CSS"
attributeName="visibility"
end="CLICKED_ZZZ.click;bg.click"
begin="CLICKME_ZZZ.click" />
<set
to="1"
attributeType="CSS"
attributeName="opacity"
end="CLICKED_ZZZ.click;bg.click"
begin="CLICKME_ZZZ.click" />
</g>
<animate id="ZZZ_ANIMATE"
fill="freeze"
dur="3s"
keyTimes="0;1"
values="0;1"
attributeType="CSS"
attributeName="opacity" />
</g>
<g id="DDD"
transform="translate(0,40)">
<g id="CLICKME_DDD">
<rect id="CLICKME_TICKBOX_FFF"
class="CLICKME_TICKBOX"
x="0"
y="0"
width="15"
height="15">
</rect>
<use id="CLICKME_LEGENDBOX_FFF"
class="LEGENDBOX_FFF"
x="30"
y="-5"
xlink:href="#LEGENDBOX">
</use>
<text id="CLICKME_TEXT_FFF"
class="CLICKME_TEXTS"
x="65"
y="12.5">
Click me
</text>
</g>
<g id="CLICKED_DDD"
class="HIDDEN_LAYER">
<use id="CLICKED_TICKBOX_FFF"
x="0"
y="0"
xlink:href="#CLICKED_TICKBOX">
</use>
<use id="CLICKED_LEGENDBOX_FFF"
class="LEGENDBOX_FFF"
x="30"
y="-5"
xlink:href="#LEGENDBOX">
</use>
<text id="CLICKED_TEXT_FFF"
class="CLICKED_TEXTS"
x="65"
y="12.5">
Click me
</text>
<set
to="visible"
attributeType="CSS"
attributeName="visibility"
end="CLICKED_DDD.click;bg.click"
begin="CLICKME_DDD.click" />
<set
to="1"
attributeType="CSS"
attributeName="opacity"
end="CLICKED_DDD.click;bg.click"
begin="CLICKME_DDD.click" />
</g>
<animate id="DDD_ANIMATE"
fill="freeze"
dur="3s"
keyTimes="0;1"
values="0;1"
attributeType="CSS"
attributeName="opacity" />
</g>
</g>
<defs id="DEFINITIONS">
<rect id="RECTANGLE"
width="85"
height="95">
</rect>
<rect id="RECTANGLE_YELLOW"
width="42.5"
height="95">
</rect>
<rect id="LEGENDBOX"
x="0"
y="0"
width="25"
height="25">
</rect>
<path id="CLICKED_TICKBOX"
class="CLICKED_TICKBOX"
d="M 0,0 15,15 M 0,15 15,0 M 0,0 15,0 15,15 0,15 Z"/>
</defs>
It would be nice if the
<use
xlink:href="#RECTANGLE"
transform="translate(15, 215)"
style="
opacity:var(--QQQ_SOMETHING_BBB, 1);
fill-opacity:var(--PPP_SOMETHING_BBB, 1)
">
</use>
Could also contain both of the titles:
<title>Something here 222.</title>
and
<title>Something here 444.</title>
depending on whether it is in <g class="FFF_RECTANGLES"> or <g class="ZZZ_RECTANGLES">.
So, it would be nice if the code could be altered, for example as:
<use
xlink:href="#RECTANGLE"
transform="translate(15, 215)"
style="
opacity:var(--QQQ_SOMETHING_BBB, 1);
fill-opacity:var(--PPP_SOMETHING_BBB, 1)"
title=:var(--SSS_SOMETHING, Or else it will be this.);
var(--TTT_SOMETHING, Or else it will be that.)"
">
</use>
The title could then be stated in:
.ZZZ_RECTANGLES{
--QQQ_SOMETHING_AAA: 0.7;
--QQQ_SOMETHING_BBB: 0.2;
--SSS_SOMETHING: Something here 222.;
}
and
.FFF_RECTANGLES{
--PPP_SOMETHING_AAA: 0.2;
--PPP_SOMETHING_BBB: 0.7;
--TTT_SOMETHING: Something here 444.;
}
Titles in SVG are elements, not attributes. So I don't believe there is any feaible way to achieve what you want using only CSS.
Since JS is universally supported, I assume you wouldn't have objection to using that to achieve this?
// Take the text typed into the input box, and use it for the tootip for the red rectangle
document.getElementById("title-input").addEventListener("input", function(evt) {
setTitleText("rect-title", this.value);
});
function setTitleText(elementId, newText) {
document.getElementById(elementId).textContent = newText;
}
<svg>
<rect y="50" width="200" height="50" fill="red">
<title id="rect-title">Default title</title>
</rect>
</svg>
<p>Enter some text to use as a title tooltip for the rect</p>
<input type="text" id="title-input"/>
I have this CODEPEN and here are my issues:
I am not understanding why the gradient I applied and referenced as my mask fill like so, doesn't render as it should. It should go from fully opaque to fully transparent. For the gradient I am using: http://angrytools.com/gradient/?0_800080,100_450045&0_0,100_100&l_180:
<mask id="myMask" x="0" y="0" width="100%" height="100%">
<rect x="0" y="0" width="100%" height="100%" fill="url(#grad1)" />
</mask>
In addition I don't understand why if I remove the fill="blue" attribute from my use element like so:
<use xlink:href="#myText" mask="url(#myMask)" />
The text appears black as if no gradient was applied. The gradient I defined is purple..
Thanks!
if you just want to apply your gradient to your text, there is no need to use masks, because gradients support the stop-opacity property.
<svg xmlns="http://www.w3.org/2000/svg" width="200px" height="200px">
<defs>
<linearGradient id="lgrad" x1="100%" y1="50%" x2="0%" y2="50%">
<stop offset="0%" style="stop-color:rgb(128,0,128);stop-opacity:0" />
<stop offset="100%" style="stop-color:rgb(69,0,69);stop-opacity:1" />
</linearGradient>
<text x="100" y="120" text-anchor="middle" id="myText" font-size="50">Hello</text>
</defs>
<use xlink:href="#myText" fill="url(#lgrad)" />
</svg>
you only need masks if you want to seperate the opacity from your fills:
<svg xmlns="http://www.w3.org/2000/svg" width="200px" height="200px">
<defs>
<linearGradient id="lgrad" x1="100%" y1="50%" x2="0%" y2="50%">
<stop offset="0" stop-color="black" />
<stop offset="1" stop-color="white" />
</linearGradient>
<mask id="myMask" x="0" y="0" width="100%" height="100%">
<rect x="0" y="0" width="100%" height="100%" fill="url(#lgrad)" />
</mask>
<text x="100" y="120" text-anchor="middle" id="myText" font-size="50">Hello</text>
</defs>
<g mask="url(#myMask)">
<use xlink:href=" #myText" transform="translate(0,-50) " fill="red " />
<use xlink:href="#myText" transform="translate(0,0)" fill="green" />
<use xlink:href="#myText" transform="translate(0,50)" fill="blue" />
</g>
</svg>
masks turn colors into opacity information. going from black(totally transparent) to white (totally opaque)
<svg xmlns="http://www.w3.org/2000/svg" width="200px" height="200px">
<defs>
<mask id="myMask" x="0" y="0" width="100%" height="100%">
<rect x="0" y="0" width="50%" height="50%" fill="white" />
<rect x="50%" y="0" width="50%" height="50%" fill="#333" />
<rect x="0%" y="50%" width="50%" height="50%" fill="#aaa" />
<rect x="50%" y="50%" width="50%" height="50%" fill="white" />
<circle cx="50%" cy="50%" r="15%" fill="black" />
</mask>
<text x="100" y="120" text-anchor="middle" id="myText" font-size="50">Hello</text>
</defs>
<rect x="0" y="0" width="100%" height="100%" fill="beige" />
<g mask="url(#myMask)">
<use xlink:href="#myText" transform="translate(0,-50)" fill="red" />
<use xlink:href="#myText" transform="translate(0,0)" fill="green" />
<use xlink:href="#myText" transform="translate(0,50)" fill="blue" />
</g>
</svg>
Have a code:
<svg id="view-svg-problem" xmlns="http://www.w3.org/2000/svg">
<defs>
<marker id='svg-marker-start' orient='auto' markerUnits="strokeWidth" markerWidth='10' markerHeight='10' refX='9' refY='5' class="svg-path-dec">
<rect x="1" y="1" width="8" height="8" />
<text x="3" y="7.1" font-weight="bold" font-family="Arial" font-size="5.5" fill="white" stroke="transparent">S</text>
</marker>
<marker id='svg-marker-end' orient='auto' markerUnits="strokeWidth" markerWidth='10' markerHeight='10' refX='5' refY='9' class="svg-path-dec">
<rect x="1" y="1" width="8" height="8" />
<text x="3" y="7.1" font-weight="bold" font-family="Arial" font-size="5.5" fill="white" stroke="transparent">F</text>
</marker>
</defs>
<path id="view-path-svg" class="svg-path" marker-start="url(#svg-marker-start)" marker-end="url(#svg-marker-end)" d="M 655,343 Q747,317 705.5,311 Q664,305 708,292 Q752,279 713.5,254 Q675,229 711.5,212.5 Q748,196 706.5,189 Q665,182 687,157.5 Q709,133 679.5,126.5 Q650,120 663,106 T676,92"></path>
</svg>
So result looks like on the picture:
Easy to see that marker-start aligns well with the path, but marker-end doesn't want to. Is there a solution to the problem?