Graphics
Graphics in HTML
HTML provides various elements for displaying graphics, including images
, vector drawings
, and interactive visuals
. The primary elements used for graphics in HTML are <canvas>
, <svg>
, and <img>
.
The <canvas>
Element
- Used to draw graphics dynamically with JavaScript.
- Supports rendering of shapes, animations, and charts.
script
element is dangerous and is disabled by default. Consider implementing your own ProseScript
element to have control over script rendering. <canvas id="myCanvas" width="200" height="100" style="border:1px solid red;"></canvas>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.fillStyle = "blue";
ctx.fillRect(20, 20, 100, 50);
</script>
The <svg>
Element
- Represents Scalable Vector Graphics
SVG
. - Defines resolution-independent images that maintain quality at any size.
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
</svg>
SVG
follows XML standards, keep these guidelines in mind:- Every element must have a closing tag.
SVG
is case-sensitive; lowercase is recommended for consistency.- All attribute values, including numbers, should be enclosed in quotes.
Understanding SVG
Code
- The
svg
element acts as the container for the SVG graphic. - The
width
andheight
attributes define the SVG's dimensions. - Since SVG is XML-based, the
xmlns
attribute ensures proper namespace binding. - The
<circle>
element is used to create a circular shape. - The
cx
andcy
attributes determine the center of the circle. If unspecified, the default is (0,0). - The
r
attribute sets the radius of the circle. - The
stroke
andstroke-width
attributes define the outline color and thickness (e.g., green with a 4px border). - The
fill
attribute specifies the circle’s interior color (yellow in this case). - Always close the
svg
tag properly.
Additional SVG
Example
<h1>My second SVG</h1>
<svg width="150" height="100" xmlns="http://www.w3.org/2000/svg">
<rect width="100%" height="100%" fill="green" />
<circle cx="75" cy="50" r="40" fill="yellow" />
<text x="75" y="60" font-size="30" text-anchor="middle" fill="red">SVG</text>
Sorry, your browser does not support inline SVG.
</svg>
Explanation of SVG Code
- The
<svg>
element serves as the main container, specifying dimensions and namespace. - The
<rect>
element creates a rectangle that fills the entire SVG with a green background. - The
<circle>
element draws a yellow circle positioned usingcx
,cy
, andr
attributes. - The
<text>
element displaysSVG
inside the shape, positioned usingx
andy
attributes, with font size, alignment, and color settings. - The
SVG
ends with the</svg>
closing tag.
Key Points
Purpose
: Embeds vector-based graphics in web pages.Attributes
:Usage
: Supports various graphical elements like shapes, text, and images.
Comparison: <canvas>
vs. <svg>
Feature | <canvas> | <svg> |
---|---|---|
Type | Raster-based | Vector-based |
Performance | Better for animations | Best for scalable graphics |
Accessibility | Requires JavaScript | XML-based, easily accessible |
Using Images
for Graphics
The <img>
tag allows displaying static graphics on web pages.

<img src="img.jpg" alt="Sample Graphic" width="200">
Conclusion
HTML offers various elements for displaying graphics, such as <canvas>
, <svg>
, and <img>
. The <canvas>
element is used for dynamic, JavaScript-driven graphics, ideal for animations. The <svg>
tag provides scalable vector graphics, maintaining quality at any size. For static images
, the <img>
tag is the simplest option.
<track>
The HTML <track> element adds text tracks to <video> and <audio> elements, including subtitles or captions. This feature improves accessibility and user experience by providing additional information or translations.
<canvas>
The HTML <canvas> element offers a drawable area on a webpage, enabling the dynamic rendering of graphics, animations, and game visuals using JavaScript.