The height
and width
properties are used to set the height and width of an element.
auto
- This is default. The browser calculates the height and widthlength
- Defines the height/width in px, cm, etc.%
- Defines the height/width in percent of the containing blockinitial
- Sets the height/width to its default valueinherit
- The height/width will be inherited from its parent valueSet the height and width of a <Div> element:
Set the height and width of another <Div> element:
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.
In this situation, using max-width
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!
Note: If you for some reason use both the width
property and the max-width
property on the same element, and the value of the width
property is larger than the max-width
property; the max-width
property will be used (and the width
property will be ignored).
This <Div> element has a height of 100 pixels and a max-width of 500 pixels: