CSS - max-width 属性



CSS 的 max-width 属性设置元素的最大宽度。如果元素的宽度超过最大宽度,则元素的高度会增加。如果内容完全适合元素的最大宽度,则不会产生任何效果。

语法

max-width: none | length | percentage | initial | inherit;

属性值

描述
none 不设置元素宽度的限制。默认值。
length 使用长度单位(例如 px、em、rem 等)设置元素的最大宽度。
percentage 使用百分比值(例如 10%)相对于容器大小设置元素的最大宽度。
initial 将属性设置为其默认值。
inherit 从父元素继承该属性。

CSS Max Width 属性示例

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

使用 None 值的 Max Width 属性

为了不设置元素宽度的任何限制,我们可以使用 none 值。定义的宽度将应用于元素。这在以下示例中显示。

示例

<!DOCTYPE html>
<html>
<head>
   <style>
      .container {
         width: 300px;
         height: 120px;
         padding: 5px;
         max-width: none;
         background-color: lightgreen;
      }
   </style>
</head>
<body>
   <h2>
      CSS max-width property
   </h2>
   <h4>
      max-width: none (the defined width 
      value will be applied)
   </h4>
   <div class="container">
      TutorialsPoint is an online platform offering free 
      tutorials and resources on various topics including 
      programming, web development, and data science. It 
      provides structured lessons, examples, and quizzes 
      for effective learning.
   </div>
</body>
</html>

使用长度值的 Max Width 属性

为了设置元素宽度的最大限制,我们可以使用长度单位(例如 px、em、rem 等)指定最大宽度值。这在以下示例中显示。

示例

<!DOCTYPE html>
<html>
<head>
   <style>
      .container {
         width: 300px;
         height: 120px;
         padding: 5px;
         background-color: lightgreen;
      }

      .ex1 {
         max-width: 200px;
      }

      .ex2 {
         max-width: 250px;
      }
   </style>
</head>
<body>
   <h2>
      CSS max-width property
   </h2>
   <h4>
      max-width: 200px (the maximum width is 200px, 
      if content is larger, the height will increase.)
   </h4>
   <div class="container ex1">
      TutorialsPoint is an online platform offering free 
      tutorials and resources on various topics including 
      programming, web development, and data science. It 
      provides structured lessons, examples, and quizzes 
      for effective learning.
   </div>
   <br/>
   <h4>
      max-width: 250px (the maximum width is 250px, 
      in the following example content is not greater 
      than the width, so no height change occurs.)
   </h4>
   <div class="container ex2">
      TutorialsPoint is an online platform offering free 
      tutorials and resources on various topics including 
      programming, web development, and data science. It 
      provides structured lessons, examples, and quizzes 
      for effective learning.
   </div>
</body>
</html>

使用百分比值的 Max Width 属性

为了设置元素宽度的最大限制,我们可以使用百分比值(例如 10%)相对于包含元素的大小指定最大宽度值。这在以下示例中显示。

示例

<!DOCTYPE html>
<html>
<head>
   <style>
      .outer-container {
         width: 400px;
         height: 180px;
         padding: 5px;
      }

      .inner-container {
         width: 100%;
         width: 100%;
         position: relative;
      }

      .color {
         background-color: lightgreen;
      }

      .ex1 {
         max-width: 40%;
      }

      .ex2 {
         max-width: 90%;
      }
   </style>
</head>
<body>
   <h2>
      CSS max-width property
   </h2>
   <h4>
      max-width: 55% (the maximum width is 55% of the 
      inner-container's width, height increases, if 
      content is larger than the max-width as in this case.)
   </h4>
   <div class="outer-container">
      <div class="inner-container">
         <div class="ex1 color">
            TutorialsPoint is an online platform offering free 
            tutorials and resources on various topics including 
            programming, web development, and data science. It 
            provides structured lessons, examples, and quizzes 
            for effective learning.
         </div>
      </div>
   </div>
   <br/>
   <h4>
      max-width: 90% (the maximum width is 90% of the 
      inner-container's width, height does not increase 
      as max-width accomodates all the content)
   </h4>
   <div class="outer-container">
      <div class="inner-container">
         <div class="ex2 color">
            TutorialsPoint is an online platform offering free 
            tutorials and resources on various topics including 
            programming, web development, and data science. It 
            provides structured lessons, examples, and quizzes 
            for effective learning.
         </div>
      </div>
   </div>
</body>
</html>

支持的浏览器

属性 Chrome Edge Firefox Safari Opera
max-width 1.0 7.0 1.0 2.0.2 7.0
css_properties_reference.htm
广告