CSS Basic Tutorial
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.
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
.
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
).
You can restrict the size of an element by setting minimum and maximum dimensions.
Try it yourself