CSS Height and Width

In CSS, the height and width properties are used to define the size of an element. You can control how tall or wide an element is, either by setting specific values or using keywords like auto or inherit. These properties play a critical role in layout design.


Syntax:

width: <value>; 
height: <value>;


Value: Can be specified in various units such as pixels (px), percentages (%), ems (em), rems (rem), viewport units (vw, vh), or keywords like auto and inherit.

Example
div {
width: 50%;
height: 200px;
}

Try it yourself

Units for Height and Width:

Pixels (px): Fixed size, not responsive to screen size changes.

Percentages (%): Relative to the size of the containing element.

Viewports (vw, vh): Based on the size of the viewport (the visible part of the web page). 1vw equals 1% of the viewport width, and 1vh equals 1% of the viewport height.

Ems (em) and Rems (rem): Relative to the font size of the element or root element (for rem).

Min-Width, Min-Height, Max-Width, Max-Height:

You can restrict the size of an element by setting minimum and maximum dimensions.

  • min-width / min-height: Ensures the element doesn't shrink smaller than the defined size.
  • max-width / max-height: Ensures the element doesn't grow larger than the defined size.
Example
div {
width: 50%;
min-width: 200px; /* Won't go below 200px */
max-width: 600px; /* Won't exceed 600px */
height: auto; /* Height adjusts automatically */
}

Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.