简体   繁体   中英

SVG with <use> tag not visible

I'm relatively new to SVGs and I've been exploring the various ways to present them in-line. Each seems to have its own pros and cons, but the <svg> tag itself seems to have the biggest drawback - it doesn't render!

My code is very simple (referencing a public URL SVG):

<div>
    <svg xmlns="http://www.w3.org/2000/svg">
        <use xlink:href="https://svgshare.com/i/7q9.svg"></use>
    </svg>
</div>

To make sure the SVG itself works using other tags, I created the following jsFiddle: http://jsfiddle.net/dykv8swr/24/ .

What am I missing?

BONUS: As a secondary question - the fill:currentColor doesn't seem to work on any SVG type, can't seem to figure that out either.

Thanks in advance.

If you are using fill: currentColor then you have to defined some color property on SVG's ancestor.

That first problem can be related to missing viewBox attribute on your <svg> .

I didn't have any problems with inline SVG and I used both things, which you described.

EDIT: I updated your fiddle to show how to and not to use inline SVG.

 body { display: flex; } div { border: 1px solid black; padding: 25px; color: red; flex: 0 0 100px; } svg, img, object, ico { height: 100px; width: 100px; cursor: pointer; fill: red; } .icon { display: block; background-image: url(https://svgshare.com/i/7q9.svg); background-repeat: no-repeat; background-size: 100px; background-position: center; } 
 <svg> <symbol xmlns="http://www.w3.org/2000/svg" id="logo" viewBox="0 0 780 540"> <title>Lorem ipsum</title> <g id="#595858ff"> <path fill="#595858" opacity="1.00" d=" M 291.35 46.42 C 324.31 31.16 360.58 22.59 396.99 23.26 C 425.41 23.62 453.62 29.53 480.15 39.60 C 517.37 51.58 550.60 74.31 577.51 102.45 C 604.03 130.08 623.78 163.68 637.41 199.35 C 624.57 213.97 611.87 228.70 599.05 243.33 C 588.86 208.07 571.74 174.73 548.78 146.08 C 524.84 116.24 492.83 92.37 456.17 80.76 C 422.64 69.88 386.49 69.19 351.94 75.14 C 335.33 77.69 319.27 83.13 304.03 90.15 C 272.73 104.57 245.04 126.93 224.90 154.94 C 207.75 178.69 196.03 206.15 189.34 234.60 C 185.70 252.62 185.20 271.16 186.79 289.44 C 190.93 334.66 208.08 378.04 232.45 416.09 C 220.18 426.84 208.15 437.87 195.89 448.63 C 180.37 424.97 168.14 399.23 158.60 372.62 C 147.20 340.67 140.46 306.98 139.66 273.03 C 139.41 245.33 143.39 217.49 152.26 191.20 C 159.22 170.41 169.18 150.66 181.42 132.49 C 186.44 124.67 193.03 118.05 199.47 111.42 C 226.07 84.73 257.12 62.29 291.35 46.42 Z"/> <path fill="#595858" opacity="1.00" d=" M 683.61 290.59 C 719.91 253.75 749.66 210.15 768.94 162.07 C 766.41 178.67 760.72 194.63 754.42 210.14 C 739.31 246.27 717.62 279.43 692.10 309.05 C 645.06 363.57 585.23 405.83 521.29 438.30 C 443.90 477.40 360.29 502.92 275.51 520.21 C 251.03 525.58 225.97 527.62 200.99 529.05 C 175.32 530.21 149.50 530.02 123.98 526.73 C 101.23 523.66 78.17 518.51 58.20 506.74 C 43.81 498.31 31.63 485.09 27.15 468.75 C 21.31 448.16 26.27 426.36 33.78 406.88 C 46.29 375.46 65.69 347.36 86.32 320.77 C 71.12 343.74 57.98 368.94 53.77 396.45 C 51.11 413.58 53.36 432.12 63.29 446.69 C 73.89 462.53 91.35 472.18 109.06 478.06 C 129.68 484.88 151.43 487.46 173.03 488.61 C 194.35 489.56 215.75 489.05 237.02 487.29 C 283.61 481.34 329.63 471.30 374.84 458.63 C 453.52 436.24 530.27 404.88 598.80 359.82 C 629.29 339.67 658.04 316.74 683.61 290.59 Z"/> </g> <g id="#739e3aff"> <path fill="currentColor" opacity="1.00" d=" M 609.38 62.57 C 631.46 62.87 653.50 65.98 674.90 71.36 C 693.29 76.26 711.65 83.17 726.55 95.32 C 737.58 104.19 745.91 116.85 748.01 131.00 C 750.65 148.26 745.45 165.61 738.05 181.06 C 724.99 207.79 705.75 230.89 685.56 252.51 C 670.27 268.59 654.37 284.14 637.24 298.28 C 608.14 322.31 576.52 343.18 543.73 361.81 C 473.46 401.44 397.34 431.28 318.14 447.17 C 263.52 457.96 207.12 461.78 151.80 454.25 C 145.21 453.43 138.69 452.12 132.10 451.34 C 153.62 451.59 175.15 450.90 196.61 449.13 C 206.62 440.25 216.44 431.14 226.36 422.17 C 228.63 420.16 230.69 417.84 233.46 416.53 C 235.29 412.77 239.43 410.81 242.25 407.83 C 239.27 349.73 236.29 291.63 233.39 233.52 C 247.46 220.36 261.60 207.25 275.72 194.14 C 290.18 199.05 304.64 203.97 319.09 208.92 C 319.11 264.05 319.05 319.18 319.12 374.31 C 323.31 371.92 327.52 369.56 331.71 367.18 C 331.50 287.18 331.18 207.18 330.84 127.18 C 345.27 118.94 359.76 110.79 374.17 102.53 C 388.04 116.47 401.83 130.49 415.72 144.41 C 415.70 179.25 415.33 214.09 415.21 248.92 C 415.19 290.74 414.57 332.55 414.62 374.37 C 418.54 373.66 422.47 373.02 426.40 372.38 C 426.39 305.07 426.40 237.77 426.40 170.47 C 445.42 177.38 464.45 184.28 483.49 191.14 C 483.53 247.28 483.50 303.41 483.51 359.55 C 486.77 357.68 490.04 355.83 493.30 353.95 C 493.14 310.70 492.58 267.45 492.46 224.20 C 502.00 221.59 511.42 218.56 520.92 215.81 C 535.08 226.56 549.12 237.47 563.27 248.23 C 563.27 268.03 563.32 287.83 563.24 307.63 C 593.12 285.02 621.49 260.37 647.61 233.50 C 659.05 219.91 669.05 205.08 677.42 189.41 C 685.94 173.11 692.87 155.49 694.06 136.97 C 694.96 123.68 691.94 109.87 684.17 98.89 C 675.79 86.90 663.02 78.74 649.68 73.23 C 636.79 67.93 623.14 64.63 609.38 62.57 Z"/> </g> </symbol> </svg> <!-- SVG + USE ERROR! --> <div> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 780 540"> <use xlink:href="#logo"></use> </svg> </div> <div> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 780 540"> <g id="#595858ff"> <path fill="#595858" opacity="1.00" d=" M 291.35 46.42 C 324.31 31.16 360.58 22.59 396.99 23.26 C 425.41 23.62 453.62 29.53 480.15 39.60 C 517.37 51.58 550.60 74.31 577.51 102.45 C 604.03 130.08 623.78 163.68 637.41 199.35 C 624.57 213.97 611.87 228.70 599.05 243.33 C 588.86 208.07 571.74 174.73 548.78 146.08 C 524.84 116.24 492.83 92.37 456.17 80.76 C 422.64 69.88 386.49 69.19 351.94 75.14 C 335.33 77.69 319.27 83.13 304.03 90.15 C 272.73 104.57 245.04 126.93 224.90 154.94 C 207.75 178.69 196.03 206.15 189.34 234.60 C 185.70 252.62 185.20 271.16 186.79 289.44 C 190.93 334.66 208.08 378.04 232.45 416.09 C 220.18 426.84 208.15 437.87 195.89 448.63 C 180.37 424.97 168.14 399.23 158.60 372.62 C 147.20 340.67 140.46 306.98 139.66 273.03 C 139.41 245.33 143.39 217.49 152.26 191.20 C 159.22 170.41 169.18 150.66 181.42 132.49 C 186.44 124.67 193.03 118.05 199.47 111.42 C 226.07 84.73 257.12 62.29 291.35 46.42 Z"/> <path fill="#595858" opacity="1.00" d=" M 683.61 290.59 C 719.91 253.75 749.66 210.15 768.94 162.07 C 766.41 178.67 760.72 194.63 754.42 210.14 C 739.31 246.27 717.62 279.43 692.10 309.05 C 645.06 363.57 585.23 405.83 521.29 438.30 C 443.90 477.40 360.29 502.92 275.51 520.21 C 251.03 525.58 225.97 527.62 200.99 529.05 C 175.32 530.21 149.50 530.02 123.98 526.73 C 101.23 523.66 78.17 518.51 58.20 506.74 C 43.81 498.31 31.63 485.09 27.15 468.75 C 21.31 448.16 26.27 426.36 33.78 406.88 C 46.29 375.46 65.69 347.36 86.32 320.77 C 71.12 343.74 57.98 368.94 53.77 396.45 C 51.11 413.58 53.36 432.12 63.29 446.69 C 73.89 462.53 91.35 472.18 109.06 478.06 C 129.68 484.88 151.43 487.46 173.03 488.61 C 194.35 489.56 215.75 489.05 237.02 487.29 C 283.61 481.34 329.63 471.30 374.84 458.63 C 453.52 436.24 530.27 404.88 598.80 359.82 C 629.29 339.67 658.04 316.74 683.61 290.59 Z"/> </g> <g id="#739e3aff"> <path fill="currentColor" opacity="1.00" d=" M 609.38 62.57 C 631.46 62.87 653.50 65.98 674.90 71.36 C 693.29 76.26 711.65 83.17 726.55 95.32 C 737.58 104.19 745.91 116.85 748.01 131.00 C 750.65 148.26 745.45 165.61 738.05 181.06 C 724.99 207.79 705.75 230.89 685.56 252.51 C 670.27 268.59 654.37 284.14 637.24 298.28 C 608.14 322.31 576.52 343.18 543.73 361.81 C 473.46 401.44 397.34 431.28 318.14 447.17 C 263.52 457.96 207.12 461.78 151.80 454.25 C 145.21 453.43 138.69 452.12 132.10 451.34 C 153.62 451.59 175.15 450.90 196.61 449.13 C 206.62 440.25 216.44 431.14 226.36 422.17 C 228.63 420.16 230.69 417.84 233.46 416.53 C 235.29 412.77 239.43 410.81 242.25 407.83 C 239.27 349.73 236.29 291.63 233.39 233.52 C 247.46 220.36 261.60 207.25 275.72 194.14 C 290.18 199.05 304.64 203.97 319.09 208.92 C 319.11 264.05 319.05 319.18 319.12 374.31 C 323.31 371.92 327.52 369.56 331.71 367.18 C 331.50 287.18 331.18 207.18 330.84 127.18 C 345.27 118.94 359.76 110.79 374.17 102.53 C 388.04 116.47 401.83 130.49 415.72 144.41 C 415.70 179.25 415.33 214.09 415.21 248.92 C 415.19 290.74 414.57 332.55 414.62 374.37 C 418.54 373.66 422.47 373.02 426.40 372.38 C 426.39 305.07 426.40 237.77 426.40 170.47 C 445.42 177.38 464.45 184.28 483.49 191.14 C 483.53 247.28 483.50 303.41 483.51 359.55 C 486.77 357.68 490.04 355.83 493.30 353.95 C 493.14 310.70 492.58 267.45 492.46 224.20 C 502.00 221.59 511.42 218.56 520.92 215.81 C 535.08 226.56 549.12 237.47 563.27 248.23 C 563.27 268.03 563.32 287.83 563.24 307.63 C 593.12 285.02 621.49 260.37 647.61 233.50 C 659.05 219.91 669.05 205.08 677.42 189.41 C 685.94 173.11 692.87 155.49 694.06 136.97 C 694.96 123.68 691.94 109.87 684.17 98.89 C 675.79 86.90 663.02 78.74 649.68 73.23 C 636.79 67.93 623.14 64.63 609.38 62.57 Z"/> </g> </svg> </div> <!-- img option --> <div> <img src="https://svgshare.com/i/7q9.svg" /> </div> <!-- object option --> <div> <object data="https://svgshare.com/i/7q9.svg"></object> </div> <!-- background-image + URL --> <div> <ico class="icon"></ico> </div> 

When coding SVG in-line for interactive purposes, you have to include the entire code, not a reference to it. As I understand <use> specifies reusable elements within an SVG .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM