max-width

CSS max-width Property

The max-width property is used to set 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 Valuenone
Applies toAll elements, but non-replaced inline elements, table rows, and row groups.
InheritedNo.
AnimatableYes. Width is animatable.
VersionCSS2
DOM Syntaxobject.style.maxWidth = “500px”;

Syntax

Syntax

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

Example of the max-width property:

<!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>

Result

Example of the max-width property defined as “px” and “em”:

<!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

Values

ValueDescriptionPlay it
autoSets the maximum width. It is the default value of this property.Play it »
lengthDefines the maximum width in px, pt, cm, etc. Default value is 0.Play it »
%Sets the maximum width in % of containing element.Play it »
initialMakes the property use its default value.Play it »
inheritInherits the property from its parents element.


请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部