CSS Height and Width
The CSS properties for height
and width
help you adjust how tall and wide something on a webpage should be.
The CSS max-width
property enables you to specify the maximum size that an element can attain.
CSS Setting height and width
You can use the height
and width
properties to change how tall and wide an element is on a webpage.
The height and width properties tell us how big something is, but they don't count the extra space around it like padding, borders, or margins. Instead, they only measure the space inside the padding, border, and margin of the element.
CSS height and width Values
The height
and width
of something can have following values.
auto
- This is the standard setting. The web browser figures out the size using the height and width values provided.length
-Specifies the size in pixels, centimeters, or other units.%
- Describes how much something stretches in relation to the block it's in, using percentages for both height and width.initial
- Resets the height and width to their original values.inherit
-The height and width of something will be the same as its parent's size.
CSS height and width Examples
Note: Keep in mind that when you use theheight
and width
properties, they don't count the padding, borders, or margins. Instead, they determine the size of the space inside those elements.
Setting max-width
The max-width
property helps limit how maximum an element can become.
You can set the max-width
in different ways: using numbers with units like pixels (px) or centimeters (cm), as a percentage (%) of the containing block, or leaving it unset (which means there's no maximum width).
The issue with the <div>
mentioned happens when the browser window is not wide enough for the element (which is 500px wide). In this situation, the browser adds a horizontal scrollbar to the page.
In this case, using max-width
instead will make sure that the web browser works better when the window is small.
Tip: Make your internet browser window narrower than 500 pixels. You'll notice a change in the appearance of two sections on the screen.