CSS

All Languages C CPP JAVA HTML CSS JAVASCRIPT PYTHON

CSS Height and Width

This element has a width of 100%.
<!DOCTYPE html>
<html>
<head>
<style>
div {
    width: 100%;
    border: 1px solid #4CAF50;
}
</style>
</head>


<div>This element has a width of 100%.</div>

</body>
</html>

Setting height and width

The height and width properties are used to set the height and width of an element.
The height and width can be set to auto (this is default. Means that the browser calculates the height and width), or be specified in length values, like px, cm, etc., or in percent (%) of the containing block.
This element has a height of 200 pixels and a width of 50%

Example

div {
    height: 200px;
    width: 50%;
    background-color: powderblue;
} 
This element has a height of 100 pixels and a width of 500 pixels.

Example

div {
    height: 100px;
    width: 500px;
    background-color: powderblue;
}
Note: 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

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.
The following example shows a <div> element with a height of 100 pixels and a max-width of 500 pixels:

Example

div {
    max-width: 500px;
    height: 100px;
    background-color: powderblue;
}

All CSS Dimension Properties

Property Description
height Sets the height of an element
max-height Sets the maximum height of an element
max-width Sets the maximum width of an element
min-height Sets the minimum height of an element
min-width Sets the minimum width of an element
width Sets the width of an element