Curriculum
Course: CSS
Login

Curriculum

CSS

CSS INTRODUCTION

0/1

CSS Selectors

0/1

CSS Comments

0/1

CSS Padding

0/1

CSS Box Model

0/1

CSS Combinators

0/1

CSS Pseudo-classes

0/1

CSS Pseudo-elements

0/1

CSS Dropdowns

0/1

CSS Image Gallery

0/1

CSS Image Sprites

0/1

CSS Counters

0/1

CSS Website Layout

0/1

CSS Specificity

0/1

CSS Math Functions

0/1
Text lesson

CSS Height, Width and Max-width

The CSS max-width property defines the maximum width that an element can have.

The element possesses a height of 50 pixels and spans the entire width of its container.

This element has a height of 50 pixels and a width of 100%.

CSS Setting height and width

The height and width properties are utilized to establish the height and width of an element.

These properties exclusively affect the dimensions of the content area within an element, excluding padding, borders, or margins. They specify the height/width of the area encompassed by the padding, border, and margin of the element.

CSS height and width Values

The height and width properties can be assigned the following values:

  • auto: Default, where the browser computes the height and width.
  • length: Specifies the height/width in pixels, centimeters, etc.
  • %: Indicates the height/width as a percentage of the containing block.
  • initial: Sets the height/width to its default value.
  • inherit: The height/width inherits its value from its parent.

CSS height and width Examples

The height of this element is 200 pixels, while its width occupies 50% of the available space.

 

 

Example

Define the height and width for a <div> element.

div {
  height: 200px;
  width: 50%;
  background-color: powderblue;
}

The height of this element is set to 100 pixels, while its width is defined as 500 pixels.

 

Example

Specify the height and width for another <div> element.

div {
  height: 100px;
  width: 500px;
  background-color: powderblue;
}
Note: Keep in mind that the height and width properties exclusively define the dimensions of the content area within an element, excluding any padding, borders, or margins. These properties specify the height/width of the area confined by the padding, border, and margin of the element.

Setting max-width

The max-width property establishes the maximum width of an element.

You can specify the max-width using length values such as pixels (px), centimeters (cm), etc., or as a percentage (%) of the containing block. Alternatively, it can be set to none, which is the default and indicates that there is no maximum width limit.

When the browser window becomes smaller than the width of the element (500px), a horizontal scrollbar is added to the page, causing issues with the <div> mentioned above.

Utilizing max-width in such scenarios enhances the browser’s handling of smaller windows.

Hint: Resize the browser window to a width smaller than 500px to observe the contrast between the two divs!

The height of this element is 100 pixels, and its maximum width is capped at 500 pixels.

Note: When both the width property and the max-width property are applied to the same element, if the value of the width property exceeds that of the max-width property, the max-width property takes precedence, and the width property is disregarded.

Example

The height of this <div> element is set to 100 pixels, with a maximum width limited to 500 pixels.

div {
  max-width: 500px;
  height: 100px;
  background-color: powderblue;
}

All CSS Dimension Properties

Property

Description

height

Specifies the height of an element.

max-height

Specifies the maximum height of an element.

max-width

Specifies the maximum width of an element

min-height

Specifies the minimum height of an element

min-width

Specifies the minimum width of an element

width

Specifies the width of an element