@@ -20,6 +20,9 @@ spec: css-backgrounds-3; type: type; text: <position>
20
20
spec: css2; type: property; text: border-collapse
21
21
spec: css-color-4; type: value; text: currentcolor
22
22
spec: css-cascade-4; type: at-rule; text: @import
23
+ spec: css-sizing-3; type: property;
24
+ text: min-width
25
+ text: box-sizing
23
26
</pre>
24
27
<style>
25
28
code, small { white-space: nowrap }
@@ -1633,59 +1636,90 @@ Mathematical Expressions: ''calc()'', ''min()'', ''max()'', and ''clamp()''</h3>
1633
1636
that evaluate to a valid argument type (like <<length>> ).
1634
1637
1635
1638
<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>
1637
1649
section {
1638
1650
float: left;
1639
1651
margin: 1em; border: solid 1px;
1640
1652
width: calc(100%/3 - 2*1em - 2*1px);
1641
1653
}
1642
1654
</pre>
1643
- </div>
1644
1655
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%);
1649
1663
}
1650
1664
</pre>
1651
1665
</div>
1652
1666
1653
1667
<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,
1655
1673
ensuring that roughly the same amount of text always fills the screen no matter the screen size.
1656
1674
1657
- <pre>
1675
+ <pre class=lang-css >
1658
1676
:root {
1659
- font-size: calc(100vw / 40 );
1677
+ font-size: calc(100vw / 35 );
1660
1678
}
1661
1679
</pre>
1662
1680
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.
1665
1687
</div>
1666
1688
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">
1670
1690
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);
1676
1700
}
1677
1701
</pre>
1678
- </div>
1679
1702
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>
1682
1708
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);
1689
1723
}
1690
1724
</pre>
1691
1725
</div>
0 commit comments