SVG and html: interactivity and vector graphics on your website
Using Scalable Vector Graphics (SVG) in combination with HTML allows you to add interactivity and eye-catching visual elements to your website. SVG is an XML-based image format that uses vector graphics to represent shapes, text and other visual elements. With HTML, you can easily integrate SVG files into your code and take advantage of the interactive features it offers to enhance the user experience.
Why use SVG?
Vector graphics have the advantage of being scalable without losing quality, making them ideal for use on different devices and screen sizes. By using SVG, you can create eye-catching graphics and animations that scale smoothly to any resolution.
In addition, SVG supports a number of visual effects, such as gradients, drop shadows and transitions. You can manipulate SVG elements using CSS and JavaScript to create interactivity and add dynamism to your website.
Integrating SVG into HTML
To embed an SVG file on your website, use the SVG element. <img>
attribute in HTML together with the src
that links to the SVG file:
<img src="mi_archivo.svg" alt="Mi SVG">
This way, the SVG file will be displayed on your web page. You can adjust the size of the SVG using CSS, as you would any other image.
Adding interactivity with CSS and JavaScript
One of the advantages of using SVG is its ability to be manipulated and animated. using CSS and JavaScript. You can use CSS for styling to elements within the SVG, such as changing colours, borders or adding shadows.
.mi-elemento-svg {
fill: blue; /* Cambia el color de relleno a azul */
stroke: red; /* Cambia el color del borde a rojo */
box-shadow: 2px 2px 5px #000; /* Agrega una sombra al SVG */
}
In addition to styles, you can also add interactivity using JavaScript. You can manipulate and control SVG elements through events, such as clicking on an element to perform a specific action.
const miElementoSVG = document.querySelector('.mi-elemento-svg');
miElementoSVG.addEventListener('click', () => {
alert('Haz hecho clic en el elemento SVG!');
});
This is just an introduction to using SVG and HTML to add interactivity and vector graphics to your website. There are many more possibilities and features you can explore and apply to create unique and engaging experiences for your users.
We encourage you to research more about SVG and try out different effects and techniques to customise and enhance your web designs.