HTML width Attribute

HTML width Attribute: The HTML width attribute specifies the width of the element, in pixels.

HTML width Attribute

This attribute can be applied on <canvas>,<embed>,<iframe>,<img>,<input>,<object>,<video> elements.

Browser Support

This attribute is supported by the following browsers

  • Chrome
  • Firefox
  • Internet Explorer
  • Safari
  • Opera

Example: for <canvas> element

<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" width="200" height="200" style="border:1px solid">
Your browser does not support the HTML5 canvas tag.
</canvas>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.fillStyle = "#92B901";
ctx.fillRect(50, 50, 100, 100);
</script>

</body>
</html>

Output:

HTML width attribute

Example: for <embed> element

<!DOCTYPE html>
<html>
<body>
<embed src="helloworld.swf" width="200" height="200" style="border:1px solid">
</body>
</html>

Output:

HTML embedded element1

HTML embedded element

Example: for <iframe> element

<!DOCTYPE html>
<html>
<body>
<iframe src="https://tutorials.freshersnow.com/wp-content/uploads/2019/04/cropped-Tutorials-by-Freshersnow.png" width="200" height="200">
<p>Your browser does not support iframes.</p>
</iframe>
</body>
</html>

Example: for <img> element

<!DOCTYPE html>
<html>
<body>
<img src="smiley.gif" alt="Smiley face" width="42" height="42">
</body>
</html>

Output:

HTML width attribute

Example: for <input> element

<!DOCTYPE html>
<html>
<body>
<form action="/action_page.php">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
  <input type="image" src="img_submit.gif" alt="Submit" width="48" height="48">
</form>
</body>
</html>

Output:

HTML width attribute

Example: for <object> element

<!DOCTYPE html>
<html>
<body>
<object width="400" height="400" data="helloworld.swf">
</object>
</body>
</html>

Example: for <video> element

<!DOCTYPE html>
<html>
<body>
<video width="320" height="240" controls>
  <source src="movie.mp4" type="video/mp4">
  <source src="movie.ogg" type="video/ogg">
  Your browser does not support the video tag.
</video>
</body>
</html>