canvas
The HTML element that creates the drawing surface.
Learn HTML Canvas with practical examples for rich HTML features, native browser behavior, accessibility, performance, SEO and live practice.
HTML advanced
The canvas element gives JavaScript a rectangular area for drawing pixels. It is useful for charts, games, visual tools, image processing and interactive effects.
Canvas is powerful because JavaScript controls every pixel. That is also the risk: the browser does not automatically understand what the drawing means.
If canvas communicates information, provide accessible text, labels, fallback content or a parallel data table depending on the use case.
The HTML element that creates the drawing surface.
The JavaScript API used for normal 2D drawing.
Canvas output is pixels, not semantic DOM elements.
Content inside canvas shown when unsupported and useful for alternatives.
Syntax and behavior
Set the drawing size with width and height attributes. CSS size alone can stretch the bitmap.
<figure>
<canvas id="progress-chart" width="640" height="320">
Progress chart: HTML 70%, CSS 45%, JavaScript 30%.
</canvas>
<figcaption>Learning progress by topic.</figcaption>
</figure>
<canvas id="important-chart"></canvas> <script> drawEverythingButExplainNothing(); </script>
Rules that matter
These elements can create rich interfaces, but they still need clear purpose, safe fallbacks and production discipline.
The drawing buffer should have explicit dimensions.
Charts need labels, captions or data alternatives.
SVG elements remain part of the DOM.
Games, painting tools and frequent redraws fit canvas well.
Retina displays may need drawing-buffer scaling.
Canvas can redraw often, so performance matters.
Production thinking
This matters because canvas is easy to make impressive and easy to make invisible to browsers, crawlers and assistive technology.
Canvas drawings are not automatically accessible. Add text alternatives, ARIA labels or separate structured content when the drawing communicates meaning.
Canvas-heavy tools need performance testing, resize handling and clear fallback behavior.
Text drawn inside canvas is not normal indexable HTML. Keep important text and data in the DOM too.
Live code lab
Edit the HTML or CSS, then use Run to refresh the preview. The preview is isolated, so links and forms stay inside this practice area.
Mini assignment
Practice assignment
Try it yourself
Self-check
Do not only read this page. Answer these questions out loud or write the answers in your own notes. If one answer feels vague, revisit the examples before moving on.