The height and width properties are used to set the height and width of an element.
The height and width properties do not include padding, borders, or margins. It sets the height/width of the area inside the padding, border, and margin of the element.
CSS height and width Values
The height and width properties may have the following values:
CSS height and width Examples
This element has a height of 200 pixels and a width of 50%
Example
Set the height and width of a <div> element:
This element has a height of 100 pixels and a width of 500 pixels.
Example
Set the height and width of another <div> element:
Note: Remember that the height and width properties do not include padding, borders, or margins! They set the height/width of the area inside the padding, border, and margin of the element!
Setting max-width
The max-width property is used to set the maximum width of an element.
The max-width can be specified in length values, like px, cm, etc., or in percent (%) of the containing block, or set to none (this is default. Means that there is no maximum width).
The problem with the <div> above occurs when the browser window is smaller than the width of the element (500px). The browser then adds a horizontal scrollbar to the page.
Using max-width instead, in this situation, will improve the browser's handling of small windows.
Tip: Drag the browser window to smaller than 500px wide, to see the difference between the two divs!
This element has a height of 100 pixels and a max-width of 500 pixels.
Note: The value of the max-width property overrides width.
Example
This <div> element has a height of 100 pixels and a max-width of 500 pixels:
Required fields are marked *
Get all latest content delivered to your email free.