CSS Height/Width
In CSS, the height
and width
properties are used to define the dimensions of an element. Here’s how they work:
height
property: It sets the height of an element, which determines the vertical size of the element. The value can be specified in various units such as pixels (px
), percentage (%
), viewport height (vh
), etc.Example:css
.box {
height: 200px;
}
width
property: It sets the width of an element, determining its horizontal size. Like height
, the value can be specified in different units like pixels (px
), percentage (%
), viewport width (vw
), etc.
Example:
css
.box { width: 300px; }
It’s important to note that height
and width
define the content area dimensions of an element, excluding padding, borders, and margins. If you want to include those areas in the overall size of an element, you can use the box-sizing
property with the value border-box
.
Example:
css
.box {
width: 200px;
height: 150px;
padding: 10px;
border: 1px solid black;
box-sizing: border-box;
}
In the example above, the total size of the .box
element, including padding and border, will be 200px (width) by 150px (height).
Remember that the height
and width
properties can be used with various CSS selectors like class names, IDs, or element types to target specific elements on a webpage.