The CSS reference

"width" - css property (v. css 1)

  • This property specifies the content width of boxes generated by block-level and replaced elements.
  • Replaced element is content which is outside the scope of the CSS formatting model, such as an image, embedded document, or applet.
  • This property does not apply to non-replaced inline-level elements.
  • The width of a replaced element's box is intrinsic and may be scaled by the user agent (UA) if the value of this property is different than 'auto'.
  • Initial value: auto
  • The property is not inherited
  • HTML usage: All elements but non-replaced inline elements, table rows, and row groups
  • Browsers that support the width - property :
    Internet Explorer Mozilla Firefox Opera Google Chrome Apple Safari

How width, height, margin, border and padding properties relates to each other:


Possible width values:

width value Comments CSS
length With this you specify a positive value followed with a unit identifier (it cannot be negative). 1
percentage With this you specify a value with a percentage. It refer to width of containing block. If the height of the containing block is not specified explicitly, the value is interpreted like 'auto'. 1
auto The outcome of this property value is much dependant on the related containing block type, replaced or none-replaced elements and the browser. For details, please study computing widths and margins for behavior. 1
inherit Will inherit the value from parent specification. This property value is new with CSS 2. 2

Possible unit identifier:

unit identifier Comments CSS
em The relative unit 'em' is relative to the font size of the element itself. 1
ex The relative unit 'ex' is relative to the font size of the element itself. 1
px Pixels, relative to the viewing device 1
in inches -> 1 inch is equal to 2.54 centimeters. 1
cm centimeters. 1
mm millimeters. 1
pt points -> the points used by CSS2 are equal to 1/72th of an inch. 1
pc picas -> 1 pica is equal to 12 points. 1

Property "width" example:

<!DOCTYPE HTML >
<html  >
  <head>
    <title>Test</title>
    <style type="text/css">
      .presentation {
        border-color: #aad5ff;
        border-style: solid;    }
      table.presentation {
        border-width: 0 0 1px 1px;
        width: 500px;   }
      th.presentation, td.presentation {
        border-width: 1px 1px 0 0;
        padding: 4px 4px 4px 4px;      }
      th.presentation { background-color: #D4FFAA;}
    </style>
  </head>
  <body>
    <table class='presentation'  border='0'
           cellpadding='0' cellspacing='0' >
      <tr>
        <th class='presentation' style="width: 22%">Values</th>
        <th class='presentation'>Comments</th>
        <th class='presentation'>CSS</th>
      </tr>
      <tr>
        <td class='presentation'>thin, medium or thick</td>
        <td class='presentation'>The width of the keyword
          values are UA dependent,
          but the following holds: <b>'thin' <= 'medium'
            <= 'thick'</b>. </td>
        <td class='presentation'>1</td>
      </tr>
      <tr>
        <td class='presentation'>length</td>
        <td class='presentation'>With this you specify a
          value with a <b>unit identifier</b>. </td>
        <td class='presentation'>1</td>
      </tr>
      <tr>
        <td class='presentation'>inherit</td>
        <td class='presentation'>Will inherit the value from
          parent specification. This property value is new
          with CSS 2.</td>
        <td class='presentation'>2</td>
      </tr>
    </table>
  </body>
</html>

Javascript access:

// To SET values ([o] is the target object)
[o].style.width="width value"
// To GET values you must first get the computed style object
// To get that object in IE or Opera:
var cStyle=[o].currentStyle;
// To get that object in Firefox, Chrome or Safari (w3c-type):
var cStyle=window.getComputedStyle([o],null)
// To GET the property value:
var value=cStyle.width;
// IE and Opera will return a value in units as used with a Set.
// Firefox will return a calculated float value in pixel (px).
// Chrome or Safari will return a calculated int value in pixel (px).
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.