SVG in HTML Pages
SVG files can be embedded into HTML documents through the following tags:,
SVG code can be embedded directly into an HTML page, or you can link directly to an SVG file.
Using the < img> tag
Through tag can embed an SVG image into an HTML page as an image; you can use the src attribute to specify the path to the SVG file, and you can also set the width and height attributes to specify the image's width and height.
Syntax:
<img src="example.svg" alt="SVG Image" width="200" height="200">
Using
<object> tag is used to embed external resources into an HTML page, you can use data attribute specifies the path to the SVG file,type attribute specifies the MIME type of the resource.
Browsers that support SVG will display the SVG image directly, while browsers that do not will display alternative content.
Syntax:
<object data="example.svg" type="image/svg+xml" width="200" height="200">
Your browser does not support SVG
</object>
Using
<iframe> tag is used to embed another HTML document in an HTML page. You can use src attribute specifies the path to the SVG file, and sets width and height attribute to specify the width and height of the iframe.
Syntax:
<iframe src="example.svg" width="200" height="200"></iframe>
Embed SVG Code Directly in HTML
Embed SVG code directly in an HTML page; SVG code can be placed in <body> tag or in another suitable location.
This approach mixes the SVG image with the HTML content, allowing you to edit and adjust the SVG image directly in the HTML page.
Example
<
svg
xmlns
=
"
http://www.w3.org/2000/svg
"
version
=
"
1.1
"
<
circle
cx
=
"
100
"
cy
=
"
50
"
r
=
"
40
"
stroke
=
"
black
"
stroke-width
=
"
2
"
fill
=
"
red
"
/>
</
svgTry it »
Link to an SVG File
You can also use tag to link to an SVG file:
<a href="circle1.svg">View SVG File</a>
Result:View SVG File
Using CSS background images
Through CSS's background-image attribute, you can embed an SVG image as a background image into an HTML element. This method is suitable for cases where you need to control the background image style in CSS.
Example
.svg-bg
{
width:
200
px
;
height:
200
px
;
background-image:
url
('
circle1
.
svg
')
;
background-size:
cover
;
}
Try it »