Aller au contenu

Propriété max-height CSS

La propriété max-height définit la hauteur maximale d'un élément. Si la propriété height est définie sur une valeur plus grande, max-height la remplace. Notez que min-height a la priorité sur max-height, et que max-height a la priorité sur height.

Valeur initialenone
S'applique àTous les éléments, à l'exception des éléments en ligne non remplacés, des colonnes de tableau et des groupes de colonnes.
HéritéeNon.
AnimableOui. La hauteur est animable.
VersionCSS2
Syntaxe DOMobject.style.maxHeight = "50px";

Syntaxe

Syntaxe de la propriété CSS max-height

css
max-height: none | length | percentage | calc() | max-content | min-content | fit-content | initial | inherit;

Exemple de la propriété max-height :

Exemple de la propriété CSS max-height avec une valeur en px

html
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      p {
        max-height: 50px;
        overflow: auto;
        border: 1px solid #666;
        padding: 5px;
      }
    </style>
  </head>
  <body>
    <h2>Max-height property example</h2>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
  </body>
</html>

Exemple de la propriété max-height définie comme "cm" :

Exemple de la propriété CSS max-height avec une valeur en cm

html
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .example1 {
        max-height: 2cm;
        overflow: auto;
        border: 1px solid #666;
        width: 300px;
      }
    </style>
  </head>
  <body>
    <h2>Max-height property example</h2>
    <h3>Max-height: none;</h3>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
    <h3>Max-height: 2cm;</h3>
    <p class="example1">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
  </body>
</html>

Exemple de la propriété max-height avec des valeurs en pourcentage et calc() :

Exemple de la propriété CSS max-height avec des valeurs en pourcentage et calc()

html
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .percent-example {
        max-height: 50%;
        overflow: auto;
        border: 1px solid #666;
        height: 200px;
      }
      .calc-example {
        max-height: calc(100% - 50px);
        overflow: auto;
        border: 1px solid #666;
        height: 300px;
      }
    </style>
  </head>
  <body>
    <h2>Max-height property example</h2>
    <h3>Max-height: 50%;</h3>
    <p class="percent-example">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
    <h3>Max-height: calc(100% - 50px);</h3>
    <p class="calc-example">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
  </body>
</html>

Valeurs

ValeurDescriptionTester »
noneValeur par défaut. Aucune hauteur maximale n'est définie.Tester »
lengthDéfinit une hauteur maximale fixe en px, pt, cm, etc.Tester »
percentageDéfinit la hauteur maximale en pourcentage de la hauteur du bloc contenant.Tester »
calc()Calcule la hauteur maximale à l'aide d'une expression.Tester »
max-contentDéfinit la hauteur maximale à la taille maximale intrinsèque du contenu.Tester »
min-contentDéfinit la hauteur maximale à la taille minimale intrinsèque du contenu.Tester »
fit-contentDéfinit la hauteur maximale à la taille fit-content.Tester »
initialDéfinit cette propriété sur sa valeur par défaut.Tester »
inheritHérite cette propriété de son élément parent.Tester »

Pratique

Quelle est la fonction de la propriété 'max-height' en CSS ?

Trouvez-vous cela utile?

Aperçu dual-run — comparez avec les routes Symfony en production.