width style property
Specifies or returns the width of the visible area for an element.
This property has effect only on block-level elements or on elements with absolute or fixed position.
The overflowing content can be manipulated by the overflow property.
The width property contains only the width of the visible contents with the vertical scrollbar, but without the padding, border and the margin.
- Use the height property to set the height of the visible area for an element.
- You can specify a range for the size of an element with the minWidth, minHeight, maxWidth and maxHeight properties.
- Use the left, top, right and bottom properties to specify the position for an 'absolute', 'fixed', or 'relative' positioned element.
The type of the width property is string.
- You can set or retrieve the value of the width property as an integer with the pixelWidth property. It contains the value in pixels.
- If you want to get or set the width property as a floating-point number that specifies the value in the current unit type of the width property, use the posWidth property.
The properties mentioned above can be used to access style settings.
If you need the width of a rendered element, you can use the clientWidth, offsetWidth and scrollWidth properties and the getBoundingClientRect method.
Syntax:
You can find the related objects in the Supported by objects section below.
This property is read/write.
CSS page for this property: width |
Possible values:
The type of this property is string.
One of the following values:
Default. The width of the object is determined by the values of other properties. | |||||||
Takes the value of this property from the computed style of the parent element. | |||||||
The width of the element in length units. For the supported length units, see the length page. | |||||||
The width of the element is the specified percentage of the width of parent element. |
Default: auto.
Example HTML code 1:
This example illustrates the use of the width property:
|
||||
<head> <style> .example { background-color: #E9E9E9; width: 300px; height: 200px; } </style> </head> <body> <div class="example"></div> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 2:
This example illustrates the use of the width property in JavaScript:
|
||||
<head> <style> #myDiv { background-color: #E9E9E9; width: 300px; height: 200px; } </style> <script type="text/javascript"> function ChangeWidth () { var div = document.getElementById ("myDiv"); var input = document.getElementById ("myInput"); div.style.width = input.value; } </script> </head> <body> <div id="myDiv"></div> <input id="myInput" type="text" value="100" /> <button onclick="ChangeWidth ();">Change the width of the div!</button> </body> |
||||
|
||||
Did you find this example helpful?
|
Supported by objects:
CSSStyleDeclaration, htmlElement.currentStyle, htmlElement.runtimeStyle, htmlElement.style
HTML elements:
a, abbr, acronym, address, applet, b, bdo, big, blink, blockquote, body, button, caption, center, cite, code, dd, del, dfn, dir, div, dl, dt, em, embed, fieldset, font, form, h1, h2, h3, h4, h5, h6, hr, i, iframe, img, input:button, input:checkbox, input:file, input:image, input:password, input:radio, input:range, input:reset, input:search, input:submit, input:text, ins, isindex, kbd, label, legend, li, listing, marquee, menu, nobr, object, ol, optgroup, option, p, plaintext, pre, q, rt, ruby, s, samp, select, small, span, strike, strong, sub, sup, table, tbody, td, textarea, tfoot, th, thead, tt, u, ul, var, xmp
Related pages:
External links:
User Contributed Comments