CSS Property width








width defines the width of an element's content area.

Summary

ItemValue
Initial value auto
Inherited No.
Version CSS1
JavaScript syntax object.style.width="50px"
Applies to Block-level and replaced elements.

CSS Syntax

width: length  | percentage  | auto | inherit




Property Values

The property values are listed in the following table.

ValueDescription
auto Default value. The browser does the calculation.
length Set width in px, cm, etc.
% Set width in percent of the containing element
inherit Inherit the width property from the parent element

Browser compatibility

width Yes Yes Yes Yes Yes




Example

An example showing how to use width CSS property.

<!DOCTYPE HTML>
<html>
     <head>
         <style>
          p {<!--   w w w.j ava  2s. c  o  m-->
              margin: 10px;
              padding: 10px;
              color: black;
              border: thin solid;
              width: 250px;
              top: 100px;
              line-height: 3em;
              font-size: 1em;
          }
         </style>
     </head>
     <body>
        <p>
          this is a test. java2s.com 
          this is a test. this is a test. this is a test. this is a test. 
          this is a test. this is a test. this is a test. this is a test. 
        </p>
     </body>
 </html>

Click to view the demo