Vite / Vite and Assets

Using SVGs in Vite

In this tutorial, we'll explore how to import and use SVG files in your Vite projects. SVGs provide a way to create high-quality, scalable graphics that can be manipulated with CS…

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Explains how to handle static assets and images in a Vite project

Using SVGs in Vite

1. Introduction

In this tutorial, we'll delve into how to import and utilize SVG files in your Vite projects. SVGs (Scalable Vector Graphics) offer a means to create scalable, high-quality graphics that can be manipulated with CSS and JavaScript.

By the end of this tutorial, you will learn:
- How to import SVGs into your Vite project
- Manipulating SVGs using CSS and JavaScript

Prerequisites:
- Basic knowledge of HTML, CSS, and JavaScript
- Familiarity with Vite

2. Step-by-Step Guide

Importing SVGs

In your Vite project, you can import SVGs just like any other JavaScript module:

import myLogo from './my-logo.svg';

By doing this, you are actually importing the URL of the SVG file. This URL can be used in your HTML in img tags:

<img src={myLogo} alt="My Logo">

Manipulating SVGs with CSS

SVGs are made up of XML-based vector images and are treated as Document Object Model (DOM) elements in the browser. This means they can be styled and manipulated using CSS:

<style>
  svg {
    width: 100px;
    height: 100px;
    fill: red;
  }
</style>

Manipulating SVGs with JavaScript

Similarly, you can manipulate SVGs using JavaScript by accessing the SVGs' DOM API:

const svgElement = document.querySelector('svg');
svgElement.style.fill = 'red';

3. Code Examples

Example 1: Importing SVGs

// Import the SVG file
import myLogo from './my-logo.svg';

// Now `myLogo` is the URL of the SVG file, you can use it in your HTML
console.log(myLogo);  // Expected output: "/assets/my-logo.49a45d.svg"

Example 2: Styling SVGs with CSS

<style>
  /* Change the color and the size of the SVG */
  svg {
    width: 100px;
    height: 100px;
    fill: red;
  }
</style>

Example 3: Manipulating SVGs with JavaScript

// Select the SVG element
const svgElement = document.querySelector('svg');

// Change the color of the SVG
svgElement.style.fill = 'red';

4. Summary

In this tutorial, we've learned how to import SVGs into a Vite project and how to manipulate them using CSS and JavaScript.

Next, you could explore more about SVGs and how to animate them using CSS or JavaScript.

Here are some resources for further learning:
- SVG Tutorial - W3Schools
- SVG Basics—How to Create Simple Shapes and Lines - Tuts+

5. Practice Exercises

  1. Import an SVG image into your Vite project and display it on your webpage.
  2. Use CSS to change the color and size of the SVG image.
  3. Use JavaScript to change the color of the SVG image when a button is clicked.

Solutions:

  1. Check the code examples above for reference.
  2. Use the fill property in CSS to change the color, and width and height properties to change the size.
  3. Attach an event listener to the button and change the fill property of the SVG using JavaScript when the button is clicked.

Remember, practice makes perfect! Keep experimenting with different SVGs and manipulating them using CSS and JavaScript.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Word Counter

Count words, characters, sentences, and paragraphs in real-time.

Use tool

Image Converter

Convert between different image formats.

Use tool

JWT Decoder

Decode and validate JSON Web Tokens (JWT).

Use tool

Watermark Generator

Add watermarks to images easily.

Use tool

JavaScript Minifier & Beautifier

Minify or beautify JavaScript code.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help