HTML width Attribute

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 3 Examples
Sizing & Layout

Introduction

The width attribute sets the horizontal size of certain HTML elements such as images, tables, and table cells. It gives you direct control over how wide an element appears in the layout.

On elements like <img>, the value is a pixel number (for example width="300"). Tables and cells can also use percentages. For full responsive control, CSS width is often used alongside or instead of the HTML attribute.

What You’ll Learn

01

Pixels

width="300"

02

Percent

On tables.

03

img sizing

With height.

04

vs CSS

Different rules.

05

Responsive

max-width.

06

JavaScript

style.width.

Purpose of width

The primary purpose of the width attribute is to define how wide an element should be rendered. On images it reserves space and scales the display width; on tables it sets overall or column layout width.

Using width and height together on images helps browsers calculate layout before the image loads, reducing content jump (CLS).

💡
HTML attribute ≠ CSS property

HTML width="300" is a number (pixels). CSS width: 300px or width: 50% or width: 50vw belongs in stylesheets. Units like vw are not valid HTML attribute values.

📝 Syntax

Set width with a numeric pixel value or, on some elements, a percentage:

width.html
<img src="photo.jpg" alt="Photo" width="300" height="200">
<table width="80%">...</table>

Syntax Rules

  • Pixel values are plain numbers: width="300", not width="300px".
  • Percentages work on table, td, th, col, and related table elements.
  • Pair width with height on images when possible.
  • For responsive layouts, add CSS such as max-width: 100%; height: auto;.
  • JavaScript: element.style.width = "200px" (CSS property).
  • IDL property on img: HTMLImageElement.width (pixels as number).

💎 Values

The HTML width attribute accepts values depending on the element:

  • Pixel number — Most common. width="400" means 400 pixels on img, canvas, iframe, etc.
  • Percentage — On tables and cells: width="50%" is half the parent table or cell width.
  • Not valid in HTML attr — Units like px, rem, vw, and em belong in CSS, not the HTML width attribute.
width-values.html
<!-- Image: pixels only -->
<img src="logo.png" alt="Logo" width="120">

<!-- Table: pixels or percent -->
<table width="100%">...</table>
<td width="200">Cell</td>

<!-- CSS for vw, rem, etc. -->
<div style="width: 50vw;">Viewport-based</div>

⚡ Quick Reference

ElementHTML width valuesExample
<img>Pixels (number)width="300"
<table>, <td>Pixels or %width="80%"
<canvas>, <video>Pixelswidth="640"
<iframe>Pixelswidth="560"
<div> (CSS)px, %, vw, rem, etc.style="width:50%"
Responsive tipCSS max-widthmax-width: 100%

Applicable Elements

ElementSupported?Typical use
<img>YesDisplay width in pixels; pair with height
<table>, <td>, <th>YesTable and column sizing (px or %)
<canvas>, <video>YesIntrinsic dimensions in pixels
<iframe>, <embed>, <object>YesEmbed frame width
<col>, <colgroup>, <hr>YesColumn or rule width
<div>, <p>No (use CSS)style or stylesheet

HTML width vs CSS width vs height

FeatureHTML attributeCSS property
Syntaxwidth="300"width: 300px;
Units allowedNumber (px) or % on tablespx, %, rem, vw, em, etc.
ElementsSpecific (img, table, …)Any element
ResponsiveLimitedFull control + media queries
Pair withheight attributemax-width, aspect-ratio

Examples Gallery

Size an image in pixels, set table column widths with percentages, and change width dynamically with JavaScript.

👀 Live Preview

Image rendered at 120px width via the HTML attribute:

Demo thumbnail

Example

Applying width to an image element:

width.html
<img src="example.jpg" alt="Example Image" width="300">

How It Works

width="300" tells the browser to display the image 300 CSS pixels wide. Height may scale proportionally if only width is set, but specifying both width and height is best practice.

Example — table and column width

Tables accept pixel numbers or percentages:

table-width.html
<table width="80%">
  <tr>
    <th width="30%">Item</th>
    <th width="70%">Details</th>
  </tr>
</table>

How It Works

The table spans 80% of its container; column headers split that space 30/70. For modern layouts, CSS on <table> or flex/grid is often preferred, but the HTML attribute remains valid.

Dynamic Values with JavaScript

Change width at runtime using the CSS property:

dynamic-width.html
<div id="dynamicElement">Resize me</div>

<script>
  document.getElementById("dynamicElement").style.width = "200px";
</script>

How It Works

JavaScript sets the CSS width property, which accepts units like px, %, and vw. For <img>, you can also set imgElement.width = 200 (pixel number property).

♿ Accessibility

  • Do not rely on width alone — Small text in a wide box or oversized images can hurt readability; test zoom levels.
  • Include meaningful alt on images regardless of display size.
  • Allow user scaling — Avoid fixed tiny widths that break when users enlarge text.
  • Responsive images — Use srcset and sizes with CSS so content adapts on small screens.
  • Tables — Prefer semantic headers; width does not replace accessible table structure.

🧠 How width Works

1

Author sets width

Pixels or %.

HTML
2

Browser calculates layout

Box dimensions.

Render
3

CSS may override

Stylesheets win.

Cascade
=

Sized element

Predictable layout.

Browser Support

The width attribute is universally supported on elements that define it. CSS width provides additional flexibility for responsive design across all modern browsers.

Universal · Fully supported

Reliable on img, table, and embeds

Combine HTML width on images with CSS max-width for responsive behavior.

100% All browsers
Google Chrome Supported
Supported
Mozilla Firefox Supported
Supported
Apple Safari Supported
Supported
Microsoft Edge Supported
Supported
width attribute Excellent

Bottom line: Safe to use on supported elements; add CSS for responsive layouts.

💡 Best Practices

✅ Do

  • Use plain numbers: width="300"
  • Set both width and height on images when known
  • Add max-width: 100% in CSS for responsiveness
  • Use srcset for multiple image resolutions
  • Prefer CSS width for layout containers

❌ Don’t

  • Write width="300px" in HTML attributes
  • Put vw or rem in HTML width attribute
  • Stretch small images to huge widths (blur)
  • Forget alt text on sized images
  • Rely on width alone for complex responsive layouts
  • Use width judiciously based on design and responsiveness goals.
  • For responsive layouts, use CSS percentages, max-width, or viewport units in stylesheets.
  • Test on various devices and browsers to verify sizing behavior.

🎉 Conclusion

The width attribute is a practical tool for setting horizontal size on images, tables, and other supported elements. Used correctly (plain pixel numbers or table percentages), it helps create predictable layouts.

Combine it with CSS for modern responsive design, and always distinguish between the HTML attribute syntax and the richer CSS width property.

Key Takeaways

Knowledge Unlocked

4 truths every developer should know about width

Bookmark these before your next width implementation.

4
Core concepts
📝 02

Tables and cells also

Tables and cells also accept percentage values.

Syntax
⚙️ 03

CSS width supports more

CSS width supports more units (vw, rem, etc.) on any element.

Usage
🔒 04

Pair img width with

Pair img width with height to reduce layout shift.

Dynamic

❓ Frequently Asked Questions

The HTML attribute expects a number without units. Use width="300". The px suffix is for CSS only.
Not as an HTML attribute. Use CSS: style="width: 400px" or a class in your stylesheet.
CSS rules in stylesheets typically override presentational HTML attributes when they conflict, depending on specificity.
Yes, when you know the display dimensions. It helps the browser reserve space and improves Core Web Vitals.
Set HTML width/height for aspect ratio hints, then add CSS max-width: 100%; height: auto; and use srcset for different file sizes.

Practice element width

Try sizing an image and a table with the width attribute.

Try the image example →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful