min()
åºçº¿
广æ³å¯ç¨
èª 2020å¹´7æ èµ·ï¼æ¤ç¹æ§å·²å¨ä¸»æµæµè§å¨ä¸å¾å°æ¯æï¼å¯å¨å¤§å¤æ°è®¾å¤åæµè§å¨çæ¬ä¸æ£å¸¸ä½¿ç¨ã
min() CSS æ¹æ³å
è®¸ä½ ä»éå·åé符表达å¼ä¸éæ©ä¸ä¸ªæå°å¼ä½ä¸º CSS ç屿§å¼ãmin() æ¹æ³å¯ä»¥ç¨äºä»¥ä¸ä»»ä½å±æ§ä¸ <length>, <frequency>, <angle>, <time>, <percentage>,<number>, æè
<integer>ã
/* property: min(expression [, expression]) */
width: min(1vw, 4em, 80px);
å¨ä¸é¢çä¾åä¸ï¼å®½åº¦æå¤æ¯ 80pxã妿è§å£ç宽度å°äº 800pxï¼æè ä¸ä¸ª em ç宽度å°äº 20pxï¼å伿´çªãæ¢å¥è¯è¯´ï¼æå¤§å®½åº¦æ¯ 80pxï¼
è¯æ³
min() æ¹æ³æ¥æä¸ä¸ªæå¤ä¸ªéå·åé符表达å¼ä½ä¸ºåæ°ï¼è¡¨è¾¾å¼çå¼ä¸æå°çå¼ä½ä¸ºåæ°å¼ã表达å¼å¯ä»¥æ¯æ°å¦å½æ°ï¼æ¥ç calc() äºè§£æ´å¤ï¼ãåé¢éï¼æè
å
¶ä»è¡¨è¾¾å¼ï¼æ¯å¦ attr()ï¼å¯ä»¥æ±å¾ææå¼çç±»åï¼æ¯å¦ <length>ï¼ãå¦æä½ æ¿æï¼ä½ çè³å¯ä»¥å¨è¡¨è¾¾å¼ä¸ç»æ¯ä¸ªå¼ä¸ä¸ªä¸åçåä½ãå¹¶ä¸å¨éè¦çå°æ¹åªç¨åæ¬å·æ¹å计ç®ä¼å
级ã
注æ
- 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
autohad been specified.max - It is permitted to nest
max()and othermin()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. You can use different units for each value in your expression. You may also use parentheses to establish computation order when needed. - Oftentimes you will want to comine
min()andmax()values, or usemin()within aclamp()orcalc()function. - You can provide more than two arguments, if you have multiple constraints to apply.
å½¢å¼è¯æ³
<min()> =
min( <calc-sum># )
<calc-sum> =
<calc-product> [ [ '+' | '-' ] <calc-product> ]*
<calc-product> =
<calc-value> [ [ '*' | / ] <calc-value> ]*
<calc-value> =
<number> |
<dimension> |
<percentage> |
<calc-keyword> |
( <calc-sum> )
<calc-keyword> =
e |
pi |
infinity |
-infinity |
NaN
示ä¾
>Growing images to a maximum size
min() 让æä»¬å¨è®¾ç½®å¾åçæå¤§å®½åº¦æ¶æ´ç®åãå¨ä¸é¢è¿ä¸ªä¾åä¸ï¼å¨å°å设å¤ä¸å®½åº¦æä¼¸ä¸º window çä¸åï¼ä½å¨å¤§å设å¤ä¸ï¼ä¸è¶
è¿ 300pxï¼ä¸ä½¿ç¨åªä½æ¥è¯¢ï¼
.logo {
width: min(50vw, 300px);
}
<img
src="/proxy/developer.mozilla.org/shared-assets/images/examples/web-docs-sprite.svg"
alt="MDN Web Docs"
class="logo" />
å¨è¿ä¸ªä¾åä¸ï¼é¤éè§å£æ¯ 600px æ´çªï¼å¦å logo ç宽ä¼è¢«è®¾ç½®ä¸º 300pxï¼å°æ¶åï¼å®ä¼éçè§å£ç缩å°è缩å°ï¼è䏿»æ¯è§å£å®½åº¦ç 50%ã
ç» label å input 设置æå¤§å¼
CSS æ¹æ³çå¦ä¸ä¸ªç¨éæ¶è®¾ç½®ååºå¼ç»ä»¶ï¼formï¼çæå¤§å°ºå¯¸ï¼è®© label å input ç宽度éçç»ä»¶ç缩å°è缩å°ã
çä¸é¢çæ ·å¼ï¼
input,
label {
padding: 2px;
box-sizing: border-box;
display: inline-block;
width: min(40%, 400px);
background-color: pink;
}
form {
margin: 4px;
border: 1px solid black;
padding: 4px;
}
è¿éï¼form ç marginï¼borderï¼padding æ»æ¯å®ç¶å ç´ ç宽度ç 100%ãæä»¬å£°æ input å label çå®½åº¦æ¯ form ç 40% è¿å°æè 400px 宽ï¼åªè¦åå³äºåªä¸ä¸ªæå°ãæ¢å¥è¯è¯´ï¼input å label çæå¤§å®½åº¦å°±æ¯ 400pxï¼æçªå°±æ¯ form ç 40%ãå æ¤çèµ·æ¥ä¼æ¾å¾å¾å°ã
<form>
<label>Type something:</label>
<input type="text" />
</form>
Think of the min() function as finding the maximum value allowed for a property.
æ éç¢é®é¢
å½min() ç¨äºæ§å¶ææ¬å¤§å°æ¶ï¼è¦ä¿è¯ææ¬è¶³å¤å¤§ä»¥ä¾¿äºé
读ã建议æ min() æ¹æ³åµå
¥å° max() ä¸ï¼ relative length unit è¿æ ·å°±å¯ä»¥ä¾¿äºé
è¯»ï¼æ¯å¦ï¼
small {
font-size: max(min(0.5vw, 0.5em), 1rem);
}
è¿ç¨äºä¿è¯æå°å¼æ¯ 1remï¼è¿æ ·å¨é¡µé¢ç¼©æ¾æ¶ææ¬ä¹ä¼ç¼©æ¾ã
è§è
| è§è |
|---|
| CSS Values and Units Module Level 4> # calc-notation> |