Using SVG Images in a Website
SVG images can be added to HTML pages in several ways. This image format also opens in text editors and can be edited as text. CSS can be added to the SVG in the text version of the file.
Placing an SVG in an image element
When you do not need to edit the individual colors & etc. for the SVG, you can place the SVG in an image element or as a background image like normal.
<figure class="overlap">
<img src="images/tree.svg" style="width:100%" alt="This image is missing" title="Example SVG"
>
</figure>
The aspect ratio will remain locked unless you disable the aspect ratio in the SVG code. This can be done by opening your SVG in Brackets or another editor and adding preserveAspectRatio="none" inside the opening SVG tag:
<svg version="1.1"
x="0px" y="0px" width="500px" height="500px" viewBox="-102.629 -40.868 500 500"
overflow="visible" enable-background="new -102.629 -40.868 500 500" xml:space="preserve" preserveAspectRatio="none">
Adding SVG code directly in HTML
Adding the code of your SVG directly into your HTML has benefits:
- Colors can be changed directly in the HTML
- Allows classes and ids to be added inside the SVG directly in the HTML
- Allows the stylesheet to effect the SVG
and disadvantages:
- Adds to the length/complexity of the page
- Prevents the image from being cached by the browser
<figure class="overlap">
<svg version="1.1"
x="0px" y="0px" width="500px" height="500px" viewBox="-102.629 -40.868 500 500"
overflow="visible" enable-background="new -102.629 -40.868 500 500" xml:space="preserve" preserveAspectRatio="none">
<defs>
</defs>
<linearGradient id="SVGID_1_" gradientUnits="userSpaceOnUse" x1="234.4741" y1="285.9443" x2="67.0762" y2="285.9443">
<stop offset="0" style="stop-color:#FFECD0"/>
<stop offset="0.1305" style="stop-color:#D9C5AB"/>
<stop offset="0.3599" style="stop-color:#9D886E"/>
<stop offset="0.5699" style="stop-color:#6D573F"/>
<stop offset="0.7532" style="stop-color:#4B331D"/>
<stop offset="0.9028" style="stop-color:#361E08"/>
<stop offset="1" style="stop-color:#2E1600"/>
</linearGradient>
<path fill="url(#SVGID_1_)" d="M94.199,175.942c0,0,63.527,183.086-6.327,204.101c-69.853,21.015,58.662-1.915,58.621-16.233
c0,0,26.857,37.359,8.254,52.848c18.439-0.192,34.328-36.696,24.24-47.004c17.971,6.862,36.318,28.649,55.486,27.479
c-2.836-8.79-44.387-45.234-47.066-62.494c-3.691-21.222-28.096-176.623,11.453-170.312c39.549,6.312-43.049-9.095-43.049-9.095
s-23.979,40.927-15.338,58.084c-17.103-19.789-22.233-50.424-22.233-50.424L94.199,175.942z"/>
<linearGradient id="SVGID_2_" gradientUnits="userSpaceOnUse" x1="157.5142" y1="7.4854" x2="121.4602" y2="192.9669">
<stop offset="0" style="stop-color:#B4FF8B"/>
<stop offset="1" style="stop-color:#0B2E00"/>
</linearGradient>
<path class="changecolor" fill="url(#SVGID_2_)" d="M98.125,178.495c16.369,28.461,81.505,13.167,68.427-18.057c32.05,14.928,75.332,9.604,62.408-19.013
c31.493,10.308,64.81-23.084,49.424-40.98c13.693-19.429-13.642-65.719-36.107-48.304c-13.371-26.668-58.358-45.23-66.769-22.011
c-15.724-16.422-71.9-48.813-76.36-15.979c-18.836-20.74-80.007-2.803-66.859,24.84c-33.376-2.814-46.478,55.171-11.966,63.769
c-20.134,25.132-6.747,65.142,20.448,53.943C52.772,197.99,98.125,178.495,98.125,178.495z"/>
</svg>
</figure>
At this point, you can add CSS inline or add classes/ids to use with the external stylesheet. If you are adding classes/ids, you most likely need to add them to the opening tag for path. The CSS properties include:
- fill (instead of background)
- stroke (to target the outline)
- stroke-width