Saltar al contenido

Propiedad max-width de CSS

La propiedad max-width se utiliza para establecer el ancho máximo de un elemento.

Esta propiedad evita que el valor de la propiedad width sea mayor que el valor especificado para max-width.

Por otro lado, la propiedad max-width anula la propiedad width, y la propiedad CSS min-width anula la propiedad max-width.

Valor inicialnone
Se aplica aTodos los elementos, excepto elementos en línea no reemplazados, filas de tabla y grupos de filas.
HeredableNo.
AnimableSí. El ancho es animable.
VersiónCSS2
Sintaxis DOMobject.style.maxWidth = "500px";

Sintaxis

Sintaxis de la propiedad max-width de CSS

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

Ejemplo de la propiedad max-width:

Ejemplo de la propiedad CSS max-width con valor en %

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

Resultado

Propiedad CSS max-width

Ejemplo de la propiedad max-width definida como "px" y "em":

Ejemplo de la propiedad CSS max-width con valores en px y em

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

Valores

ValorDescripciónProbarlo
autoEstablece el ancho máximo. Es el valor predeterminado de esta propiedad.Probarlo »
lengthDefine el ancho máximo en px, pt, cm, etc. El valor predeterminado es 0.Probarlo »
%Establece el ancho máximo en % del elemento contenedor.Probarlo »
initialHace que la propiedad use su valor predeterminado.Probarlo »
inheritHereda la propiedad de su elemento padre.

Práctica

¿Qué hace la propiedad CSS max-width?

¿Te resulta útil?

Vista previa dual-run — compárala con las rutas Symfony en producción.