CSS max-width Property

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

This property prevents the width property's value from becoming larger than the value specified for max-width.

Meanwhile, the max-width property overrides width property and CSS min-width property overrides the max-width property.

Initial Value none
Applies to All elements, but non-replaced inline elements, table rows, and row groups.
Inherited No.
Animatable Yes. Width is animatable.
Version CSS2
DOM Syntax object.style.maxWidth = "500px";

Syntax

max-width: none | length | initial | inherit;

Example

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      div {
      max-width: 50%;
      background-color: #1c87c9;
      }
    </style>
  </head>
  <body>
    <h2>Max-width property example</h2>
    <div>The max-width of this text is defined as 50%.</div>
  </body>
</html>

In the following example the first element is specified by "px" and the second element by "em".

Example

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      div {
      max-width: 250px;
      background-color: #8ebf42;
      }
      p {
      max-width: 20em;
      background-color: #ccc;
      color: #fff;
      }
    </style>
  </head>
  <body>
    <h2>Max-width property example</h2>
    <div>The max-width of this div element is defined as 250px.</div>
    <p>The max-width of this paragraph is defined as 20em.</p>
  </body>
</html>

Values

Value Description
auto Sets the maximum width. It is the default value of this property.
length Defines the maximum width in px, pt, cm, etc. Default value is 0.
% Sets the maximum width in % of containing element.
initial Makes the property use its default value.
inherit Inherits the property from its parents element.




Related articles