CSS - max-block-size 属性



CSS max-block-size 属性设置元素的最大块级尺寸。块的方向由writing-mode 属性决定。如果内容完全适应块内,则此属性无效。

语法

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

属性值

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

CSS 最大块级尺寸属性示例

以下示例解释了具有不同值的max-block-size 属性。

具有 auto 值的最大块级尺寸属性

为了不设置元素block-size 的任何特定限制,我们使用auto 值。块的大小取决于内容的长度。以下示例对此进行了说明。

示例

<!DOCTYPE html>
<html>
<head>
   <style>
      .container {
         background-color: lightgreen;
         max-block-size: auto;
      }
   </style>
</head>
<body>
   <h2>
      CSS max-block-size property
   </h2>
   <h4>
      max-block-size: auto
   </h4>
   <div class="container">
      <p>
         TutorialsPoint offers comprehensive online 
         tutorials across various subjects, including 
      </p>
      <p>
         programming, web development, and data science. 
         It provides accessible, step-by-step guides, 
      </p>
      <p>
         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;
      }

      .size1 {
         max-block-size: 85px;
      }

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

具有百分比值的最大块级尺寸属性

要设置元素块的大小,我们可以使用百分比值(例如 10%)相对于包含元素的大小来指定大小。指定的大小将应用于块。如果内容大于块的大小,它将溢出。以下示例对此进行了说明。

示例

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

      .container {
         background-color: lightgreen;
      }

      .size1 {
         max-block-size: 55%;
      }

      .size2 {
         max-block-size: 75%;
      }
   </style>
</head>
<body>
   <h2>
      CSS max-block-size property
   </h2>
   <h4>
      max-block-size: 55% (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 
         </p>
         <p>
            programming, web development, and data science. 
            It provides accessible, step-by-step guides, 
         </p>
         <p>
            practical examples, and interactive learning 
            resources for learners of all levels.
         </p>
      </div>
   </div>
   <br/>
   <h4>
      max-block-size: 75% (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 
         </p>
         <p>
            programming, web development, and data science. 
            It provides accessible, step-by-step guides, 
         </p>
         <p>
            practical examples, and interactive learning 
            resources for learners of all levels.
         </p>
      </div>
   </div>
</body>
</html>

具有书写模式的最大块级尺寸属性

max-block-size 属性可以与writing-mode 属性结合使用,后者决定块的方向。在水平模式下,block-size 从上到下增长。在垂直模式下,block-size 从左到右增长。以下示例对此进行了说明。

示例

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

      .container {
         background-color: lightgreen;
         max-block-size: 55%;
      }

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

      .vertical {
         writing-mode: vertical-rl;
      }
   </style>
</head>
<body>
   <h2>
      CSS max-block-size property
   </h2>
   <h4>
      max-block-size: 55% (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 
         </p>
         <p>
            programming, web development, and data science. 
            It provides accessible, step-by-step guides, 
         </p>
         <p>
            practical examples, and interactive learning 
            resources for learners of all levels.
         </p>
      </div>
   </div>
   <br/>
   <h4>
      max-block-size: 55% (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 
         </p>
         <p>
            programming, web development, and data science. 
            It provides accessible, step-by-step guides, 
         </p>
         <p>
            practical examples, and interactive learning 
            resources for learners of all levels.
         </p>
      </div>
   </div>
</body>
</html>

支持的浏览器

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