问题
I have the following SVG that I use inline:
<svg width="30" height="23" viewBox="0 0 1398 1648"><path d="M1110.5 1008.81q37.8031 0 64.4934 -26.7359q26.6902 -26.7359 26.6902 -64.4477q0 -37.7118 -26.6902 -64.4477q-26.6902 -26.7359 -64.4934 -26.7359q-37.6729 0 -64.4282 26.7554q-26.7554 26.7554 -26.7554 64.4282q0 37.712 26.69 64.4478 q26.69 26.7358 64.4936 26.7358zM1085.83 924.412q10.313 0 17.6013 7.33401q7.28831 7.33401 7.28831 17.5547q0 10.352 -7.26875 17.6203q-7.26875 7.26836 -17.6209 7.26836q-10.3515 0 -17.6201 -7.26839q-7.26859 -7.26839 -7.26859 -17.6203 q0 -10.3513 7.26868 -17.62q7.26868 -7.26868 17.62 -7.26868zM267.895 1017.63q37.6729 0 64.4288 -26.7554q26.7558 -26.7554 26.7558 -64.4282q0 -37.6728 -26.7558 -64.4282q-26.7558 -26.7554 -64.4288 -26.7554q-37.6723 0 -64.4279 26.7555 q-26.7557 26.7555 -26.7557 64.4281q0 37.7118 26.6902 64.4477q26.6902 26.7359 64.4934 26.7359zM243.232 932.558q10.3513 0 17.62 7.26868q7.26868 7.26868 7.26868 17.62q0 10.3513 -7.26868 17.62q-7.26868 7.26868 -17.62 7.26868t-17.62 -7.26868t-7.26868 -17.62 q0 -10.3513 7.26868 -17.62q7.26868 -7.26868 17.62 -7.26868zM1297.39 766.48q58.4139 -70.152 77.5266 -147.337q19.1127 -77.1851 0.84226 -148.941q-18.2704 -71.756 -74.3439 -137.208q-56.0735 -65.4515 -140.522 -113.902q-84.4482 -48.4505 -205.702 -77.1412 q-121.254 -28.6907 -262.375 -28.6907h-1.35742q-141.109 0 -262.307 28.7563q-121.198 28.7563 -205.536 77.3021q-84.3387 48.5458 -140.302 114.099q-55.9633 65.5534 -74.1281 137.387q-18.1648 71.8339 1.01167 149.044q19.1765 77.2102 77.5926 147.309 q-14.4449 16.0507 -27.1514 37.1074q-50.7359 85.8689 -25.8788 182.368q24.8571 96.4988 110.727 147.071q85.8681 50.736 182.367 25.8788q96.499 -24.8572 147.07 -110.727q18.6952 2.39837 35.7852 -12.6865q17.09 -15.0849 32.7898 -38.5143 q15.6998 -23.4294 33.9779 -47.1394q18.2781 -23.71 47.3972 -40.6321q29.1191 -16.9221 65 -16.9221h4.52539q35.764 0 64.8242 16.8335q29.0602 16.8335 47.3461 40.4617q18.2859 23.6282 33.9778 47.0057q15.6919 23.3775 32.7387 38.5612 q17.0468 15.1837 35.6093 13.0324q6.28875 9.74797 14.708 20.3633q62.6399 77.4092 161.749 87.8468q99.1089 10.4376 176.513 -52.3234q77.4092 -62.6398 87.8468 -161.749q10.4376 -99.1094 -52.3234 -176.513zM802.327 680.953l-35.5234 -58.375h99.5557 l-35.5234 58.375q-2.03613 3.39404 -5.42969 5.43066q-14.2804 8.56928 -23.0791 -5.43066zM553.212 680.953l-35.5234 -58.375h99.5557l-35.5225 58.375q-2.03861 3.39572 -5.43066 5.43066q-14.2804 8.56928 -23.0791 -5.43066zM154.085 797.932 q53.2982 -47.1605 124.218 -42.9054q70.9195 4.25509 118.109 57.3859q43.2158 48.487 43.2158 113.584q0.355782 77.4576 -57.6963 128.743q-53.2625 47.2016 -124.219 42.99q-70.956 -4.21163 -118.109 -57.4705q-47.2018 -53.2623 -42.9904 -124.219 q4.21144 -70.9562 57.4708 -118.109zM1005.51 430.028q3.40299 2.55133 0.905273 5.88281q-1.81575 2.54331 -4.97754 1.35742q-159.065 -58.6016 -309.301 -58.6016q-150.237 0 -309.302 58.6016q-3.84115 1.70779 -5.42969 -2.2627q-1.18588 -3.16179 1.35742 -4.97754 q44.4155 -32.4157 158.521 -66.7777q114.106 -34.362 154.853 -34.362q40.7469 0 154.852 34.362q114.105 34.362 158.521 66.7776zM1239.01 804.267q43.2168 48.8367 43.2168 113.584q0.355778 77.4568 -57.6973 128.743q-53.2618 47.2016 -124.218 42.99 q-70.956 -4.21163 -118.109 -57.4705q-47.2016 -53.2625 -42.99 -124.219q4.21163 -70.956 57.4705 -118.109q53.2623 -47.2018 124.219 -42.9904q70.9562 4.21144 118.109 57.4708zM692.138 925.996q-32.3148 0 -60.1187 13.6801t-46.3427 33.2041 q-18.5387 19.524 -34.084 39.048q-15.5453 19.524 -30.3775 33.2041q-14.8322 13.6801 -28.1884 13.6801q29.4644 14.7821 79.2422 23.0971q49.7778 8.31494 84.8235 9.23882l35.0456 0.923882q14.8633 -0.00730341 38.7111 -0.925708q23.8478 -0.918405 77.3412 -9.23517 t83.0581 -23.0989q-13.3562 0 -28.1883 -13.6801q-14.8321 -13.6801 -30.3773 -33.2041q-15.5451 -19.524 -34.0837 -39.048q-18.5386 -19.524 -46.3424 -33.2041q-27.8038 -13.6801 -60.1186 -13.6801z" transform="translate(0,1792) scale(1, -1)" style="fill: currentcolor;"></path></svg>
Because I'm using it many times on my website, I don't want to copy the code above multiple times as this would be a waste of kb. Instead, I want to use the SVG as a file and load through img, object or iframe tags.
I have tried to save the above code into a svg file and to load it through img, object or iframe tags, with no success. Example:
<img src="gab.svg" />
Or :
<object type="image/svg+xml" data="gab.svg" class="logo">Gab</object>
I guess this should be obvious, but I don't get why it's not working. If I replace the SVG code above with a simpler SVG (like of a rectangle), then everything works fine.
What am I missing?
回答1:
It's an XML file, so at a minimim you need to tell the browser it is an SVG file. You do that by adding an xmlns
attribute to the root <svg>
element.
<svg xmlns="http://www.w3.org/2000/svg" ....
If you are doing an icon that has xlink:href
attributes, you should also add the xlink
namespace.
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" ....
That should be enough for browsers. But if you want to be pedantic, or are going to have the file processed by programs other than a browser, you may need to add the full XML preamble:
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" ....
来源:https://stackoverflow.com/questions/49041381/convert-an-inline-svg-into-a-svg-file