@@ -20,6 +20,9 @@ spec: css-backgrounds-3; type: type; text: <position>
2020spec: css2; type: property; text: border-collapse
2121spec: css-color-4; type: value; text: currentcolor
2222spec: css-cascade-4; type: at-rule; text: @import
23+ spec: css-sizing-3; type: property;
24+ text: min-width
25+ text: box-sizing
2326</pre>
2427<style>
2528code, small { white-space: nowrap }
@@ -1633,59 +1636,90 @@ Mathematical Expressions: ''calc()'', ''min()'', ''max()'', and ''clamp()''</h3>
16331636 that evaluate to a valid argument type (like <<length>> ).
16341637
16351638 <div class="example">
1636- <pre>
1639+
1640+ [=Math functions=] can be used to combine value that use different units.
1641+ In this example the author wants the <em> margin box</em> of each section
1642+ to take up 1/3 of the space,
1643+ so they start with ''100%/3'' ,
1644+ then subtract the element's borders and margins.
1645+ ('box-sizing' can automatically achieve this effect for borders and padding,
1646+ but a [=math function=] is needed if you want to include margins.)
1647+
1648+ <pre class=lang-css>
16371649 section {
16381650 float: left;
16391651 margin: 1em; border: solid 1px;
16401652 width: calc(100%/3 - 2*1em - 2*1px);
16411653 }
16421654 </pre>
1643- </div>
16441655
1645- <div class="example">
1646- <pre>
1647- p {
1648- margin: calc(1rem - 2px) calc(1rem - 1px);
1656+ Similar, in this example the gradient will show a color transition
1657+ only in the first and last ''20px'' of the element:
1658+
1659+ <pre class=lang-css>
1660+ .fade {
1661+ background-image: linear-gradient(silver 0%, white 20px,
1662+ white calc(100% - 20px), silver 100%);
16491663 }
16501664 </pre>
16511665 </div>
16521666
16531667 <div class="example">
1654- The following sets the 'font-size' so that exactly 40em fits within the viewport,
1668+
1669+ [=Math functions=] can also be useful just to express values
1670+ in a more natural, readable fashion,
1671+ rather than as an obscure decimal.
1672+ For example, the following sets the 'font-size' so that exactly 35em fits within the viewport,
16551673 ensuring that roughly the same amount of text always fills the screen no matter the screen size.
16561674
1657- <pre>
1675+ <pre class=lang-css >
16581676 :root {
1659- font-size: calc(100vw / 40 );
1677+ font-size: calc(100vw / 35 );
16601678 }
16611679 </pre>
16621680
1663- If the rest of the design is specified using the ''rem'' unit,
1664- the entire layout will scale to match the viewport width.
1681+ Functionality-wise, this is identical to just writing ''font-size: 2.857vw'' ,
1682+ but then the intent
1683+ (that ''35em'' fills the viewport)
1684+ is much less clear to someone reading the code;
1685+ the later reader will have to reverse the math themselves
1686+ to figure out that 2.857 is meant to approximate 100/35.
16651687 </div>
16661688
1667- <div class='example'>
1668- The following example stacks two centered background images,
1669- with one offset slightly from the other.
1689+ <div class="example">
16701690
1671- <pre>
1672- .foo {
1673- background: url(top.png), url(bottom.png);
1674- background-repeat: no-repeat;
1675- background-position: calc(50% + 20px) calc(50% + 20px), 50% 50%;
1691+ ''min()'' , ''max()'' , and ''clamp()'' can be used to make sure a value doesn't exceed a "safe" limit:
1692+ For example, "responsive type" that sets 'font-size' with viewport units
1693+ might still want a minimum size to ensure readability:
1694+
1695+ <pre class=lang-css>
1696+ .type {
1697+ /* Set font-size to 10x the average of vw and vh,
1698+ but don't let it go below 12px. */
1699+ font-size: max(10 * (1vw + 1vh) / 2, 12px);
16761700 }
16771701 </pre>
1678- </div>
16791702
1680- <div class='example'>
1681- This example shows how to place color-stops on a gradient an equal distance from either end.
1703+ Note: Full math expressions are allowed in each of the arguments;
1704+ there's no need to nest a ''calc()'' inside!
1705+ You can also provide more than two arguments,
1706+ if you have multiple constraints to apply.
1707+ </div>
16821708
1683- <pre>
1684- .foo {
1685- background-image: linear-gradient(to right, silver,
1686- white 50px,
1687- white calc(100% - 50px),
1688- silver);
1709+ <div class="example">
1710+ An occasional point of confusion when using ''min()'' /''max()''
1711+ is that you use ''max()'' to impose a minimum value on something
1712+ (that is, properties like 'min-width' effectively use ''max()'' ),
1713+ and ''min()'' to impose a maximum value on something;
1714+ it's easy to accidentally reach for the opposite function
1715+ and try to use ''min()'' to add a minimum size.
1716+ Using ''clamp()'' can make the code read more naturally,
1717+ as the value is nestled between its minimum and maximum:
1718+
1719+ <pre class=lang-css>
1720+ .type {
1721+ /* Force the font-size to stay between 12px and 100px */
1722+ font-size: clamp(12px, 10 * (1vw + 1vh) / 2, 100px);
16891723 }
16901724 </pre>
16911725 </div>
0 commit comments