CSS HEIGHT AND WIDTH

CSS height and width are properties used to set the vertical and horizontal size of an HTML element, respectively.

  • height: Specifies how tall an element should be.
  • width: Specifies how wide an element should be.
You can define these using different units like pixels (px), percentages (%), viewport units (vw, vh), and more.

For example:

  • width: 200px; means the element will be 200 pixels wide.
  • height: 50%; means the element’s height will be 50% of its containing element’s height.
These properties help control the layout and design of web pages by controlling the size of elements.
If you want, I can explain more about how these work or how to apply them in different scenarios!

EXAMPLE :-

div {
  width: 300px;
  height: 200px;
}

This makes the <div> 300px wide and 200px tall.

Common values

/* Fixed size */
width: 300px;
height: 200px;

/* Percentage of parent */
width: 50%;
height: 50%;

/* Relative to viewport */
width: 100vw;
height: 100vh;

/* Let browser determine size */
width: auto;
height: auto;

Min and Max

You can also limit how large or small an element becomes:

.element {
  min-width: 200px;
  max-width: 800px;

  min-height: 100px;
  max-height: 500px;
}

A very common responsive pattern is:

img {
  width: 100%;
  height: auto;
}

This makes an image fit its container while keeping its original aspect ratio.


Comments