Esta página foi traduzida do inglês pela comunidade.Saiba mais e junte-se à comunidade MDN Web Docs.
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 propriedademax-width doCSS estabelece a largura máxima de um elemento. Ele evita que ovalor usado da propriedadewidth se torne maior que o valor especificado pormax-width.
In this article
Experimente
max-width: 150px;max-width: 20em;max-width: 75%;max-width: 20ch;<section> <div> Change the maximum width. </div></section>#example-element { display: flex; flex-direction: column; background-color: #5b6dcd; height: 80%; justify-content: center; color: #ffffff;}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.noneA largura não possui valor máximo. (padrão)
max-contentExperimentalA
max-widthintrínseca preferida.min-contentExperimentalO 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 commax-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 ornone |
| Animation type | alength,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> <div> 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
Veja também
- O box model,
box-sizing width,min-width- The mapped logical properties:
max-inline-size,max-block-size