width

The HTML width attribute specifies the width of elements like images, tables, and iframes, and can be defined using pixel values or percentages.

width attribute

The HTML width attribute sets the width for elements like <img>, <table>, and <iframe>. For an <img>, it adjusts the display width in pixels or percentages without modifying the source file. For a <table>, it controls the table’s width within the layout. In an <iframe>, it defines the width of the embedded content area. While the width attribute offers a simple way to make adjustments, using CSS is generally recommended for more flexible and responsive designs, as it separates styling from content and provides greater design options.

Syntax

index.html
<tagname width="pixels">

Example

Red Rose
index.html
<img src="img.jpg" alt="Red Rose" height="100" width="200">

Values

  • pixels
    • A numeric widthin pixels.

Applies To

The width attribute can be applied to the following HTML elements:

Conclusion

The width attribute in HTML specifies the width of elements such as <img>, <table>, and <iframe>, usually in pixel values. Although it provides a simple method to adjust element sizes, using CSS is generally preferred for greater flexibility and responsiveness. CSS offers more control over layout and design, making it easier to adapt to different devices.