totn CSS

CSS: max-width property

This CSS tutorial explains how to use the CSS property called max-width with syntax and examples.

Description

The CSS max-width property defines the maximum width of the content area of an element.

Syntax

The syntax for the max-width CSS property is:

max-width: value;

Parameters or Arguments

value

The max-width of the content area of an element. It can be one of the following:

Value Description
fixed Fixed value expressed in pixels, em's, etc.
div { max-width: 150px; }
div { max-width: 50em; }
percentage Percentage value
div { max-width: 90%; }
none No maximum width is applied to the element
div { max-width: none; }
inherit Indicates that the element will inherit the max-width from its parent element
div { max-width: inherit; }

Note

  • The content area of an element is inside the padding, border, and margin of the element.
  • The CSS max-width property applies to block level and replaced elements.
  • The CSS max-width property is used to prevent the CSS width from becoming larger than the value specified in the CSS max-width property.
  • When the value is provided as a percentage, it is relative to the width of the containing block.
  • See also height, width, max-height, min-height, min-width.

Browser Compatibility

The CSS max-width property has basic support with the following browsers:

  • Chrome
  • Android
  • Firefox (Gecko)
  • Firefox Mobile (Gecko)
  • Internet Explorer (IE)
  • IE Phone
  • Opera
  • Opera Mobile
  • Safari (WebKit)
  • Safari Mobile

Example

We will discuss the max-width property below, exploring examples of how to use this property in CSS.

Using Fixed Value

Let's look at a CSS max-width example where we have provided the max-width as a fixed value expressed in pixels.

div { max-width: 200px; }

In this CSS max-width example, the width of the <div> tag can not exceed 200px.

We can also express the max-width as a fixed value in em's.

For example:

div { max-width: 175em; }

In this CSS max-width example, the width of the <div> tag can not exceed 175em.

Using Percentage

Let's look at a CSS max-width example where we have provided the max-width as a percentage.

div { max-width: 90% }

In this CSS max-width example, the width of the <div> can not exceed 90%.

Using none

Let's look at a CSS max-width example where we have used none as the max-width value.

div { max-width: none; }

In this CSS max-width example, no maximum width is applied to the <div> element. This would be useful if you wanted to remove a max-width from a tag.