- Home
- /
- HTML Canvas
HTML Tutorial
INTRODUCTION
HTML BASIC TAGS
INLINE & BLOCK ELEMENTS
HTML LISTS
HTML TABLES
HTML FORMS
HEAD ELEMENTS
HTML MEDIA
MISCELLANEOUS TAGS
- Home
- /
- HTML Canvas
HTML Canvas
The <canvas>
element provides a space in your HTML where graphics can be drawn, such as shapes, charts, animations, or games. While it’s often used with JavaScript, understanding its structure in HTML is essential.
What is Canvas?
The <canvas>
tag defines a drawable region on a web page. It acts as a graphics container where you can use JavaScript to draw anything from simple lines to complex animations.
Why use Canvas?
- Graphics: Build charts, diagrams, game element, and custom shapes.
- Dynamics Visuals: useful for creating real-time animations or data visualizations.
- Interactive content: Enables clickable maps, drawing apps, and more (when JavaScript is used).
Basic Syntax
Key Attributes
Attribute | Purpose |
---|---|
id | Identifies the canvas for JavaScript access |
width | Sets the canvas width in pixels |
height | Sets the canvas height in pixels |
Default canvas size is 300×150 pixels if no width
or height
is set.
CSS Styling Example
canvas {
border: 1px solid black;
}
Use CSS to visually style the canvas just like any other HTML element.
Simple Canvas Setup
Basic Canvas Box
Conclusion
The <canvas>
tag is the foundation for drawing in HTML. While it’s often paired with JavaScript for interactivity and rendering, it’s valuable to know its basic usage, structure, and styling possibilities in pure HTML.