max() - CSS(层叠样式表) 编辑

max() 这个CSS函数让你可以从一个逗号分隔的表达式列表中选择最大(正方向)的值作为属性的值 . max() 可以用于以下场合 <length>, <frequency>, <angle>, <time>, <percentage>, <number>, 或 <integer>

/* property: max(expression [, expression]) */
width: max(10vw, 4em, 80px);

在上面这个例子中,宽度最小会是80px,除非视图宽度大于800px或者是一个em比20px宽。简单来说,最小宽度是80px。你也可以认为max()的值提供了一个属性最小可能的值。

语法

max() 方法接受一个或多个用逗号分隔的表达式作为他的参数,数值最大的表达式的值将会作为指定的属性的值。

表达式可以是数学运算 (可在 calc() 了解更多信息), 直接数值, 或者是其他表达式,例如attr(),这将会计算成一个合法的参数类型(例如 <length>),也可以是嵌套的  min() 和 max() 函数.

你可以使用你的表达式中分别使用不同的单位。需要时,你也可以使用小括号来设定运算顺序。

备注

  • Math expressions involving percentages for widths and heights on table columns, table column groups, table rows, table row groups, and table cells in both auto and fixed layout tables may be treated as if auto had been specified.
  • It is permitted to nest min() and other max() functions as expression values. The expressions are full math expressions, so you can use direct addition, subtraction, multiplication and division without using the calc() function itself.
  • The expression can be values combining the addition ( + ), subtraction ( - ), multiplication ( * ) and division ( / ) operators, using standard operator precedence rules. Make sure to put a space on each side of the + and - operands. The operands in the expression may be any <length> syntax value. 
  • Oftentimes you will want to combine min() and max() values, or use max() within a clamp() or calc() function.

Formal syntax

max( <calc-sum># )

where
<calc-sum> = <calc-product> [ [ '+' | '-' ] <calc-product> ]*

where
<calc-product> = <calc-value> [ '*' <calc-value> | '/' <number> ]*

where
<calc-value> = <number> | <dimension> | <percentage> | ( <calc-sum> )

例子

使图片保持一个最小的尺寸

max() makes it easy to set a minimum width for an image. In this example, the CSS creates a logo that stretches half way across the window on larger devices, but does not not exceed 300px on wider devices, without the use of media queries:

.logo {
  width: max(50vw, 300px);
}
<img src="/wiki/static/img/web-docs-sprite.svg" alt="MDN Web Docs" class="logo">

In this example, the logo will be at least 300px wide, but wider if the viewport grows above 600px, at which point it will grow as the viewport grows, always being 50% of the width of the viewport.

为字体设定一个最小字号

Another use case for CSS functions is allow a font size to grow while ensuring it is at least a mimum size, enabling responsive font sizes while ensuring legibility.

Let's look at some CSS:

h1 {
  font-size: 2rem;
}
h1.responsive {
  font-size: max(4vw, 2em, 2rem);
}

The font-size will at minimum be 2rems, or twice the default size of font for the page. This ensure it is legible and ensures accessibility

<h1>This text is always legible, but doesn't change size</h1>
<h1 class="responsive">This text is always legible, and is responsive, to a point</h1>

Think of the max() function as finding the minimum value allowed for a property.

无障碍

When max() is used for controlling text size, make sure the text is always large enough to read. A suggestion is to use the min() function nested within a max() that has as its second value a relative length unit that is always large enough to read. For example:

small {
  font-size: max(min(0.5vw, 0.5em), 1rem);
}

This ensures a minimum size of 1rem, with a text size that scales if the page is zoomed.

规范

SpecificationStatusComment
CSS Values and Units Module Level 4
max()
Editor's DraftInitial definition

浏览器兼容性

BCD tables only load in the browser

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

也可以看看

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:84 次

字数:14761

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文