In this tutorial, we'll dive into the world of Scalable Vector Graphics (SVGs) and how to use them effectively in a Bootstrap project. We will cover how to add SVGs as inline HTML elements, how to style them using CSS, and manipulate them with JavaScript.
By the end of this tutorial, you will be able to:
Prerequisites
Before starting this tutorial, ensure you have a basic understanding of HTML, CSS, JavaScript, and how to set up a Bootstrap project.
SVGs are XML-based vector images. Since they are vector-based, they can be scaled to any size without losing quality. SVGs can be used directly in your HTML, styled with CSS, and manipulated with JavaScript.
SVGs can be added directly to your HTML document using the <svg>
tag. You can use the width
and height
attributes to set the size of the SVG.
Here's an example of an SVG in HTML:
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>
This code creates a circle with a radius of 40 (r="40"), centered at (50,50) (cx="50", cy="50"), with a green outline (stroke="green") that is 4 units wide (stroke-width="4"), and filled with yellow color (fill="yellow").
You can style SVGs similarly to how you style HTML elements with CSS. To do this, you can assign a class to the SVG and use that class in your CSS.
<!-- HTML -->
<svg width="100" height="100" class="my-svg">
<circle cx="50" cy="50" r="40" />
</svg>
/* CSS */
.my-svg circle {
stroke: green;
stroke-width: 4;
fill: yellow;
}
This will give you the same result as the previous example, but using CSS instead of inline HTML attributes.
You can manipulate SVGs using JavaScript just like any other HTML element.
Below is an example of changing the color of the fill when the SVG is clicked:
<!-- HTML -->
<svg width="100" height="100" id="my-svg" onclick="changeColor()">
<circle cx="50" cy="50" r="40" />
</svg>
// JavaScript
function changeColor() {
var svgElement = document.getElementById('my-svg');
var circle = svgElement.querySelector('circle');
circle.setAttribute('fill', 'blue');
}
When the SVG is clicked, the fill color of the circle will change to blue.
In this tutorial, we have learned how to use SVGs in a Bootstrap project. We have added SVGs to HTML, styled them with CSS, and manipulated them using JavaScript.
To continue your learning journey, you can:
Solutions
<svg height="100" width="100">
<rect width="100" height="100" style="fill:rgb(0,0,255);stroke-width:3;stroke:rgb(0,0,0)" />
</svg>
<svg height="100" width="100" id="mySVG">
<circle cx="50" cy="50" r="40" style="fill:yellow" />
</svg>
<script>
document.getElementById("mySVG").addEventListener("mouseover", function() {
this.querySelector('circle').style.fill = 'red';
});
</script>
<button onclick="changeColor()">Change color</button>
<svg height="100" width="100" id="mySVG">
<circle cx="50" cy="50" r="40" style="fill:yellow" />
</svg>
<script>
function changeColor() {
document.getElementById("mySVG").querySelector('circle').style.fill = 'green';
}
</script>