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

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

CSS max-width Property

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>

Leave a Reply

Your email address will not be published. Required fields are marked *