Css 简明教程

CSS - min-content Property

CSS min-content 是一个值,它可用于对大小属性进行大小设定,根据内容设定盒子或元素的最小尺寸。

它特别建议文本内容会利用软换行机会,这样材料就能调整为其最长单词的长度。 min-content 根据盒子内内容所需的当然最小尺寸计算而得。

Syntax

/* Used as a length */
width: min-content;
inline-size: min-content;
height: min-content;
block-size: min-content;
/* Used in grid tracks */
grid-template-columns: 100px 2fr min-content;

CSS min-content - Box Sizing

以下示例演示了 min-content 关键字用于盒子大小设定的用途。

<html>
<head>
<style>
   body {
      background-color: #b9cded;
      font-family: 'Helvetica Neue', Arial, sans-serif;
      margin: 0;
      padding: 20px;
   }
   .container {
      width: 100%;
      display: flex;
      flex-direction: column;
      align-items: flex-start;
   }
   .item {
      width: min-content;
      background-color: #ffffff;
      padding: 5px;
      margin-bottom: 20px;
      border: 1px solid #e0e0e0;
      box-shadow: 0 3px 6px rgba(0, 0, 0, 1);
      color: #444444;
      border-radius: 8px;
   }
</style>
</head>
<body>
<div class="container">
   <div class="item">Min-Content Example.</div>
   <div class="item">Well-Crafted Min-Content Example.</div>
   <div class="item">Thoughtfully Presented Min-Content Example.</div>
</div>
</body>
</html>

CSS min-content - Sizing Grid Columns

以下示例演示了 min-content 关键字用于设定网格列大小的用途。

<html>
<head>
<style>
   body {
      margin: 0;
      font-family: 'Arial', sans-serif;
      background-color: #f9f9f9;
   }
   #container {
      display: grid;
      grid-template-columns: 1fr min-content min-content;
      grid-gap: 15px;
      box-sizing: border-box;
      max-width: 800px;
      margin: 20px auto;
      padding: 20px;
      background-color: #f2f2f2;
      border: 1px solid #82807f;
      box-shadow: 0 4px 8px rgba(0, 0, 0, 0.5);
      border-radius: 8px;
   }
   .item {
      background-color: #5f9ea0;
      color: #141414;
      padding: 15px;
      border-radius: 5px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}
</style>
</head>
<body>
<div id="container">
   <div class="item">Flexible Content Area</div>
   <div class="item">Min-Content 1 Content Area</div>
   <div class="item">Min-Content 2 Content Area</div>
   <div class="item">Dynamic Content Area</div>
   <div class="item">Min-Content 3 Content Area</div>
   <div class="item">Min-Content 4 Content Area</div>
</div>
</body>
</html>