CSS - max-inline-size 属性



CSS max-inline-size 属性设置元素的最大内联大小。内联的方向由 writing-mode 属性决定。如果内容在元素的内联大小内可以很好地适应,则该属性无效。

语法

max-inline-size: auto | length | percentage | initial | inherit;

属性值

描述
auto 此值不设置宽度限制。默认值。
长度 它使用长度单位(例如 px、em、rem 等)设置元素的 max-inline-size。
百分比 它使用相对于包含元素大小的百分比值设置元素的 max-inline-size。
initial 它将属性设置为其默认值。
inherit 它从父元素继承属性。

CSS 最大内联大小属性示例

以下示例使用不同的值说明了 max-inline-size 属性。

使用 Auto 值的最大内联大小属性

为了不设置元素 inline-size 的任何特定限制,我们使用 auto 值。元素的大小取决于内容的长度。以下示例显示了这一点。

示例

<!DOCTYPE html>
<html>
<head>
   <style>
      .container {
         background-color: lightgreen;
         max-inline-size: auto;
         display: inline-block;
      }
   </style>
</head>
<body>
   <h2>
      CSS max-inline-size property
   </h2>
   <h4>
      max-inline-size: auto
   </h4>
   <div class="container">
      <p>
         TutorialsPoint offers comprehensive online 
         tutorials across various subjects, including 
         programming, web development, and data science. 
         It provides accessible, step-by-step guides,
         practical examples, and interactive learning 
         resources for learners of all levels.
      </p>
   </div>
</body>
</html>

使用长度值的最大内联大小属性

要设置元素的内联大小,我们可以使用长度单位(例如 px、em、rem 等)指定大小。指定的大小将应用于元素。如果内容大于元素的大小,它将溢出。以下示例显示了这一点。

示例

<!DOCTYPE html>
<html>
<head>
   <style>
      .container {
         background-color: lightgreen;
         margin: 10px;
         display: inline-block;
      }

      .size1 {
         max-inline-size: 160px;
      }

      .size2 {
         max-inline-size: 6.5em;
      }
   </style>
</head>
<body>
   <h2>
      CSS max-inline-size property
   </h2>
   <h4>
      max-inline-size: 160px
   </h4>
   <div class="container size1">
      <p>
         TutorialsPoint offers comprehensive online 
         tutorials across various subjects, including 
         programming, web development, and data science. 
         It provides accessible, step-by-step guides,
         practical examples, and interactive learning 
         resources for learners of all levels.
      </p>
   </div>
   <br/>
   <h4>
      max-inline-size: 6.5em
   </h4>
   <div class="container size2">
      <p>
         TutorialsPoint offers comprehensive online 
         tutorials across various subjects, including 
         programming, web development, and data science. 
         It provides accessible, step-by-step guides,
         practical examples, and interactive learning 
         resources for learners of all levels.
      </p>
   </div>
</body>
</html>

使用百分比值的最大内联大小属性

要设置元素的内联大小,我们可以使用相对于包含元素大小的百分比值(例如 10%)指定大小。指定的大小将应用于元素。如果内容大于元素的大小,它将溢出。以下示例显示了这一点。

示例

<!DOCTYPE html>
<html>
<head>
   <style>
      .outer-container {
         height: 150px;
      }

      .container {
         display: inline-block;
         background-color: lightgreen;
      }

      .size1 {
         max-inline-size: 45%;
      }

      .size2 {
         max-inline-size: 55%;
      }
   </style>
</head>
<body>
   <h2>
      CSS max-inline-size property
   </h2>
   <h4>
      max-inline-size: 45% (of the size 
      of the containing element)
   </h4>
   <div class="outer-container">
      <div class="container size1">
         <p>
            TutorialsPoint offers comprehensive online 
            tutorials across various subjects, including 
            programming, web development, and data science. 
            It provides accessible, step-by-step guides,
            practical examples, and interactive learning 
            resources for learners of all levels.
         </p>
      </div>
   </div>
   <br/>
   <h4>
      max-inline-size: 55% (of the size 
      of the containing element)
   </h4>
   <div class="outer-container">
      <div class="container size2">
         <p>
            TutorialsPoint offers comprehensive online 
            tutorials across various subjects, including 
            programming, web development, and data science. 
            It provides accessible, step-by-step guides,
            practical examples, and interactive learning 
            resources for learners of all levels.
         </p>
      </div>
   </div>
</body>
</html>

使用书写模式的最大内联大小属性

max-inline-size 属性可以与 writing-mode 属性结合使用,该属性确定内联方向。在水平模式下,inline-size 从左到右增长。在垂直模式下,inline-size 从上到下(或从下到上)增长。以下示例显示了这一点。

示例

<!DOCTYPE html>
<html>
<head>
   <style>
      .outer-container {
         height: 150px;
      }

      .container {
         display: inline-block;
         background-color: lightgreen;
         max-inline-size: 65%;
      }

      .horizontal {
         writing-mode: horizontal-lr;
      }

      .vertical {
         writing-mode: vertical-rl;
      }
   </style>
</head>
<body>
   <h2>
      CSS max-inline-size property
   </h2>
   <h4>
      max-inline-size: 65% (of the size of the 
      containing element); writing-mode: horizontal-lr;
   </h4>
   <div class="outer-container">
      <div class="container horizontal">
         <p>
            TutorialsPoint offers comprehensive online 
            tutorials across various subjects, including 
            programming, web development, and data science. 
            It provides accessible, step-by-step guides,
            practical examples, and interactive learning 
            resources for learners of all levels.
         </p>
      </div>
   </div>
   <br/>
   <h4>
      max-inline-size: 65% (of the size of the 
      containing element); writing-mode: vertical-rl;
   </h4>
   <div class="outer-container">
      <div class="container vertical">
         <p>
            TutorialsPoint offers comprehensive online 
            tutorials across various subjects, including 
            programming, web development, and data science. 
            It provides accessible, step-by-step guides,
            practical examples, and interactive learning 
            resources for learners of all levels.
         </p>
      </div>
   </div>
</body>
</html>

支持的浏览器

属性 Chrome Edge Firefox Safari Opera
max-inline-size 57.0 79.0 41.0 12.1 44.0
css_properties_reference.htm
广告