The Wayback Machine - https://web.archive.org/web/20241222201618/https://www.geeksforgeeks.org/how-to-use-svg-images-in-css-html/
Open In App

How to use SVG Images in HTML?

Last Updated : 20 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

SVG images are special graphics used in HTML that doesn’t lose quality when resized. They are great for icons, logos, and shapes because they stay clear at any size. To use SVG images in HTML, add them with <img src=" image.svg"> or paste SVG code directly with <SVG>.

1. Using SVG Image with <img> Tag

The <img> tag is used to include SVG images in your HTML, just like any other image format. You specify the image file using the src attribute. If you don’t set a specific size, the SVG will appear at its original size. To change the size, you can use the width and height attributes or style it with CSS.

HTML
<img src="https://media.geeksforgeeks.org/gfg-gg-logo.svg" 
	 alt="GFG Logo">

Output

Output
Svg Images

2. Using SVG Image with CSS background-image Property

To use an SVG image as a background in CSS, you can set the image URL in a class using background-image. The .mySVG class in the example below adds the SVG as a background, makes sure it doesn’t repeat, and sets the height to 200px.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <style>
        .mySVG {
            background-image: url('https://media.geeksforgeeks.org/gfg-gg-logo.svg');
            background-repeat: no-repeat;
            height: 200px;
        }
    </style>
</head>

<body>
    <div class="mySVG"></div>
</body>

</html>

Output

Output

3. How to use an SVG as an <object>

The <object> tag allows you to embed an external SVG file directly into your webpage. This method displays the SVG inline, and you can manipulate it with CSS or JavaScript.

HTML
<object data="https://media.geeksforgeeks.org/gfg-gg-logo.svg"></object>

Output

svgobj
Using SVG as an object

How to Use SVG Images in CSS & HTML? - FAQs

How do I add an SVG image in HTML?

You can add an SVG image in HTML using the <img> tag or by embedding the SVG code directly within your HTML document.

Can CSS be applied to an SVG image?

Yes, you can style SVG elements directly using CSS, just as you would with HTML elements, by targeting them with classes, IDs, or inline styles.

How do I use an SVG as a background image in CSS?

You can set an SVG as a background image using the background-image property in CSS, just like any other image format.

Can I animate SVG images using CSS?

Yes, SVG images can be animated with CSS using properties like transform, opacity, and keyframes.

How do I embed external SVG files in HTML?

You can use the <object>, <embed>, or <img> tags to embed external SVG files in your HTML code.


Next Article

Similar Reads

three90RightbarBannerImg