HTML provides several ways to add graphics and drawings to your web pages. You can use images, vector graphics, or dynamic drawings with the <canvas> element.
Images are the most common graphics element in HTML.
<img src="image.jpg" alt="Description" width="300" height="200">
| Attribute | Description |
|---|---|
src |
File path or URL of the image |
alt |
Text shown if the image fails to load |
width / height |
Resize the image |
loading="lazy" |
Loads image only when needed |
The <canvas> element allows you to draw graphics dynamically using JavaScript.
<canvas id="myCanvas" width="400" height="200" style="border:1px solid #000000;">
Your browser does not support the canvas element.
</canvas>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.fillStyle = "blue";
ctx.fillRect(50, 50, 150, 100);
</script>
SVG is an XML-based format to create vector graphics that scale without losing quality.
| Tag | Purpose |
|---|---|
<svg> |
Vector graphics |
<canvas> |
Dynamic drawing using JavaScript |
<img> |
Static images |
<figure> |
Group media elements |
<figcaption> |
Caption for images or graphics |
<figure>
<img src="flower.jpg" alt="Flower" width="300">
<figcaption>Beautiful flower in bloom</figcaption>
</figure>