What is SVG?
SVG stands for Scalable Vector Graphics. It is an XML-based markup language used to create two-dimensional vector graphics that can be scaled and resized without any loss in quality. SVG is widely used on the web and supported by all modern web browsers. Unlike raster graphics formats like JPEG or PNG, which use a grid of pixels to represent images, SVG uses mathematical formulas to describe shapes and paths. this makes SVG files lightweight and resolution-independent, allowing them to be resized and rendered smoothly on screens of different sizes and resolutions.
SVG supports a wide range of graphic elements, including lines, curves, shapes, text, gradients, and transformations. It also supports interactive elements such as hyperlinks and animation, making it a powerful tool for creating dynamic and interactive web content. One of the key advantages of SVG is its ability to be easily manipulated using CSS and JavaScript. Styles and animations can be applied to SVG elements using CSS, while JavaScript can be used to dynamically modify and interact with SVG elements, enabling advanced interactivity and responsiveness.
SVG is not only used for web graphics but also finds applications in other areas, such as data visualization, icon design, user interfaces, and print media. It provides a flexible and accessible format for creating visually appealing and interactive content across various platforms and devices.
Getting started: A simple example
Let us dive straight in with a simple example. Take a look at the following code.
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="100">
<rect x="50" y="35" width="200" height="150" fill="green" />
Output
The element is the container for the SVG content. Specify the XML namespace for SVG using the xmlns attribute. the width and height attributes define the dimensions of the SVG viewport, which is the area where the SVG content is displayed inside the element; we have an element that represents a rectangle shape.
The x and y attributes determine the position of the top-left corner of the rectangle relative to the SVG viewport. In this example, the rectangle is positioned at x=50 and y=35. The width and height attributes specify the size of the rectangle. In this case, the width is 200 pixels, and the height is 150 pixels. The fill attribute sets the fill color of the rectangle. In this example, we set it to "red", resulting in a red rectangle. When you display this SVG code in a web browser, you should see a red rectangle with the specified dimensions and position within the SVG viewport.
Basic properties of SVG files
SVG files have several basic properties that define their characteristics and behavior. Here are some of the key properties of SVG files:
- Scalability: SVG files are scalable, meaning they can be resized without any loss in quality. This is because SVG uses mathematical formulas to describe shapes and paths rather than a grid of pixels like raster graphics formats.
- Resolution independence: Since SVG files are vector-based, they are resolution independent. This means they can be displayed at different resolutions and on different devices without losing sharpness or clarity.
- XML-based format: SVG files are written in XML (eXtensible Markup Language), making them human-readable and easily editable. They can be created and modified using text editors or specialized SVG editing software.
- Shape and path-based: SVG supports various geometric shapes such as rectangles, circles, ellipses, lines, and polygons. It also allows the creation of complex paths using straight lines, curves, and arcs.
- Styling with CSS: SVG elements can be styled using CSS (Cascading Style Sheets). This enables the application of colors, gradients, strokes, opacity, and other visual properties to the SVG content.
- Interactivity: SVG files can incorporate interactivity using JavaScript or SMIL (Synchronized Multimedia Integration Language). Elements within the SVG can respond to user actions, trigger animations, or handle events.
- Animation: SVG supports animation through the use of CSS animations, SMIL animations, or JavaScript libraries. This allows elements to move, transform, fade, or change over time, creating dynamic and engaging visual effects.
- Accessibility: SVG files can be made accessible by providing alternative text descriptions (using the aria-label attribute) for screen readers and other assistive technologies. This ensures that visually impaired users can understand and interact with the SVG content.
- Support across platforms: SVG is supported by all modern web browsers, making it compatible with different operating systems and devices. It can also be embedded in other file formats, such as HTML, CSS, and PDF, making it versatile and widely usable.
These properties make SVG files a popular choice for creating scalable graphics, icons, data visualizations, and interactive web content.
How we can apply Multi Icons in our Project?
<html>
<head>
<title>SVG Shapes Example</title>
</head>
<body>
<h1>SVG Shapes Example</h1>
<svg xmlns="http://www.w3.org/2000/svg" width="400" height="200">
<polygon points="200,150 250,50 300,150" fill="red" />
<rect x="50" y="50" width="100" height="100" fill="green" />
<ellipse cx="350" cy="150" rx="50" ry="30" fill="pink" />
<line x1="50" y1="150" x2="150" y2="50" stroke="pink" stroke-width="2" />
<circle cx="250" cy="100" r="50" fill="red" />
</svg>
</body>
</html>
Output
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200" viewBox="0 0 200 200">
<path d="M20 100 L100 20 L180 100 L180 180 L20 180 Z" fill="grey" />
<rect x="40" y="120" width="120" height="60" fill="black" />
</svg>
Where we can create SVG if we don't want to do the code for SVG?
- Adobe Illustrator: Adobe Illustrator is a professional vector graphics editor that supports SVG format. It provides a wide range of tools and features for creating and editing SVG files, including drawing tools, shape manipulation, color management, and exporting options.
- Inkscape: Inkscape is a free and open-source vector graphics editor that supports SVG as its native format. It offers a powerful set of tools for creating and editing SVG files, including drawing tools, object manipulation, text editing, and various effects.
- Vectr: Vectr is a free online vector graphics editor that supports the SVG format. It provides a user-friendly interface and basic drawing tools for creating and editing SVG files. Vectr is accessible through a web browser and also offers desktop and mobile apps.
- Gravit Designer: Gravit Designer is a feature-rich vector design tool available as a desktop application, web app, or Chrome app. It supports SVG format and provides an extensive set of tools for creating and editing vector graphics, including shape tools, typography, effects, and exporting options.
- SVG-edit: SVG-edit is a free and open-source web-based SVG editor. It allows you to create and modify SVG files directly in your web browser. SVG-edit offers a simple interface with basic drawing tools and features for working with SVG files.
- Figma: Figma is a web-based design and prototyping tool that supports the SVG format. It offers collaborative features and a robust set of design tools for creating and editing vector graphics. Figma allows teams to work together on SVG files in real time.
- Optimizers: There are various online SVG optimization tools available to reduce the file size of SVGs without sacrificing quality. These tools remove unnecessary metadata, clean up code, and compress the SVG data. Examples include SVGO, SVGOMG, and SVG Optimizer.
- Text Editors: Any text editor can be used to create and edit SVG files since SVG is an XML-based format. Popular text editors like Visual Studio Code, Sublime Text, or Atom provide syntax highlighting and XML formatting support for working with SVG code directly.
How to Manipulate SVG with JavaScript?
<svg id="my-svg" xmlns="http://www.w3.org/2000/svg" width="200" height="200" viewBox="0 0 200 200">
<circle id="my-circle" cx="100" cy="100" r="50" fill="blue" />
</svg>
var svg = document.getElementById('my-svg');
var circle = document.getElementById('my-circle');
// Add a click event listener to the circle
circle.addEventListener('click', function() {
// Change the fill color of the circle to red
circle.setAttribute('fill', 'red');
// Increase the circle's radius by 10
var newRadius = parseInt(circle.getAttribute('r')) + 10;
circle.setAttribute('r', newRadius);
// Rotate the entire SVG element by 45 degrees
svg.setAttribute('transform', 'rotate(45)');
});
The SVG element has an ID of "my-svg", and the circle element within the SVG has an ID of "my-circle". You can access SVG elements by their IDs using getElementById(). we add a click event listener to the circle element using addEventListener(). When the circle is clicked, the defined function will be executed inside the event listener function; we use setAttribute() to change the fill color of the circle to red. We pass the attribute name ('fill') and the new value ('red') as arguments we also increase the radius of the circle by 10. We retrieve the current radius using getAttribute(), convert it to an integer, add 10, and then set the new radius using setAttribute().
Finally, we rotate the entire SVG element by 45 degrees using the setAttribute() method on the SVG element. We pass the attribute name ('transform') and the transformation value ('rotate(45)').
How to CSS with SVG Icon?
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200" viewBox="0 0 200 200">
<circle cx="100" cy="100" r="80" />
</svg>
circle {
fill: blue;
stroke: red;
stroke-width: 5;
}
The SVG element contains an <circle> element, which represents a circle shape. We apply CSS styling to the <circle> element using a CSS selector. In this case, we select the <circle> element using the element selector circle, set the fill property to blue, which determines the fill color of the circle, then set the stroke property to red, which determines the stroke color of the circle. also, the stroke-width property to 5, which determines the thickness of the stroke.
When we apply this CSS code to the HTML with the SVG element, the circle will have a blue fill color, a red stroke color, and a stroke width of 5 pixels can further customize the styling by adding additional CSS properties and values. For example, you can change the size, position, and opacity or add animations to the SVG element or its child elements. CSS provides a wide range of styling options to enhance the appearance of SVG graphics within your web page or application.
Conclusion
SVG icons provide numerous benefits for web development, and scalability ensures they look sharp at any size and on high-resolution displays. also, they are lightweight, leading to faster page load times and reduced bandwidth usage. SVG icons are responsive and adaptable to various screen sizes, and devices also support Retina displays, maintaining their quality even at higher resolutions. The ability to edit and customize SVG icons offers design flexibility. Interactivity can be added using CSS and JavaScript, enhancing user experiences. SVG icons can be made accessible to users with disabilities through semantic descriptions and ARIA attributes. Integrating SVG icons into web projects is easy, allowing for seamless inclusion in HTML markup. Overall, SVG icons are a powerful and versatile choice for creating visually appealing and scalable icons in modern web design.