EN
Este conteúdo não está disponível em seu idioma, mas aqui está a versão em inglês.

width property

Specifies the width of the object.

Syntax

width: auto | width | percentage

Property values

auto

Default. Default width of the object.

width

Floating-point number, followed by an absolute units designator (cm, mm, in, pt, or pc) or a relative units designator (em, ex, or px). For more information about the supported length units, see CSS Values and Units Reference.

percentage

Integer, followed by a %. The value is a percentage of the width of the parent object, whether or not it is specified explicitly. Negative values are not allowed.

CSS information

Applies ToAll elements
Mediavisual
Inheritedno
Initial Valueauto

Remarks

As of Microsoft Internet Explorer 6, when you use the !DOCTYPE declaration to specify standards-compliant mode, this property specifies the distance between the left and right edges of the content box—that is, within the padding.

When the !DOCTYPE declaration does not specify standards-compliant mode, and with earlier versions of Windows Internet Explorer, this property includes the object's content box, plus the values of the following properties: border-left, border-right, padding-left, and padding-right. Subtracting the sum of the values of these properties from the value of the width property equals the width of the parent object's content box.

To perform operations on the numeric value of this property, use pixelWidth or posWidth.

For more information about how to access the dimension and location of elements on the page through the Dynamic HTML (DHTML) Document Object Model (DOM), see Measuring Element Dimension and Location with CSSOM in Internet Explorer 9.

Examples

In the HTML and CSS examples below, the width of the three div elements are set to different values using the width property. The width property is set to auto, the default value, for the first div, 350px for the second div, and 20% for the third div, or 20% of the width of the parent object.


<div id="one">
   Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt  
</div>
<div id="two">
   Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt  
</div>
<div id="three">
   Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt  
</div>



#one {
   width: auto;
   color: black;
}
#two { 
   width: 350px;
   color: navy;
}
#three {
   width: 20%;
   color: red;
}


You can see the result below.

Three lines of text with different values specified for the width property

This example uses inline scripting to set the width of an image when an onclick event occurs.

Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/width_s.htm


<img src="sphere.jpg" onclick="this.style.width='1cm'" ondblclick="this.style.width=''">

See also

CSSStyleDeclaration
currentStyle
runtimeStyle
style
Conceptual
Measuring Element Dimension and Location with CSSOM in Internet Explorer 9
Other Resources
CSS Enhancements in Internet Explorer 6

 

 

Mostrar:
© 2014 Microsoft