{"id":9372,"date":"2020-04-12T23:29:02","date_gmt":"2020-04-12T15:29:02","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9372"},"modified":"2020-04-12T23:29:02","modified_gmt":"2020-04-12T15:29:02","slug":"css-min-max-clamp","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2020\/04\/css-min-max-clamp\/","title":{"rendered":"\u4e86\u89e3CSS min()\/max()\/clamp()\u6570\u5b66\u51fd\u6570"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9372\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9372<\/a><br \/>\n\u672c\u6587\u6b22\u8fce\u5206\u4eab\u4e0e\u805a\u5408\uff0c\u5168\u6587\u8f6c\u8f7d\u5c31\u4e0d\u5fc5\u4e86\uff0c\u5c0a\u91cd\u7248\u6743\uff0c\u5708\u5b50\u5c31\u8fd9\u4e48\u5927\uff0c\u82e5\u6025\u7528\u53ef\u4ee5\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/202004-min-max-clamp.png';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/202004\/2020-04-12_230431.png\" width=\"650\" height=\"336\" alt=\"\u517c\u5bb9\u6027\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<p><code>min()\/max()\/clamp()<\/code>\u8fd93\u4e2a\u6570\u5b66\u51fd\u6570\u662f\u73b0\u4ee3\u6d4f\u89c8\u56682018\u5e74\u5e95\u5f00\u59cb\u652f\u6301\u7684\uff0c\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u80c6\u513f\u8db3\u591f\u80a5\uff0c\u662f\u53ef\u4ee5\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u5c0f\u8303\u56f4\u4f7f\u7528\u7684\u3002<\/p>\n<p>\u548c<code>calc()<\/code>\u51fd\u6570\u7c7b\u4f3c\uff0c\u4efb\u4f55\u53ef\u4ee5\u4f7f\u7528<code>&lt;length&gt;<\/code>\uff0c<code>&lt;frequency&gt;<\/code>\uff0c<code>&lt;angle&gt;<\/code>\uff0c<code>&lt;time&gt;<\/code>\uff0c<code>&lt;percentage&gt;<\/code>\uff0c<code>&lt;number&gt;<\/code>\u6216\u8005<code>&lt;integer&gt;<\/code>\u6570\u636e\u7c7b\u578b\u7684\u5730\u65b9\u90fd\u53ef\u4ee5\u4f7f\u7528<code>min()\/max()\/clamp()<\/code>\u8fd93\u4e2a\u6570\u5b66\u51fd\u6570\u3002<\/p>\n<p><code>min()\/max()\/clamp()<\/code>\u8fd93\u4e2a\u6570\u5b66\u51fd\u6570\u548c<code>calc()<\/code>\u51fd\u6570\u662f\u53ef\u4ee5\u76f8\u4e92\u5d4c\u5957\u4f7f\u7528\u7684\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>width: calc(min(800px, 100vw) \/ 6);<\/pre>\n<p>\u4e0b\u9762\u5177\u4f53\u4ecb\u7ecd\u4e0b<code>min()\/max()\/clamp()<\/code>\u8fd93\u4e2a\u6570\u5b66\u51fd\u6570\u3002<\/p>\n<h3>\u4e00\u3001min()\u51fd\u6570<\/h3>\n<p>\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>min(expression [, expression])<\/pre>\n<p>\u652f\u6301\u4e00\u4e2a\u6216\u591a\u4e2a\u8868\u8fbe\u5f0f\uff0c\u6bcf\u4e2a\u8868\u8fbe\u5f0f\u4e4b\u95f4\u4f7f\u7528\u9017\u53f7\u5206\u9694\uff0c\u7136\u540e\u4ee5\u6700\u5c0f\u7684\u8868\u8fbe\u5f0f\u7684\u503c\u4f5c\u4e3a\u8fd4\u56de\u503c\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>width: min(10vw, 5em, 80px);<\/pre>\n<p>\u5176\u4e2d\u51fa\u73b0\u4e862\u4e2a\u76f8\u5bf9\u957f\u5ea6\u503c\uff0c1\u4e2a\u56fa\u5b9a\u957f\u5ea6\u503c\u3002\u56e0\u6b64\uff0c\u4e0a\u9762\u7684<code>width<\/code>\u8ba1\u7b97\u503c\u6700\u5927\u5c31\u662f80px\u3002\u81f3\u4e8e\u771f\u5b9e\u7684\u5bbd\u5ea6\u503c\uff0c\u5982\u679c\u6d4f\u89c8\u5668\u89c6\u53e3\u5c0f\u4e8e800px\u5bbd\uff0c\u6216\u6587\u5b57\u5b57\u53f7\u5c0f\u4e8e16px\uff0c\u5219\u4f1a\u66f4\u5c0f\u3002<\/p>\n<p>\u4e5f\u5c31\u662f\uff0c\u867d\u7136\u51fd\u6570\u7684\u540d\u79f0\u662f<code>min()<\/code>\uff0c\u8868\u793a\u6700\u5c0f\uff0c\u5b9e\u9645\u4e0a\u8fd9\u4f5c\u7528\u662f\u7528\u6765\u9650\u5236\u6700\u5927\u503c\u7684\u3002<\/p>\n<p><code>min()<\/code>\u51fd\u6570\u7684\u8868\u8fbe\u5f0f\u53ef\u4ee5\u662f\u6570\u5b66\u8868\u8fbe\u5f0f\uff08\u4f7f\u7528\u7b97\u672f\u8fd0\u7b97\u7b26\uff09\u3001\u5177\u4f53\u7684\u503c\u6216\u5176\u4ed6\u8868\u8fbe\u5f0f\uff08\u5982attr()\u65b0\u8bed\u6cd5\uff09\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u4e0b\u9762\u8fd9\u4e9bCSS\u58f0\u660e\u90fd\u662f\u5408\u6cd5\u7684\uff1a<\/p>\n<pre><span class=\"green\" style=\"color:green;\">\/* \u5408\u6cd5 *\/<\/span>\r\nwidth: min(10px * 10, 10em);\r\nwidth: min(calc(10px * 10), 10em);\r\nwidth: min(10px * 10, var(--width));<\/pre>\n<p>\u8fc7\u53bb\u6211\u4eec\u5e0c\u671b\u7f51\u9875\u5728\u684c\u9762\u7aef\u6d4f\u89c8\u56681024\u50cf\u7d20\uff0c\u5728\u79fb\u52a8\u7aef100%\u5bbd\u5ea6\u662f\u8fd9\u4e48\u5b9e\u73b0\u7684\uff1a<\/p>\n<pre>.constr {\r\n    width: 1024px;\r\n    max-width: 100%;\r\n}<\/pre>\n<p>\u73b0\u5728\u6709\u4e86<code>min()<\/code><\/p>\n<p>\u51fd\u6570\uff0c\u6211\u4eec\u53ea\u9700\u8981\u4e00\u53e5CSS\u58f0\u660e\u5c31\u53ef\u4ee5\u5b9e\u73b0\u4e86\uff1a<\/p>\n<pre>.constr {\r\n    width: min(1024px, 100%);\r\n}<\/pre>\n<h3>\u4e8c\u3001max()\u51fd\u6570<\/h3>\n<p><code>max()<\/code>\u51fd\u6570\u548c<code>min()<\/code>\u51fd\u6570\u8bed\u6cd5\u7c7b\u4f3c\uff0c\u533a\u522b\u5728\u4e8e<code>max()<\/code>\u51fd\u6570\u8fd4\u56de\u7684\u662f\u6700\u5927\u503c\uff0c<code>min()<\/code>\u51fd\u6570\u8fd4\u56de\u7684\u662f\u6700\u5c0f\u503c\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>width: max(10vw, 5em, 80px);<\/pre>\n<p>\u8868\u793a\u6700\u5c0f\u5bbd\u5ea6\u662f<code>80px<\/code>\uff0c\u5982\u679c\u6d4f\u89c8\u5668\u89c6\u53e3\u5927\u4e8e800px\uff0c\u6216\u8005\u6587\u5b57\u5b57\u53f7\u5927\u4e8e16px\uff0c\u5219\u6700\u7ec8\u7684\u5bbd\u5ea6\u503c\u4f1a\u66f4\u5927\u3002<\/p>\n<p>\u4e5f\u5c31\u662f\uff0c\u867d\u7136<code>max()<\/code>\u51fd\u6570\u987e\u540d\u601d\u4e49\u662f\u6700\u5927\uff0c\u4f46\u662f\u5b9e\u9645\u4f5c\u7528\u662f\u7528\u6765\u9650\u5236\u6700\u5c0f\u503c\u7684\u3002<\/p>\n<p>\u5176\u4ed6\u7279\u6027\u90fd\u548c<code>min()<\/code>\u51fd\u6570\u7c7b\u4f3c\uff0c\u4e0d\u8d58\u8ff0\u3002<\/p>\n<h3>\u4e09\u3001clamp()\u51fd\u6570<\/h3>\n<p>clamp()\u51fd\u6570\u4f5c\u7528\u662f\u8fd4\u56de\u4e00\u4e2a\u533a\u95f4\u8303\u56f4\u7684\u503c\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>clamp(MIN, VAL, MAX)<\/pre>\n<p>\u5176\u4e2dMIN\u8868\u793a\u6700\u5c0f\u503c\uff0cVAL\u8868\u793a\u9996\u9009\u503c\uff0cMAX\u8868\u793a\u6700\u5927\u503c\u3002\u610f\u601d\u662f\uff0c\u5982\u679cVAL\u5728MIN\u548cMAX\u8303\u56f4\u4e4b\u95f4\uff0c\u5219\u4f7f\u7528VAL\u4f5c\u4e3a\u51fd\u6570\u8fd4\u56de\u503c\uff1b\u5982\u679cVAL\u5927\u4e8eMAX\uff0c\u5219\u4f7f\u7528MAX\u4f5c\u4e3a\u8fd4\u56de\u503c\uff1b\u5982\u679cVAL\u5c0f\u4e8eMIN\uff0c\u5219\u4f7f\u7528MIN\u4f5c\u4e3a\u8fd4\u56de\u503c\u3002<\/p>\n<p><code>clamp(MIN, VAL, MAX)<\/code>\u5b9e\u9645\u4e0a\u7b49\u540c\u4e8e<code>max(MIN, min(VAL, MAX))<\/code>\u3002<\/p>\n<p>\u6211\u4eec\u4e3e\u4e2a\u4f8b\u5b50\u770b\u4e0b\u6548\u679c\uff1a<\/p>\n<pre>&lt;button&gt;\u6211\u7684\u5bbd\u5ea6\u662f\uff1f&lt;\/button&gt;<\/pre>\n<pre>button {\r\n    width: clamp(200px, 50vw, 600px);\r\n}<\/pre>\n<p>\u5982\u679c\u6211\u4eec\u4e0d\u65ad\u6539\u53d8\u6d4f\u89c8\u5668\u7684\u5bbd\u5ea6\uff0c\u53ef\u4ee5\u770b\u5230\u6309\u94ae\u7684\u5bbd\u5ea6\u5728<code>200px-600px<\/code>\u4e4b\u95f4\u53cd\u590d\u6a2a\u8df3\u3002<\/p>\n<p>\u4f8b\u5982\u6d4f\u89c8\u5668\u89c6\u53e3\u5f88\u5bbd\u7684\u65f6\u5019\uff0c\u6309\u94ae\u5bbd\u5ea6\u662f600px\uff1b\u968f\u7740\u6d4f\u89c8\u5668\u89c6\u53e3\u5bbd\u5ea6\u4e0d\u65ad\u53d8\u5c0f\uff0c\u5c0f\u5230646px\u7684\u65f6\u5019\uff0c\u6309\u94ae\u7684\u5bbd\u5ea6\u53d8\u6210\u4e86323px\uff1b\u968f\u7740\u6d4f\u89c8\u5668\u89c6\u53e3\u5bbd\u5ea6\u8fdb\u4e00\u6b65\u53d8\u5c0f\uff0c\u4f1a\u53d1\u73b0\u6309\u94ae\u5bbd\u5ea6\u7ef4\u6301\u5728<code>200px<\/code>\u4e0d\u518d\u53d8\u5c0f\u4e86\u3002\u5982\u4e0b\u56fe\u6240\u793a\u3002<\/p>\n<div class=\"figure\">\n    <img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202004\/4-63_clamp-size-1.png\" loading=\"lazy\" width=\"608\" height=\"66\">\n<\/div>\n<div class=\"figure\">\n    <img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202004\/4-64_clamp-size-2.png\" loading=\"lazy\" width=\"327\" height=\"63\">\n<\/div>\n<div class=\"figure\">\n    <img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202004\/4-65_clamp-size-3.png\" loading=\"lazy\" width=\"206\" height=\"62\">\n<\/div>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/202004\/css-clamp-function-demo.php\" rel=\"noopener\" target=\"_blank\">CSS clamp()\u51fd\u6570\u57fa\u672c\u6548\u679c\u6f14\u793ademo<\/a><\/p>\n<h3>\u56db\u3001\u7ed3\u8bed<\/h3>\n<p>CSS\u65b0\u7279\u6027\u51fa\u73b0\u7684\u592a\u591a\u4e86\u592a\u5feb\uff0c\u591a\u5230\u6709\u4e9b\u6ea2\u51fa\uff0c\u5feb\u5230\u5e94\u63a5\u4e0d\u6687\u3002<\/p>\n<p>\u6211\u611f\u89c9<code>attr()<\/code>\u652f\u6301\u6570\u636e\u7c7b\u578b\u7684\u8bed\u6cd5\u5feb\u8981\u6709\u6d4f\u89c8\u5668\u652f\u6301\u4e86\u3002<\/p>\n<p>\u8981\u662f\u652f\u6301\u4e86\uff0c\u53ef\u5c31\u5389\u5bb3\u5927\u53d1\u4e86\u6bd4CSS Houdini\u8fd8\u8981\u8ba9\u4eba\u5174\u594b\uff0c\u62ed\u76ee\u4ee5\u5f85\u5427\u3002<\/p>\n<p>\u5468\u672b\u4e24\u5929\u57fa\u672c\u4e0a\u90fd\u5728\u4e00\u76f4\u5199\u4e1c\u897f\uff0c\u5c31\u4e0d\u518d\u5570\u55e6\u4e86\u3002<\/p>\n<p>\u4f30\u8ba1\u8fd9\u7bc7\u6587\u7ae02\u5e74\u4e4b\u540e\u4f1a\u8fdb\u5165\u56f4\u89c2\u9ad8\u5cf0\u671f\u3002<\/p>\n<p>\u6b22\u8fce\u9605\u8bfb\u6b22\u8fce\u5206\u4eab\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/emtion\/emoji\/2600.svg\" width=\"20\" height=\"20\" align=\"absmiddle\"><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u6b22\u8fce\u5206\u4eab\uff0c\u52ff\u5168\u6587\u8f6c\u8f7d\uff0c\u5982\u679c\u5b9e\u5728\u559c\u6b22\uff0c\u53ef\u6536\u85cf\uff0c\u6c38\u4e0d\u8fc7\u671f\uff0c\u4e14\u4f1a\u53ca\u65f6\u66f4\u65b0\u77e5\u8bc6\u70b9\u53ca\u4fee\u6b63\u9519\u8bef\uff0c\u9605\u8bfb\u4f53\u9a8c\u4e5f\u66f4\u597d\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9372\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9372<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/202004\/min-max-clamp-cover.png\" width=\"160\" height=\"105\" alt=\"CSS\u6570\u5b66\u51fd\u6570\u5c01\u9762\" class=\"alignright  size-medium\" loading=\"lazy\" \/><\/p>\n<p>\u76ee\u524d\u4e3b\u6d41\u6d4f\u89c8\u5668\u90fd\u5df2\u7ecf\u652f\u6301\u4e86CSS min()\/max()\/clamp()\u6570\u5b66\u51fd\u6570\uff0c\u53ef\u4ee5\u8ba9\u4f60\u7684\u5e03\u5c40\u548c\u6837\u5f0f\u8868\u73b0\u66f4\u52a0\u7684\u667a\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[971,1593,1591,1595,1592,1594,765,1527],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9372"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=9372"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9372\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=9372"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=9372"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=9372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}