width Property - Javascript CSS Style Property

Javascript examples for CSS Style Property:width

Description

The width property sets or gets the width an element.

Property Values

Value Description
autoThe browser calculates the width. This is default
length Defines the width in length units
% Defines the width in percentage of the parent element
initial Sets this property to its default value.
inherit Inherits this property from its parent element.

Technical Details

Item Value
Default Value: auto
Return Value: A String, representing the width of an element
CSS VersionCSS1

Set the width of a <button> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<img id="myImg" src="http://java2s.com/resources/c.png" style="width:100px;height:132px;">
<br>

<button type="button" onclick="myFunction()">Return width of image</button>

<script>
function myFunction() {//from   www  .ja v a2 s . com
    document.getElementById("myImg").style.width = '200px';
}
</script>

</body>
</html>

Related Tutorials