Menu

CSS Dimensions Explained

CSS Dimensions

The CSS dimensions are used to set the height and width of the HTML elements. We can set the height and width of the HTML element with the help of the CSS height and width property.

The CSS offers several properties for setting the dimension of the elements, which are given below:

PropertiesDescription
heightThe height property sets the height of the HTML element.
widthThe width property sets the width of the HTML element.
line-heightThe line-height property sets the height of a line of a text.
max-heightThe max-height property sets the maximum height that an element can be.
min-heightThe min-height property sets the minimum height that an element can be.
max-widthThe max-width property sets the maximum width of an element.
min-widthThe min-width property sets the minimum width of an element.

CSS Height and Width Values

The CSS height and width property consist of the following values:

ValueDescription
autoThis is the default value of both properties.
lengthThe user can set the height and width of the box in px, cm, etc.
%The user can set the value of the height and width in % according to their containing block
initialThis value sets the value of the height and width to its default value.
inheritThis value inherits the value of height and width property from its parent element.

Example: Specifying height and width of the HTML element in %.

<!DOCTYPE html>
<html>
<head>
<style>
div {
  height: 200px;
  width: 100%;
  background-color:#89eff5;
  text-align: center;
  font-size: 30px;
}
</style>
</head>
<body>
<div>Hello!</div>
</body>
</html>

Max-Width of a box in CSS

The max-width property is used to set the max-width of any element. The values of this property can be specified in length, cm, px, %, etc. This property is used to overcome the problem that occurs when the user resizes the screen which is smaller than the element's height and width then the browser adds the side scroll bar to the web page. So, this property improves the handling of the elements when the user switches to the smaller window.

Live Example: Specifying max-width of an HTML element

In this example, we have set the max-width of the element 500px. When we resize the screen to the smaller one, no side scroll bar will be displayed.

Conclusion

In this lesson we have learned about the various values of the CSS height and width property. Also we have learned how to set max-height and max-width of an HTML elements.