CSS Height and Width

CSS Height and Width

CSS height and width are two of the most important properties used to define the size of elements. They are used to control the size of elements such as images, divs, tables, and other HTML elements.

CSS height and width are both set in CSS units and can be used to control the size of elements. When setting the height and width of an element, it is important to remember that the element’s overall size will be affected by padding, margins, and borders.

The height and width properties are used to set the height and width of an element.

You can set the following values for both properties:

  • auto 
  • length 
  • initial 
  • inherit
div {
  border: 1px #333 solid;
  height: 20px;
  width: 50%;
}
Width & Height

Max and Min Width and Height

Oftentimes, we want to limit the width of an element relative to its parent, and at the same time, to make it dynamic. So having a base width or height with the ability to make it extend based on the available space. Let’s say, for example, that we have a button that should have a minimum width, where it shouldn’t go below it. This is where the maximum and minimum properties become handy.

div {
    width: 100px;
    min-width: 50%;
    max-width: 100%;
}
div {
    height: 100px;
    min-height: 80%;
    max-height: 75%;
}