CSS Height, Width and Overflow

CSS height and width properties are used to set the height and width of an element which are set to auto by default. The width and height values can be specified in length values like px, cm, etc, or in percent (%) of the containing block.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
div {
  height: 200px;
  width: 50%;
  background: red;
}

CSS height and width properties do not include paddings, borders and margins. They set the height and width of the HTML element inside the paddings, borders and margin of the element.

CSS Max-width

CSS max-width property is used to set the maximum width of an element. The max-width can be specified in length values like px, cm, etc, or in percent (%) of the containing block or set to none (default). The max-width value limits the maximum allowed width for an element. Any value below that width will be shown as it is while elements with higher width value than the one defined in max-width will be resized to the max-width value.

CSS max-width property is higly useful where a web page has larger elements which cannot be displayed on smaller screen without resizing.

img{
  max-width: 100%;
}

CSS Max-height

CSS max-height property is used to set the maximum height of an element. The max-height can be specified in length values like px, cm, etc, or in percent (%) of the containing block or set to none (default). The max-height value limits the maximum allowed height for an element. Any value below that height will be shown as it is while elements with higher height value than the one defined in max-height will be resized to the max-height value.

CSS max-height property is higly useful where a web page has elements with greater height which might look odd visually.

div{
  max-height: 300px;
}

CSS Overflow

The CSS overflow property controls what happens to content that is too big to fit into an area. It specifies whether to clip the content or add scrollbars when the content of an element is too big to fit in the specified area. It is generally used with the CSS height property while it can be used with CSS width property too.

The overflow property works for block elements only which have a specified width and height and can have one of the following values.

Value Description
visible Default. The content is allowed to flow outside the specified width and height.
hidden The overflow is clipped and the rest of the content will be hidden.
scroll The overflow is clipped and a scrollbar is added both horizontally and vertically which can be used to see the rest of the content
auto Similar to scroll but it adds scrollbars only when it is needed
div {
  width: 200px;
  height: 50px;
  background-color: #eee;
  overflow: scroll;
}

CSS Overflow - overflow-x and overflow-y

The overflow-x and overflow-y properties specifies whether to change the overflow of content horizontally or vertically.

CSS property overflow-x specifies adds a horizontal scrollbar while overflow-y adds a vertical scrollbar.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
div {
  width: 200px;
  height: 50px;
  background-color: #eee;
  overflow-y: scroll;
}


0 Like 0 Dislike 0 Comment Share

Leave a comment