max-width
        
        
          
                Baseline
                
                  Widely available
                
                 *
              
        
        
        
          
                
              
                
              
                
              
        
        
      
      This feature is well established and works across many devices and browser versions. It’s been available across browsers since julho de 2015.
* Some parts of this feature may have varying levels of support.
A propriedade max-width do CSS estabelece a largura máxima de um elemento. Ele evita que o valor usado da propriedade width se torne maior que o valor especificado por max-width.
Experimente
max-width: 150px;
max-width: 20em;
max-width: 75%;
max-width: 20ch;
<section class="default-example" id="default-example">
  <div class="transition-all" id="example-element">
    Change the maximum width.
  </div>
</section>
#example-element {
  display: flex;
  flex-direction: column;
  background-color: #5b6dcd;
  height: 80%;
  justify-content: center;
  color: #ffffff;
}
max-width substitui width, mas min-width substitui max-width.
Sintaxe
/* <length> valor */
max-width: 3.5em;
/* <porcentagem> valor */
max-width: 75%;
/* Valores de keyword */
max-width: none;
max-width: max-content;
max-width: min-content;
max-width: fit-content(20em);
/* Valores Globais */
max-width: inherit;
max-width: initial;
max-width: revert;
max-width: revert-layer;
max-width: unset;
Valores
- <length>
- 
Define a max-widthcomo um valor absoluto.
- <percentage>
- 
Define a max-widthcomo uma porcentagem da largura do bloco que o contém.
- none
- 
A largura não possui valor máximo. (padrão) 
- max-contentExperimental
- 
A max-widthintrínseca preferida.
- min-contentExperimental
- 
O mínimo max-widthintrínseco.
- fit-content(- <length-percentage>)
- 
Use a fórmula fit-contentcom o espaço disponível substituído pelo argumento especificado, i.e.min(max-content, max(min-content, argumento)).
Preocupações de Acessibilidade
Certifique-se que os elementos definidos com max-width não sejam truncados e/ou não obscureçam outros conteúdos quando a página for ampliada para aumentar o tamanho do texto.
Definição formal
| Initial value | none | 
|---|---|
| Aplica-se a | all elements but non-replaced inline elements, table rows, and row groups | 
| Inherited | não | 
| Percentages | refer to the width of the containing block | 
| Computed value | the percentage as specified or the absolute length or none | 
| Animation type | a length, percentage or calc(); | 
Sintaxe formal
max-width =
none |
<length-percentage [0,∞]> |
min-content |
max-content |
fit-content( <length-percentage [0,∞]> ) |
<calc-size()> |
<anchor-size()> |
stretch |
fit-content |
contain
<length-percentage> =
<length> |
<percentage>
<calc-size()> =
calc-size( <calc-size-basis> , <calc-sum> )
<anchor-size()> =
anchor-size( [ <anchor-name> || <anchor-size> ]? , <length-percentage>? )
<calc-size-basis> =
<size-keyword> |
<calc-size()> |
any |
<calc-sum>
<calc-sum> =
<calc-product> [ [ '+' | '-' ] <calc-product> ]*
<anchor-name> =
<dashed-ident>
<anchor-size> =
width |
height |
block |
inline |
self-block |
self-inline
<calc-product> =
<calc-value> [ [ '*' | / ] <calc-value> ]*
<calc-value> =
<number> |
<dimension> |
<percentage> |
<calc-keyword> |
( <calc-sum> )
<calc-keyword> =
e |
pi |
infinity |
-infinity |
NaN
Exemplos
>Definindo a largura máxima em pixels
Neste exemplo, o "child" terá 150 pixels de largura ou a largura do "parent", o que for menor:
HTML
<div id="parent">
  <div id="child">
    Fusce pulvinar vestibulum eros, sed luctus ex lobortis quis.
  </div>
</div>
CSS
#parent {
  background: lightblue;
  width: 300px;
}
#child {
  background: gold;
  width: 100%;
  max-width: 150px;
}
Resultado
Especificações
| Specification | 
|---|
| CSS Box Sizing Module Level 4> # sizing-values> | 
Compatibilidade com navegadores
Loading…
Veja também
- O box model, box-sizing
- width,- min-width
- The mapped logical properties: max-inline-size,max-block-size