
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
HTML
The width attribute of the <canvas> element is used to set the width of the canvas in pixels. Following is the syntax −
<canvas width="pixels_val">
Above, pixels_val is the width set in pixels. Let us now see an example to implement the width attribute of the <canvas> element −
Example
<!DOCTYPE html> <html> <body> <canvas id="newCanvas" width="400" height="200" style="border:3px dashed yellow"> HTML5 canvas tag isn't supported by your browser. </canvas> <script> var c = document.getElementById("newCanvas"); var context = c.getContext("2d"); context.fillStyle = "#FF5655"; context.fillRect(100, 50, 60, 100); </script> </body> </html>
Output
In the above example, we have created a canvas and used JavaScript −
<script> var c = document.getElementById("newCanvas"); var context = c.getContext("2d"); context.fillStyle = "#FF5655"; context.fillRect(100, 50, 60, 100); </script>
Before that we have set the canvas id with width and height −
<canvas id="newCanvas" width="400" height="200" style="border:3px dashed yellow"> HTML5 canvas tag isn't supported by your browser. </canvas>
- Related Articles
- HTML width Attribute
- Drawing lines with continuously varying line width on HTML canvas
- How to use image height and width attribute in HTML Page?
- Composition attribute in HTML5 Canvas?
- Crop Canvas / Export HTML5 Canvas with certain width and height
- HTML Canvas Basics
- HTML canvas Tag
- HTML pattern Attribute
- HTML novalidate Attribute
- HTML maxlength Attribute
- HTML wrap Attribute
- HTML disabled Attribute
- HTML for Attribute
- HTML min Attribute
- HTML disabled Attribute

Advertisements