CSS TutorialLesson #32

Min / Max

The `min-width` and `max-width` properties are used to set the minimum and maximum width of an element.

CSS Min/Max Height and Width

The min-width and max-width properties are used to set the minimum and maximum width of an element.

The min-height and max-height properties are used to set the minimum and maximum height of an element.


CSS Using max-width

The max-width property sets the maximum allowed width of an element. This prevents the width of an element to be larger than the max-width property value.

The max-width property can have the following values:

  • length - Defines the maximum width in px, cm, etc.
  • % - Defines the maximum width in percent of the containing block
  • none - This is default. Means that there is no maximum width

One problem with the width property can occur when the browser window is smaller than the width of the element. The browser then adds a horizontal scrollbar to the page. So, using max-width will improve the browser's handling on small windows.


CSS max-width Examples

Drag the browser window to smaller than 600px wide, to see the difference between the two divs below!

Using width:

This element has a width of 600 pixels.

Using max-width:

This element has a max-width of 600 pixels.

Example

Example Code

css
Try it Yourself
.div1 {
  max-width: 500px;
  background-color: powderblue;

  }

  .div2 {
  width: 500px;
  background-color: powderblue;
}

Note: If you use both the width property and the max-width property on the same element, and the value of the width property is larger than the max-width property; the max-width property value will be used!

Example

Example Code

css
Try it Yourself
.div1 {
  width: 100%;
  max-width: 900px;

  background-color: powderblue;
}

Try it Yourself - Examples

This example demonstrates how to set a minimum width and a maximum width of an element using a pixel value.

This example demonstrates how to set a minimum height and a maximum height of an element using a pixel value.


All CSS Dimension Properties

PropertyDescription
heightSets the height of an element
max-heightSets the maximum height of an element
max-widthSets the maximum width of an element
min-heightSets the minimum height of an element
min-widthSets the minimum width of an element
widthSets the width of an element

Interactive Code Examples

CSS Example 1

css
Try it Yourself
Interactive runnable CSS example from EA Academy.
Live Output Preview:

CSS Example 2

css
Try it Yourself
Interactive runnable CSS example from EA Academy.
Live Output Preview: