width attribute | width property
Sets or retrieves the calculated width of the object.
Syntax
| HTML | <element width="p" ... > |
|---|---|
| JavaScript | |
Property values
Type: Integer
-
Integer that specifies the width of the object in pixels or as a percentage.
- String that specifies an integer value followed by a %. The value is a percentage of the width of the parent object.
Remarks
If you specify the width property of an img, but not the height property, the resulting height of the img is sized proportionally to the specified width property and the actual height, in pixels, of the source image file. Consider the following example:
| Dimensions of image in source file (pixels): | 100 X 50 (W X H) |
| Specified image width: | 2in |
| Specified image height: | not specified |
| Resulting image width: | 2in |
| Resulting image height: | 1in ((50/100) * 2 inches) |
If you specify the width property of an img, and the height and width of the image in the source file are identical, the height of the image matches the width.
If you specify the height property and the width property of an img, the resulting image dimensions match the height and width specified.
When scripting the height property, use either the pixelHeight or posHeight property to numerically manipulate the height value.
If dynamic changes are intended for the height and width, the original values should be set through style (for example, "style=''height:200px; width:200px'') rather than through the height and width attributes.
Although you can specify the width as a percentage, this property always retrieves the width in pixels.
This property is an integer value. Although an HTML author can specify the width as a percentage, this property always specifies the width in pixels in C++.
If you specify the width property of an img, but not the height property, the resulting height of the img is sized proportionally to the specified width property and the actual height, in pixels, of the source image file. Consider the following example:
| Dimensions of image in source file (pixels): | 100 X 50 (W X H) |
| Specified image width: | 2in |
| Specified image height: | not specified |
| Resulting image width: | 2in |
| Resulting image height: | 1in ((50/100) * 2 inches) |
If you specify the width property of an img, and the height and width of the image in the source file are identical, the height of the image matches the width.
If you specify the height property and the width property of an img, the resulting image dimensions match the height and width specified.
Examples
This example sets the width of the image to 20 pixels regardless of the original size of the image.
<img src="large.png" width="20">
See also
- img
- input type=button
- input type=checkbox
- input type=email
- input type=file
- input type=image
- input type=number
- input type=password
- input type=radio
- input type=range
- input type=reset
- input type=search
- input type=submit
- input type=tel
- input type=text
- input type=url