👀 Live Preview
Image rendered at 120px width via the HTML attribute:

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.
width="300"
On tables.
With height.
Different rules.
max-width.
style.width.
widthThe 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 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.
Set width with a numeric pixel value or, on some elements, a percentage:
<img src="photo.jpg" alt="Photo" width="300" height="200">
<table width="80%">...</table>width="300", not width="300px".table, td, th, col, and related table elements.width with height on images when possible.max-width: 100%; height: auto;.element.style.width = "200px" (CSS property).HTMLImageElement.width (pixels as number).The HTML width attribute accepts values depending on the element:
width="400" means 400 pixels on img, canvas, iframe, etc.width="50%" is half the parent table or cell width.px, rem, vw, and em belong in CSS, not the HTML width attribute.<!-- 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>| Element | HTML width values | Example |
|---|---|---|
<img> | Pixels (number) | width="300" |
<table>, <td> | Pixels or % | width="80%" |
<canvas>, <video> | Pixels | width="640" |
<iframe> | Pixels | width="560" |
<div> (CSS) | px, %, vw, rem, etc. | style="width:50%" |
| Responsive tip | CSS max-width | max-width: 100% |
| Element | Supported? | Typical use |
|---|---|---|
<img> | Yes | Display width in pixels; pair with height |
<table>, <td>, <th> | Yes | Table and column sizing (px or %) |
<canvas>, <video> | Yes | Intrinsic dimensions in pixels |
<iframe>, <embed>, <object> | Yes | Embed frame width |
<col>, <colgroup>, <hr> | Yes | Column or rule width |
<div>, <p> | No (use CSS) | style or stylesheet |
width vs CSS width vs height| Feature | HTML attribute | CSS property |
|---|---|---|
| Syntax | width="300" | width: 300px; |
| Units allowed | Number (px) or % on tables | px, %, rem, vw, em, etc. |
| Elements | Specific (img, table, …) | Any element |
| Responsive | Limited | Full control + media queries |
| Pair with | height attribute | max-width, aspect-ratio |
Size an image in pixels, set table column widths with percentages, and change width dynamically with JavaScript.
Image rendered at 120px width via the HTML attribute:
Applying width to an image element:
<img src="example.jpg" alt="Example Image" width="300">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.
Tables accept pixel numbers or percentages:
<table width="80%">
<tr>
<th width="30%">Item</th>
<th width="70%">Details</th>
</tr>
</table>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.
Change width at runtime using the CSS property:
<div id="dynamicElement">Resize me</div>
<script>
document.getElementById("dynamicElement").style.width = "200px";
</script>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).
alt on images regardless of display size.srcset and sizes with CSS so content adapts on small screens.Pixels or %.
Box dimensions.
Stylesheets win.
Predictable layout.
The width attribute is universally supported on elements that define it. CSS width provides additional flexibility for responsive design across all modern browsers.
Combine HTML width on images with CSS max-width for responsive behavior.
Bottom line: Safe to use on supported elements; add CSS for responsive layouts.
width="300"max-width: 100% in CSS for responsivenesssrcset for multiple image resolutionswidth="300px" in HTML attributesvw or rem in HTML width attributewidth judiciously based on design and responsiveness goals.max-width, or viewport units in stylesheets.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.
widthBookmark these before your next width implementation.
HTML width uses numbers for pixels: width="300", not 300px.
Tables and cells also accept percentage values.
SyntaxCSS width supports more units (vw, rem, etc.) on any element.
Pair img width with height to reduce layout shift.
Dynamicwidth="300". The px suffix is for CSS only.style="width: 400px" or a class in your stylesheet.max-width: 100%; height: auto; and use srcset for different file sizes.Try sizing an image and a table with the width attribute.
5 people found this page helpful